summaryrefslogtreecommitdiff
path: root/lib/array.fnl
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2024-10-10 11:51:21 +0600
committerunwox <me@unwox.com>2024-10-10 11:51:38 +0600
commitdfcdaab3a84389e610a57fa82ee0d3a216f4821d (patch)
treee0318abc43baaf83987381312a02c97ffe8b6f9b /lib/array.fnl
parentddc11ff1cc31f17ff46523a649917940357825b5 (diff)
allow to specify several tags for a product (instead of one category)
Diffstat (limited to 'lib/array.fnl')
-rw-r--r--lib/array.fnl43
1 files changed, 39 insertions, 4 deletions
diff --git a/lib/array.fnl b/lib/array.fnl
index 11e4d2a..9ed0d80 100644
--- a/lib/array.fnl
+++ b/lib/array.fnl
@@ -4,10 +4,45 @@
(local copy
(fn [a b]
(reduce
- (fn [_ v c]
- (table.insert c v)
- c)
+ (fn [_ v res]
+ (table.insert res v)
+ res)
b a)))
(copy a b))
-{: concat}
+(fn flip [a]
+ (collect [k v (pairs a)]
+ (values v k)))
+
+(fn diff [a b]
+ (local flip-b (flip b))
+ (reduce
+ (fn [_ v res]
+ (when (not (. flip-b v))
+ (table.insert res v))
+ res)
+ a []))
+
+(fn unique [a]
+ (var hash {})
+ (reduce
+ (fn [_ v res]
+ (when (not (. hash v))
+ (tset hash v true)
+ (table.insert res v))
+ res)
+ a []))
+
+(fn flatten [a]
+ (reduce
+ (fn [_ value rest]
+ (concat
+ rest
+ (if (= (type value) "table")
+ (flatten value)
+ [value])))
+ a []))
+
+(local join table.concat)
+
+{: concat : diff : unique : flatten : join}