summaryrefslogtreecommitdiff
path: root/cljlib.fnl
diff options
context:
space:
mode:
authorAndrey Orst <andreyorst@gmail.com>2020-11-15 17:50:34 +0300
committerAndrey Orst <andreyorst@gmail.com>2020-11-15 17:50:34 +0300
commitaec65aee793ece4416d7f5f50fc3a326deba8917 (patch)
treeee7533a33cc33e00fdd994f0d852497078b1067b /cljlib.fnl
parent0c1e26ef51ccbc70039b7e16112ea38d8b4e6197 (diff)
fix(core): fix reduce-kv and tests
Diffstat (limited to 'cljlib.fnl')
-rw-r--r--cljlib.fnl29
1 files changed, 14 insertions, 15 deletions
diff --git a/cljlib.fnl b/cljlib.fnl
index e953675..98bb98e 100644
--- a/cljlib.fnl
+++ b/cljlib.fnl
@@ -285,6 +285,14 @@ Additionally you can use [`conj`](#conj) and [`apply`](#apply) with
:nil nil
_ (error (.. "expected table, string or nil") 2))))
+(fn* core.kvseq
+ "Transforms any table kind to key-value sequence."
+ [tbl]
+ (let [res (empty [])]
+ (each [k v (pairs tbl)]
+ (insert res [k v]))
+ res))
+
(fn* core.first
"Return first element of a table. Calls `seq` on its argument."
[col]
@@ -512,14 +520,13 @@ Reduce table by adding values from keys that start with letter `a`:
```"
[f val tbl]
(var res val)
- (each [_ [k v] (pairs (or (seq tbl) (empty [])))]
+ (each [_ [k v] (ipairs (or (kvseq tbl) (empty [])))]
(set res (f res k v))
- (when-some [reduced (when-some [m (getmetatable res)]
- (and m.cljlib/reduced
- (= m.cljlib/reduced.status :ready)
- m.cljlib/reduced.val))]
- (set res reduced)
- (lua :break)))
+ (match (getmetatable res)
+ m (if (and m.cljlib/reduced
+ (= m.cljlib/reduced.status :ready))
+ (do (set res m.cljlib/reduced.val)
+ (lua :break)))))
res)
(fn* core.mapv
@@ -614,14 +621,6 @@ Basic `zipmap` implementation:
(filter pred r)))
(empty [])))
-(fn* core.kvseq
- "Transforms any table kind to key-value sequence."
- [tbl]
- (let [res (empty [])]
- (each [k v (pairs tbl)]
- (insert res [k v]))
- res))
-
(fn* core.identity "Returns its argument." [x] x)
(fn* core.comp