mirror of
https://github.com/janet-lang/janet
synced 2025-05-03 07:54:14 +00:00
commit
51c0cf97bc
@ -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,21 +1455,21 @@
|
|||||||
`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]
|
||||||
|
(if (lengthable? t)
|
||||||
|
(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)
|
||||||
@ -1477,6 +1477,7 @@
|
|||||||
(each v t
|
(each v t
|
||||||
(put ret (-- n) v))
|
(put ret (-- n) v))
|
||||||
ret)
|
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
|
||||||
@ -1586,32 +1587,41 @@
|
|||||||
(defn keys
|
(defn keys
|
||||||
"Get the keys of an associative data structure."
|
"Get the keys of an associative data structure."
|
||||||
[x]
|
[x]
|
||||||
(def arr @[])
|
(if (lengthable? x)
|
||||||
|
(do
|
||||||
|
(def arr (array/new-filled (length x)))
|
||||||
(var i 0)
|
(var i 0)
|
||||||
(eachk k x
|
(eachk k x
|
||||||
(put arr i k)
|
(put arr i k)
|
||||||
(++ i))
|
(++ i))
|
||||||
arr)
|
arr)
|
||||||
|
(seq [k :keys x] k)))
|
||||||
|
|
||||||
(defn values
|
(defn values
|
||||||
"Get the values of an associative data structure."
|
"Get the values of an associative data structure."
|
||||||
[x]
|
[x]
|
||||||
(def arr @[])
|
(if (lengthable? x)
|
||||||
|
(do
|
||||||
|
(def arr (array/new-filled (length x)))
|
||||||
(var i 0)
|
(var i 0)
|
||||||
(each v x
|
(each v x
|
||||||
(put arr i v)
|
(put arr i v)
|
||||||
(++ i))
|
(++ i))
|
||||||
arr)
|
arr)
|
||||||
|
(seq [v :in x] v)))
|
||||||
|
|
||||||
(defn pairs
|
(defn pairs
|
||||||
"Get the key-value pairs of an associative data structure."
|
"Get the key-value pairs of an associative data structure."
|
||||||
[x]
|
[x]
|
||||||
(def arr @[])
|
(if (lengthable? x)
|
||||||
|
(do
|
||||||
|
(def arr (array/new-filled (length x)))
|
||||||
(var i 0)
|
(var i 0)
|
||||||
(eachp p x
|
(eachp p x
|
||||||
(put arr i p)
|
(put arr i p)
|
||||||
(++ i))
|
(++ i))
|
||||||
arr)
|
arr)
|
||||||
|
(seq [p :pairs x] p)))
|
||||||
|
|
||||||
(defn frequencies
|
(defn frequencies
|
||||||
"Get the number of occurrences of each value in an indexed data structure."
|
"Get the number of occurrences of each value in an indexed data structure."
|
||||||
|
@ -680,6 +680,13 @@ JANET_CORE_FN(janet_core_is_dictionary,
|
|||||||
return janet_wrap_boolean(janet_checktypes(argv[0], JANET_TFLAG_DICTIONARY));
|
return janet_wrap_boolean(janet_checktypes(argv[0], JANET_TFLAG_DICTIONARY));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JANET_CORE_FN(janet_core_is_lengthable,
|
||||||
|
"(lengthable? x)",
|
||||||
|
"Check if x is a bytes, indexed, or dictionary.") {
|
||||||
|
janet_fixarity(argc, 1);
|
||||||
|
return janet_wrap_boolean(janet_checktypes(argv[0], JANET_TFLAG_LENGTHABLE));
|
||||||
|
}
|
||||||
|
|
||||||
JANET_CORE_FN(janet_core_signal,
|
JANET_CORE_FN(janet_core_signal,
|
||||||
"(signal what x)",
|
"(signal what x)",
|
||||||
"Raise a signal with payload x. ") {
|
"Raise a signal with payload x. ") {
|
||||||
@ -1079,6 +1086,7 @@ static void janet_load_libs(JanetTable *env) {
|
|||||||
JANET_CORE_REG("bytes?", janet_core_is_bytes),
|
JANET_CORE_REG("bytes?", janet_core_is_bytes),
|
||||||
JANET_CORE_REG("indexed?", janet_core_is_indexed),
|
JANET_CORE_REG("indexed?", janet_core_is_indexed),
|
||||||
JANET_CORE_REG("dictionary?", janet_core_is_dictionary),
|
JANET_CORE_REG("dictionary?", janet_core_is_dictionary),
|
||||||
|
JANET_CORE_REG("lengthable?", janet_core_is_lengthable),
|
||||||
JANET_CORE_REG("slice", janet_core_slice),
|
JANET_CORE_REG("slice", janet_core_slice),
|
||||||
JANET_CORE_REG("range", janet_core_range),
|
JANET_CORE_REG("range", janet_core_range),
|
||||||
JANET_CORE_REG("signal", janet_core_signal),
|
JANET_CORE_REG("signal", janet_core_signal),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user