about summary refs log tree commit diff stats
path: root/fnl/play.fnl
blob: ca84e31ee8b72d31a74e2c6335f0678a817f573b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
(local lume (require :lume))

(local kv {:abc 123 :def 456 :xyz 789})
(local seq [123 456 789])

(fn even? [x] (= (% x 2) 0))

;; map a function over a table. note that the table is the first arg.
(lume.map seq (fn [x] (* 3 x))) ; -> [369 1368 2367]

;; keep or drop elements from a table based on a predicate function.
(lume.filter seq even?) ; -> [456]
(lume.reject seq even?) ; -> [123 789]

;; the classic reduce, aka foldl; run a function over everything in
;; the table with an accumulator value.
(lume.reduce seq (fn [acc x] (+ acc x))) ; -> 1368

;; return a new table with all the keys and values from any number of tables.
;; right-most arguments take precedence.
(lume.merge seq kv {:abc 963})
;; -> { :abc 963 :def 456 :xyz 789 1 123 2 456 3 789 }

;; unlike most of the other functions here, the extend function
;; modifies its first argument rather than returning a fresh table:
(local newtable {:qrs 147 :tuv 258})
(lume.extend newtable {:jkl 999})
newtable ; -> { :jkl 999 :qrs 147 :tuv 258 }

;; return all the keys in a table:
(lume.keys kv) ; -> ["xyz" "def" "abc"]

;; return the key under which a given value is first found in a table:
(lume.find seq 456) ; -> 2
(lume.find kv 789) ; "xyz"

;; similar to find, except use a predicate function instead of a value.
;; returns both the value matched and its index:
(lume.match seq even?) ; -> 456 2

;; select a subset of keys from a table:
(lume.pick kv :abc :xyz) ; -> { :abc 123 :xyz 789 }

;; finally we have a handful of miscellaneous non-table-related functions.

;; oddly Lua does not ship with a string splitting function; use this instead:
(lume.split "diode,capacitor,switch" ",") ; -> ["diode" "capacitor" "switch"]

;; generate a QUID and return a string representation:
(lume.uuid) ; -> "d6cce35c-487a-458f-aab2-9032c2621f38"