Permalink
Browse files

v1.2.0: Adding index-on

  • Loading branch information...
1 parent 4738cc0 commit 2cf28f8964b76a32f92c4e941583c3f151ae9eaf @timmc committed Apr 5, 2012
Showing with 23 additions and 1 deletion.
  1. +3 −0 README.md
  2. +1 −1 project.clj
  3. +13 −0 src/org/timmc/handy.clj
  4. +6 −0 test/org/timmc/test/handy.clj
View
3 README.md
@@ -30,6 +30,9 @@ Built with Leiningen 1.x
### v1.1.0
* Add `with-temp-ns`, a sandboxing macro (may move to a different ns in future)
+### v1.2.0
+* Add `index-on`, a generalization of group-by
+
## License
Copyright (C) 2012 Tim McCormack
View
2 project.clj
@@ -1,4 +1,4 @@
-(defproject org.timmc/handy "1.1.0"
+(defproject org.timmc/handy "1.2.0"
:description "Common utilities to fill in the gaps"
:url "https://github.com/timmc/handy"
:license {:name "Eclipse Public License - v1.0"
View
13 src/org/timmc/handy.clj
@@ -67,3 +67,16 @@ logical true/false."
(finally
(in-ns '~old-ns)
(remove-ns '~tmp-ns))))))
+
+;;;; Structural manipulation
+
+(defn ^{:since "1.2.0"} index-on
+ "From a table (coll of record maps) produce a map of index key values
+to projections on the other keys. r->k is a function of a record to some
+key value, e.g. #(get % 5) or (juxt :a :b) or just :c.
+
+Example: (index-on [{:a 0, :b 1, :c 2}, {:a 3, :b 4, :c 5}] :a [:b])
+ => {0 {:b 1}, 3 {:b 4}}"
+ [table r->k keep-keys]
+ (into {} (for [record table]
+ [(r->k record) (select-keys record keep-keys)])))
View
6 test/org/timmc/test/handy.clj
@@ -103,3 +103,9 @@
(def a (org.timmc.test.handy/resolver Shape))]
(+ 2 3))
5)))
+
+;;;; Structural
+
+(deftest indexing
+ (is (= (index-on [{:a 0, :b 1, :c 2}, {:a 3, :b 4, :c 5}] (juxt :a :b) [:c])
+ {[0 1] {:c 2}, [3 4] {:c 5}})))

0 comments on commit 2cf28f8

Please sign in to comment.