1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 01:37:19 +00:00

update interleave, interpose

This commit is contained in:
primo-ppcg 2023-09-02 18:27:34 +07:00
parent 51c0cf97bc
commit 9cf674cdcb

View File

@ -966,7 +966,6 @@
1 (map-n 1 ,maptype ,res ,f ,ind ,inds) 1 (map-n 1 ,maptype ,res ,f ,ind ,inds)
2 (map-n 2 ,maptype ,res ,f ,ind ,inds) 2 (map-n 2 ,maptype ,res ,f ,ind ,inds)
3 (map-n 3 ,maptype ,res ,f ,ind ,inds) 3 (map-n 3 ,maptype ,res ,f ,ind ,inds)
4 (map-n 4 ,maptype ,res ,f ,ind ,inds)
(do (do
(def iter-keys (array/new-filled ninds)) (def iter-keys (array/new-filled ninds))
(def call-buffer (array/new-filled ninds)) (def call-buffer (array/new-filled ninds))
@ -1666,14 +1665,7 @@
(defn interleave (defn interleave
"Returns an array of the first elements of each col, then the second elements, etc." "Returns an array of the first elements of each col, then the second elements, etc."
[& cols] [& cols]
(def res @[]) (mapcat tuple ;cols))
(def ncol (length cols))
(when (> ncol 0)
(def len (min ;(map length cols)))
(loop [i :range [0 len]
ci :range [0 ncol]]
(array/push res (in (in cols ci) i))))
res)
(defn distinct (defn distinct
"Returns an array of the deduplicated values in `xs`." "Returns an array of the deduplicated values in `xs`."
@ -1720,14 +1712,23 @@
``Returns a sequence of the elements of `ind` separated by ``Returns a sequence of the elements of `ind` separated by
`sep`. Returns a new array.`` `sep`. Returns a new array.``
[sep ind] [sep ind]
(def len (length ind)) (var k (next ind nil))
(def ret (array/new (- (* 2 len) 1))) (if (not= nil k)
(if (> len 0) (put ret 0 (in ind 0))) (if (lengthable? ind)
(var i 1) (do
(while (< i len) (def ret (array/new-filled (- (* 2 (length ind)) 1) sep))
(array/push ret sep (in ind i)) (var i 0)
(++ i)) (while (not= nil k)
(put ret i (in ind k))
(set k (next ind k))
(+= i 2))
ret) ret)
(do
(def ret @[(in ind k)])
(while (not= nil (set k (next ind k)))
(array/push ret sep (in ind k)))
ret))
@[]))
(defn partition (defn partition
``Partition an indexed data structure `ind` into tuples ``Partition an indexed data structure `ind` into tuples