potpuri.core

assoc-first

added in 0.2.1

(assoc-first coll where v)

Finds the first element in collection matching where parameter and replaces that with v.

Implementation depends on collection type.

assoc-if

added in 0.2.0

(assoc-if m key val)(assoc-if m key val & kvs)

Assoc key-value pairs with non-nil values into map.

assoc-in-path-vals

added in 0.1.0

(assoc-in-path-vals c)

Re-created a map from it’s path-vals extracted with (path-vals).

build-tree

added in 0.5.0

(build-tree {:keys [parent-fn id-fn assoc-children-fn], :as opts} items)

Builds a tree from given items collections.

ID is what is used to match parents and children. Root items are those where parent-fn returns nil.

Options:

  • :parent-fn (required) Used to create a map from ID => children
  • :id-fn (required) Used to get the ID from an item
  • :assoc-children-fn (required) Attach the children to an item
  • :item-fn (optional) Called for each item, after children has been attached to the item
  • :children-fn (optional) Called for each children collection

Example: (build-tree {:id-fn :id, :parent-fn :parent, :assoc-children-fn #(assoc %1 :children %2)} [{:id 1} {:id 2 :parent 1} {:id 3 :parent 1}]) => [{:id 1 :children [{:id 2} {:id 3}]}]

Check test file for more examples.

condas->

macro

added in 0.3.0

(condas-> expr name & clauses)

A mixture of cond-> and as-> allowing more flexibility in the test and step forms

conjv

added in 0.2.0

(conjv coll el)

Append an element to a collection. If collection is nil, creates vector instead of sequence. The appending might happen on different places depending on the type of collection.

Examples:

(conjv nil 5) => [5]
(conjv [1] 2) => [1 2]
(update-in {} [:a] conjv 5) => {:a [5]}
(-> [] (conjv 5)) => [5]

consv

added in 0.2.0

(consv coll el)

Prepend an element to a collection. Returns a vector.

Examples:

(consv nil 1) => [1]
(consv [2] 1) => [1 2]
(update-in {:a 2} [:a] consv 1) => {:a [1 2]}
(-> [2] (consv 5)) => [1 2]

deep-merge

added in 0.2.0

(deep-merge strategy & values)(deep-merge values)

Recursively merges maps.

If the first parameter is a keyword it tells the strategy to use when merging non-map collections. Options are

  • :replace, the default, the last value is used
  • :into, if the value in every map is a collection they are concatenated using into. Thus the type of (first) value is maintained.

Examples:

(deep-merge {:a {:c 2}} {:a {:b 1}}) => {:a {:b 1 :c 2}}
(deep-merge :replace {:a [1]} {:a [2]}) => {:a [2]}
(deep-merge :into {:a [1]} {:a [2]}) => {:a [1 2]}
(deep-merge {:a 1} nil) => nil

See also: meta-merge.

dissoc-in

added in 0.1.0

(dissoc-in m [k & ks :as keys])

Dissociates an entry from a nested associative structure returning a new nested structure. keys is a sequence of keys. Any empty maps that result will not be present in the new structure.

filter-entries

added in 0.5.1

(filter-entries pred coll)

Filter given associative collection using function on the values.

filter-keys

added in 0.2.2

(filter-keys pred coll)

Filter given associative collection using function on the keys.

filter-vals

added in 0.2.2

(filter-vals pred coll)

Filter given associative collection using function on the values.

find-first

added in 0.2.0

(find-first coll where)

Find first value from collection which mathes the where parameter.

If where parameter is:

  • a fn, it’s used as predicate as is
  • a map, a predicate is created which checks if value in collection has same values for each key in where map
  • Something which implements IFn, e.g. keywords and sets, is used as is
  • any value, a predicate is created which checks if value is identitical

Examples:

(find-first [1 2 3] even?) => 2
(find-first [{:id 1} {:id 2, :foo :bar}] {:id 2}) => {:id 2, :foo :bar}
(find-first [{:a 1} {:b 2, :foo :bar}] :b) => {:b 2, :foo :bar}
(find-first [1 2 3] #{3}) => 3
(find-first [1 2 3] 3) => 3
(-> [1 2 3] (find-first odd?)) => 1

find-index

added in 0.2.0

(find-index coll where)

Find index of vector which matches the where parameter.

If where parameter is:

  • a fn, it’s used as predicate as is
  • a map, a predicate is created which checks if value in collection has same values for each key in where map
  • Something which implements IFn, e.g. keywords and sets, is used as is
  • any value, a predicate is created which checks if value is identitical

Examples:

(find-index [1 2 3] even?) => 1
(find-index [{:id 1} {:id 2}] {:id 2}) => 1
(find-index [{:a 1} {:b 2}] :b) => 1
(find-index [1 2 3] #{3}) => 2
(find-index [1 2 3] 3) => 2
(-> [1 2 3] (find-index odd?)) => 0

fn->

macro

added in 0.1.0

(fn-> & body)

Creates a function that threads on input with some->

fn->>

macro

added in 0.1.0

(fn->> & body)

Creates a function that threads on input with some->>

if-all-let

macro

added in 0.2.3

(if-all-let bindings then)(if-all-let bindings then else)

bindings => [binding-form test, binding-form test ...]

If all tests are true, evaluates then with binding-forms bound to the values of tests, if not, yields else.

index-by

added in 0.3.0

(index-by f coll)

Returns a map of the elements of coll keyed by the result of f on each element. The value at each key will the last item for given f result.

map-entries

added in 0.5.1

(map-entries f coll)

Map the entries of given associative collection using function.

map-keys

added in 0.2.0

(map-keys f coll)

Map the keys of given associative collection using function.

map-of

macro

added in 0.1.0

(map-of & syms)

Creates map with symbol names as keywords as keys and symbol values as values.

map-vals

added in 0.2.0

(map-vals f coll)

Map the values of given associative collection using function.

path-vals

added in 0.1.0

(path-vals m)

Returns vector of tuples containing path vector to the value and the value.

remove-entries

added in 0.5.1

(remove-entries pred coll)

Removes given associative collection using function on the values.

remove-keys

added in 0.5.0

(remove-keys pred coll)

Removes given associative collection using function on the keys.

remove-vals

added in 0.5.0

(remove-vals pred coll)

Removes given associative collection using function on the values.

update-first

added in 0.2.1

(update-first coll where f & args)

Finds the first element in collection matching the where parameter and updates that using f. f is called with current value and rest of update-first params.

Implementation depends on collection type.

wrap-into

added in 0.2.0

(wrap-into coll v)

Wrap non-collection values into given collection. Collections are only put into the collection (non-wrapped).

Examples:

(wrap-into [] :a) => [:a]
(wrap-into [] [:a]) => [:a]
(wrap-into #{} [:a]) => #{:a}

zip

added in 0.4.0

(zip & colls)

Returns a sequence of vectors where the i-th vector contains the i-th element from each of the argument collections. The returned sequence is as long as the shortest argument.

Example:

(zip [1 2 3] [:a :b :c])  => ([1 :a] [2 :b] [3 :c])
(zip [1] [1 2] [1 2 3])   => ([1 1 1])