From dfcdaab3a84389e610a57fa82ee0d3a216f4821d Mon Sep 17 00:00:00 2001 From: unwox Date: Thu, 10 Oct 2024 11:51:21 +0600 Subject: allow to specify several tags for a product (instead of one category) --- lib/array.fnl | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) (limited to 'lib/array.fnl') 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} -- cgit v1.2.3