mirror of
https://github.com/janet-lang/janet
synced 2025-04-27 05:03:14 +00:00
Add index-of to core library.
This commit is contained in:
parent
1748e8510e
commit
b564087db0
@ -957,6 +957,18 @@
|
|||||||
(def i (find-index pred ind))
|
(def i (find-index pred ind))
|
||||||
(if (= i nil) nil (in ind i)))
|
(if (= i nil) nil (in ind i)))
|
||||||
|
|
||||||
|
(defn index-of
|
||||||
|
"Find the first key associated with a value x in a data structure, acting like a reverse lookup.
|
||||||
|
Will not look at table prototypes.
|
||||||
|
Returns dflt if not found."
|
||||||
|
[x ind &opt dflt]
|
||||||
|
(var k (next ind nil))
|
||||||
|
(var ret dflt)
|
||||||
|
(while (not= nil k)
|
||||||
|
(when (= (in ind k) x) (set ret k) (break))
|
||||||
|
(set k (next ind k)))
|
||||||
|
ret)
|
||||||
|
|
||||||
(defn take
|
(defn take
|
||||||
"Take first n elements in an indexed type. Returns new indexed instance."
|
"Take first n elements in an indexed type. Returns new indexed instance."
|
||||||
[n ind]
|
[n ind]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user