diff options
Diffstat (limited to 'lib/array.fnl')
| -rw-r--r-- | lib/array.fnl | 43 |
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} |
