From 8cb6c1a5741f3f71cae18df1eedcf327ff99e718 Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Thu, 12 Nov 2020 21:06:49 +0300 Subject: feature(core): support strings in seq --- cljlib.fnl | 34 ++++++++++++++++++++++------------ tests/core.fnl | 7 +++++-- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/cljlib.fnl b/cljlib.fnl index f0b4d18..07e5621 100644 --- a/cljlib.fnl +++ b/cljlib.fnl @@ -147,18 +147,28 @@ Transforms original table to sequential table of key value pairs stored as sequential tables in linear time. If `tbl' is an associative table, returns `[[key1 value1] ... [keyN valueN]]' table. If `tbl' is sequential table, returns its shallow copy." - [tbl] - (when-some [_ (and tbl (next tbl))] - (var assoc? false) - (let [assoc (empty []) - seq (empty [])] - (each [k v (pairs tbl)] - (if (and (not assoc?) - (not (= (type k) :number))) - (set assoc? true)) - (insert assoc [k v]) - (tset seq k v)) - (if assoc? assoc seq)))) + [col] + (match (type col) + :table + (when-some [_ (and col (next col))] + (var assoc? false) + (let [assoc (empty []) + seq (empty [])] + (each [k v (pairs col)] + (if (and (not assoc?) + (not (= (type k) :number))) + (set assoc? true)) + (insert assoc [k v]) + (tset seq k v)) + (if assoc? assoc seq))) + :string + (let [res [] + char utf8.char] + (each [_ b (utf8.codes col)] + (insert res (char b))) + res) + :nil nil + _ (error "expected table or string" 2))) (macro safe-seq [tbl] "Create sequential table, or empty table if `seq' returned `nil'." diff --git a/tests/core.fnl b/tests/core.fnl index b0c02aa..1df3921 100644 --- a/tests/core.fnl +++ b/tests/core.fnl @@ -200,7 +200,9 @@ (assert-eq (seq {}) nil) (assert-eq (seq [1]) [1]) (assert-eq (seq [1 2 3]) [1 2 3]) - (assert-eq (seq {:a 1}) [["a" 1]])) + (assert-eq (seq {:a 1}) [["a" 1]]) + (assert-eq (seq "abc") ["a" "b" "c"]) + (assert-eq (seq "абв") ["а" "б" "в"])) (testing "mapv" (assert-not (pcall mapv)) @@ -224,7 +226,8 @@ ["Happy Days co." "Coffee With You"] ["secretary" "chief officer"]) ["Bob Smith works as secretary at Happy Days co." - "Alice Watson works as chief officer at Coffee With You"])) + "Alice Watson works as chief officer at Coffee With You"]) + (assert-eq (table.concat (mapv string.upper "vaiv")) "VAIV")) (testing "reduce" (fn* add -- cgit v1.2.3