framed.std.core

Utility functions to complement clojure.core

coll-wrap

(coll-wrap x-or-xs)
Wrap value in a vector if it is not sequential already
Ex:
  (coll-wrap 2)       ; => [2]
  (coll-wrap [1 2 3]) ; => [1 2 3]

find

(find pred coll)
Return the first value of x in coll that is logically true for (pred x)
Similar to clojure.core/some, but returns the item itself.

Ex: (find even? [1 1 1 3 4 5 6]) ; => 4

flip

(flip f)(flip f y x)
Takes two arguments in the reverse order of f ('flips' a function
of two arguments)
If supplied a function with no args, returns a new function
accepting the reversed args

Ex:
  (flip dissoc :foo {:foo 1 :bar 2})
  ; => {:bar 2}

  (def flipped-dissoc (flip dissoc))
  (flipped-dissoc :foo {:foo 1 :bar 2})
  ; => {:bar 2}

from-edn

(from-edn x)
Attempt to parse x as EDN, or return nil on failure

future-loop

macro

(future-loop & body)
Execute body repeatedly within a future, returning the future

map-from-keys

macro

(map-from-keys & forms)
Given symbols, e.g. `(map-from-keys foo bar)`,
return a map with those names as keyword keys, and those values:

Ex:
  (map-from-keys foo bar)
  ; => {:foo foo
        :bar bar}

map-kv

(map-kv val-fn coll)(map-kv key-fn val-fn coll)
Same as `map-tup` but returns results in a map

map-tup

(map-tup val-fn coll)(map-tup key-fn val-fn coll)
For all k,v in coll, return a seq of [(key-fn k) (val-fn v)] tuples

Ex:
  (map-tup #(* 2 %) #(* 3 %) {1 2, 3 4, 5 6})
  ; => ([2 6] [6 12] [10 18])

mapcat

(mapcat f coll)
Like clojure.core/mapcat over a single coll without object
reachability memory issues. This is especially useful when
large seqs are generated by f, but this is a general-purpose
replacement for its clojure.core counterpart.

See http://clojurian.blogspot.com/2012/11/beware-of-mapcat.html
    http://stackoverflow.com/questions/21943577/mapcat-breaking-the-lazyness

rand-alphanumeric

(rand-alphanumeric len)
Generate a string of random letters/digits of a given length

rand-int-between

(rand-int-between min-val max-val)
Generate a random int in the inclusive range of min-val to max-val

shuffle

(shuffle rng coll)
Same as clojure.core/shuffle but accepts source of randomness
for deterministic testing

to-edn

(to-edn x)
pr-str x only if it is truthy, else return nil

when-assoc

(when-assoc coll k v)
When v is truthy, assoc it into coll at k. Otherwise return coll

when-assoc-in

(when-assoc-in coll ks v)
When v is truthy, assoc it into coll at ks. Otherwise return coll

zip

(zip & colls)
Zip corresponding elements from two or more colls together
Ex:
  (zip [1 2] [3 4])
  ; => [[1 3] [2 4]]

zipmap-seq

(zipmap-seq key-fn val-fn coll)
Given a collection `coll`, return a map where for all k in coll,
key of entry is (f k) and value of entry is (g k)

Ex:
  (zipmap-seq #(* 2 %) (* 3 %) [1 2 3])
  ;=> {2 3, 4 6, 6 9}