summaryrefslogtreecommitdiff
path: root/core.fnl
diff options
context:
space:
mode:
authorAndrey Orst <andreyorst@gmail.com>2020-10-26 22:51:21 +0300
committerAndrey Orst <andreyorst@gmail.com>2020-10-26 22:51:21 +0300
commit89db88c88083aaabba412662d9da10102f519b8d (patch)
treeac05da989c01aafca4c9a9167cc23754e0adff22 /core.fnl
parenteb5ac9685626493847340274a97a0e99c0231e7b (diff)
feature(core): add assoc function & test
Diffstat (limited to 'core.fnl')
-rw-r--r--core.fnl17
1 files changed, 16 insertions, 1 deletions
diff --git a/core.fnl b/core.fnl
index fc072ed..0be0a48 100644
--- a/core.fnl
+++ b/core.fnl
@@ -353,6 +353,21 @@ oppisite truth value."
(fn inc [x] (+ x 1))
(fn dec [x] (- x 1))
+
+(fn* assoc
+ "Associate key `k' with value `v' in associative `tbl'."
+ ([tbl k v] (doto tbl (tset k v)))
+ ([tbl k v & kvs]
+ (tset tbl k v)
+ (assert (zero? (% (length kvs) 2)) "expected even amount key-value args")
+ (var [i k v] [1 nil nil])
+ (var (i k) (next kvs))
+ (while i
+ (set (i v) (next kvs i))
+ (tset tbl k v)
+ (set (i k) (next kvs i)))
+ tbl))
+
{: apply
: seq
: first
@@ -390,4 +405,4 @@ oppisite truth value."
: reverse
: inc
: dec
- }
+ : assoc}