summaryrefslogtreecommitdiff
path: root/cljlib.fnl
diff options
context:
space:
mode:
authorAndrey Orst <andreyorst@gmail.com>2020-11-12 21:06:49 +0300
committerAndrey Orst <andreyorst@gmail.com>2020-11-12 21:06:49 +0300
commit8cb6c1a5741f3f71cae18df1eedcf327ff99e718 (patch)
tree5bbddc4dd496c9c9df112d5c5a8ce11638098e5d /cljlib.fnl
parenta1851986383148593ca85675d3dafd1e8517481a (diff)
feature(core): support strings in seq
Diffstat (limited to 'cljlib.fnl')
-rw-r--r--cljlib.fnl34
1 files changed, 22 insertions, 12 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'."