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

Add index-of to core library.

This commit is contained in:
Calvin Rose 2020-08-02 13:47:56 -05:00
parent 1748e8510e
commit b564087db0

View File

@ -957,6 +957,18 @@
(def i (find-index pred ind))
(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
"Take first n elements in an indexed type. Returns new indexed instance."
[n ind]