Skip to content

Commit

Permalink
v1.2.0: Adding index-on
Browse files Browse the repository at this point in the history
  • Loading branch information
timmc committed Apr 5, 2012
1 parent 4738cc0 commit 2cf28f8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion 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"
Expand Down
13 changes: 13 additions & 0 deletions src/org/timmc/handy.clj
Expand Up @@ -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)])))
6 changes: 6 additions & 0 deletions test/org/timmc/test/handy.clj
Expand Up @@ -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.