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 +++++++++++++++++++++++++++++++++++++++---- lib/string.fnl | 2 +- 2 files changed, 40 insertions(+), 5 deletions(-) (limited to 'lib') 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} diff --git a/lib/string.fnl b/lib/string.fnl index d82c658..9ac6edd 100644 --- a/lib/string.fnl +++ b/lib/string.fnl @@ -19,4 +19,4 @@ (.. (trim (luna.utf.sub str 1 len)) (or ellipsis "..."))) "")) -{: split : ends-with : truncate} +{: split : ends-with : trim : truncate} -- cgit v1.2.3