Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andreas Kostler
committed
Apr 13, 2011
0 parents
commit 87ccfb8
Showing
5 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pom.xml | ||
*jar | ||
lib | ||
classes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.")) |