Skip to content

Commit

Permalink
Project kick-off
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Kostler committed Apr 13, 2011
0 parents commit 87ccfb8
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
pom.xml
*jar
lib
classes
13 changes: 13 additions & 0 deletions README
@@ -0,0 +1,13 @@
# binj

Erlany style Bin for Clojure

## Usage

ToDo

## License

Copyright (C) 2011 FIXME

Distributed under the Eclipse Public License, the same as Clojure.
4 changes: 4 additions & 0 deletions project.clj
@@ -0,0 +1,4 @@
(defproject binj "1.0.0-SNAPSHOT"
:description "FIXME: write"
:dependencies [[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]])
72 changes: 72 additions & 0 deletions src/binj/core.clj
@@ -0,0 +1,72 @@
(ns binj.core)



(defn << [& rest]
(let [rest
(if (and (= (count rest))
(string? (first rest)))
(map int (first rest))
rest)]
(flatten (map (fn [val]
(if (seq? val)
(apply decimal-to-bytelist val)
(bit-and val 0xff))) rest))))
(defn decimal-to-bytelist
([val] (decimal-to-bytelist val 8 1))
([val bits] (decimal-to-bytelist val bits 1))
([val bits unit]
(loop [bits (* bits unit)
val val
acc '()]
(if (<= bits 8)
(conj acc (bit-and val (dec (bit-shift-left 1 bits))))
(recur (- bits 8) (bit-shift-right val 8) (conj acc (bit-and val 0xff)))))))

(defn- reduce-bl [bl]
(reduce + (map #(* (bit-and %1 0xff) %2)
bl
(iterate #(bit-shift-left % 8) 1))))

(defn bytelist-to-decimal
([bytelist] (reduce-bl (reverse bytelist)))
([bytelist n] (reduce-bl (take n (reverse bytelist)))))

(defn destructure-binvec [bin-vec]
(let [bdg-pairs (partition 2 (first bin-vec))
bin (second bin-vec)]
(loop [bdgs (reverse bdg-pairs)
acc '()
bin (if (number? bin) bin
(bytelist-to-decimal bin))]
(if (empty? bdgs) acc
(let [bdg (first bdgs)
sym (first bdg)
val (second bdg)]
(recur (rest bdgs)
(conj (conj acc
(bit-and bin (dec (bit-shift-left 1 val))))
sym)
(bit-shift-right bin val)))))))

(defn foo [bindings]
(let [bdg-pair (partition 2 bindings)]
(loop [bdg bdg-pair
acc []]
(if (empty? bdg) acc
(let [b (first bdg)
bin (first b)
val (second b)]
(if (and (vector? bin)
(some number? bin))
(recur (rest bdg)
(into acc (destructure-binvec b)))
(recur
(rest bdg)
(conj (conj acc bin) val))))))))


(defmacro let-bin [bindings & body]
(let [bin (foo bindings)]
`(let [~@(destructure bin)] ~@body)))

6 changes: 6 additions & 0 deletions test/binj/test/core.clj
@@ -0,0 +1,6 @@
(ns binj.test.core
(:use [binj.core] :reload)
(:use [clojure.test]))

(deftest replace-me ;; FIXME: write
(is false "No tests have been written."))

0 comments on commit 87ccfb8

Please sign in to comment.