1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-17 02:40:00 +00:00

Merge pull request #1280 from primo-ppcg/lengthable

Add `lengthable?`
This commit is contained in:
Calvin Rose 2023-09-01 17:41:25 -05:00 committed by GitHub
commit 51c0cf97bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 36 deletions

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
@ -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)
(var i 0) (do
(eachk k x (def arr (array/new-filled (length x)))
(put arr i k) (var i 0)
(++ i)) (eachk k x
arr) (put arr i k)
(++ i))
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)
(var i 0) (do
(each v x (def arr (array/new-filled (length x)))
(put arr i v) (var i 0)
(++ i)) (each v x
arr) (put arr i v)
(++ i))
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)
(var i 0) (do
(eachp p x (def arr (array/new-filled (length x)))
(put arr i p) (var i 0)
(++ i)) (eachp p x
arr) (put arr i p)
(++ i))
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."

View File

@ -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),