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

allow reverse on non-lengthable

This commit is contained in:
primo-ppcg 2023-09-01 15:20:33 +07:00
parent 645109048b
commit 4cb1f616c5

View File

@ -1435,7 +1435,7 @@
(defn every? (defn every?
``Evaluates to the last element of `ind` if all preceding elements are truthy, ``Evaluates to the last element of `ind` if all preceding elements are truthy,
otherwise evaluates to the first falsey argument.`` otherwise evaluates to the first falsey element.``
[ind] [ind]
(var res true) (var res true)
(loop [x :in ind :while res] (loop [x :in ind :while res]
@ -1455,28 +1455,29 @@
`Reverses the order of the elements in a given array or buffer and returns it `Reverses the order of the elements in a given array or buffer and returns it
mutated.` mutated.`
[t] [t]
(def len-1 (- (length t) 1)) (var i 0)
(def half (/ len-1 2)) (var j (length t))
(forv i 0 half (while (< i (-- j))
(def j (- len-1 i)) (def ti (in t i))
(def l (in t i)) (put t i (in t j))
(def r (in t j)) (put t j ti)
(put t i r) (++ i))
(put t j l))
t) t)
(defn reverse (defn reverse
`Reverses the order of the elements in a given array or tuple and returns `Reverses the order of the elements in a given array or tuple and returns
a new array. If a string or buffer is provided, returns an array of its a new array. If a string or buffer is provided, returns a buffer instead.`
byte values, reversed.`
[t] [t]
(var n (length t)) (if (lengthable? t)
(def ret (if (bytes? t) (do
(buffer/new-filled n) (var n (length t))
(array/new-filled n))) (def ret (if (bytes? t)
(each v t (buffer/new-filled n)
(put ret (-- n) v)) (array/new-filled n)))
ret) (each v t
(put ret (-- n) v))
ret)
(reverse! (seq [v :in t] v))))
(defn invert (defn invert
``Given an associative data structure `ds`, returns a new table where the ``Given an associative data structure `ds`, returns a new table where the