Permalink
Browse files

first working version

  • Loading branch information...
0 parents commit 84595f14a5d8ca2224a2bf86cf04fecf9139dcb5 @amalloy committed Oct 12, 2010
Showing with 60 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +33 −0 README
  3. +4 −0 project.clj
  4. +13 −0 src/bit_packer/core.clj
  5. +6 −0 test/bit_packer/test/core.clj
@@ -0,0 +1,4 @@
+pom.xml
+*jar
+lib
+classes
@@ -0,0 +1,33 @@
+# bit-packer
+
+pack: Encode a number into a lazy sequence of digits in base N, with
+the most significant digits last
+
+unpack: Perform the reverse operation, decoding a sequence of base-N
+digits (most significant digits last) into a single number
+
+Neither of these functions deals with actual characters: in base 16,
+the value 10 is the integer '10'. If you want to output in some
+particular format, that is left to you; this simply does the mod-N
+arithmetic for you.
+
+## Usage
+
+bit-packer.core> (pprint (map #(pack 100 %) (take 15 (drop 2 (range)))))
+((0 0 1 0 0 1 1)
+ (1 0 2 0 1)
+ (0 1 2 1)
+ (0 0 4)
+ (4 4 2)
+ (2 0 2)
+ (4 4 1)
+ (1 2 1)
+ (0 0 1)
+ (1 9)
+ (4 8)
+ (9 7)
+ (2 7)
+ (10 6)
+ (4 6))
+
+Distributed under the Eclipse Public License, the same as Clojure.
@@ -0,0 +1,4 @@
+(defproject bit-packer "1.0.0-SNAPSHOT"
+ :description "FIXME: write"
+ :dependencies [[org.clojure/clojure "1.2.0"]
+ [org.clojure/clojure-contrib "1.2.0"]])
@@ -0,0 +1,13 @@
+(ns bit-packer.core)
+
+(defn pack [num base]
+ (map second
+ (take-while (complement #(every? zero? %))
+ (rest (iterate (fn [[n]] ((juxt quot rem) n base)) [num 0])))))
+
+(defn unpack [[& nums] base]
+ (reduce +
+ 0
+ (map *
+ nums
+ (iterate #(* base %) 1))))
@@ -0,0 +1,6 @@
+(ns bit-packer.test.core
+ (:use [bit-packer.core] :reload)
+ (:use [clojure.test]))
+
+(deftest replace-me ;; FIXME: write
+ (is false "No tests have been written."))

0 comments on commit 84595f1

Please sign in to comment.