1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-16 02:09:56 +00:00

Merge pull request #1281 from primo-ppcg/interleave-interpose

Update `interleave`, `interpose`
This commit is contained in:
Calvin Rose 2023-09-04 10:27:27 -05:00 committed by GitHub
commit a13aeaf955
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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))
@ -1472,8 +1471,8 @@
(do (do
(var n (length t)) (var n (length t))
(def ret (if (bytes? t) (def ret (if (bytes? t)
(buffer/new-filled n) (buffer/new-filled n)
(array/new-filled n))) (array/new-filled n)))
(each v t (each v t
(put ret (-- n) v)) (put ret (-- n) v))
ret) ret)
@ -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)
ret) (put ret i (in ind k))
(set k (next ind k))
(+= i 2))
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
@ -2893,8 +2894,8 @@
(try (try
(printf pf x) (printf pf x)
([e] ([e]
(eprintf "bad pretty format %v: %v" pf e) (eprintf "bad pretty format %v: %v" pf e)
(eflush))) (eflush)))
(flush)) (flush))
(do (do
(debug/stacktrace f x "") (debug/stacktrace f x "")