Last active
January 13, 2016 17:33
-
-
Save danielytics/a8e3d358d389773b36bf to your computer and use it in GitHub Desktop.
Useful functions for retrieving items from a list
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
user=> (def items [{:id 1 :data "hi"} | |
{:id 2 :data "ho"} | |
{:id 3 :data "test"}]) | |
#'user/items | |
user=> (first-where items :id = 1) | |
{:data "hi", :id 1} | |
user=> (first-where items :id > 1) ; will return only the first matching item | |
{:data "ho", :id 2} | |
user=> (get-where items :id > 1) ; will return all matching items | |
[{:data "ho", :id 2} | |
{:data "test", :id 3}] |
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
; Some functions for working with vectors of maps. | |
(defn first-where | |
"Get the first map from a list of maps where the criteria is true. | |
The criteria is specified through f, op and key. The element returned | |
is the element for which (op (f elem) key) returns truthy." | |
[seq f op key] | |
(some #(when (op (f %) key) %) seq)) | |
(defn get-where | |
"Get all maps from a list of maps where the criteria is true. | |
The criteria is specified through f, op and key. The elements returned | |
are the elements for which (op (f elem) key) returns truthy." | |
[seq f op key] | |
(filterv #(op (f %) key) seq)) | |
(defn update-where | |
"Update a vector of maps by applying func to each map where criteria is true. | |
The criteria is specified through f, op and key. The elements updated are | |
the elements for which (op (f elem) key) returns truthy." | |
[seq f op key func & args] | |
(mapv | |
(fn [elem] | |
(if (op (f elem) key) | |
(apply func elem args) | |
elem)) | |
seq)) | |
(defn transform-where | |
"Get and transform all maps from a vector of maps where the criteria is true. | |
The criteria is specified through f, op and key. The elements for which | |
(op (f elem) key) returns truthy are then passed through xform and returned." | |
[seq f op key xform & args] | |
(mapv | |
#(xform % args) | |
(get-where f op key seq))) |
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
(ns erinite.data.vom | |
"Data access and transformation for vector-of-maps data") | |
(defn criteria-fn | |
"Creates a function to test the specified criteria on the supplied input" | |
[key op & args] | |
(fn [item] | |
(apply op (key item) args))) | |
(defn select | |
"Select all maps where the specified criteria is true" | |
[vom & criteria] | |
(filter | |
(apply criteria-fn criteria) | |
vom)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment