1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-17 14:14:49 +00:00

Allow iterating over generators with pairs, keys, and values.

This commit is contained in:
Calvin Rose 2023-08-22 19:24:44 -05:00
parent 70a467d469
commit d9605c2856
3 changed files with 12 additions and 6 deletions

View File

@ -1,10 +1,11 @@
(defn action []
(print "Handled SIGHUP!")
(flush))
(print "cleanup")
(os/exit 1))
(defn main [_]
# Set the interrupt-interpreter argument to `true` to allow
# interrupting the busy loop `(forever)`. By default, will not
# interrupt the interpreter.
(os/sigaction :hup action true)
(os/sigaction :term action true)
(os/sigaction :int action true)
(forever))

View File

@ -1584,7 +1584,7 @@
(defn keys
"Get the keys of an associative data structure."
[x]
(def arr (array/new-filled (length x)))
(def arr @[])
(var i 0)
(eachk k x
(put arr i k)
@ -1594,7 +1594,7 @@
(defn values
"Get the values of an associative data structure."
[x]
(def arr (array/new-filled (length x)))
(def arr @[])
(var i 0)
(each v x
(put arr i v)
@ -1604,7 +1604,7 @@
(defn pairs
"Get the key-value pairs of an associative data structure."
[x]
(def arr (array/new-filled (length x)))
(def arr @[])
(var i 0)
(eachp p x
(put arr i p)

View File

@ -172,5 +172,10 @@
(assert (= (length (range -10)) 0) "(range -10)")
(assert (= (length (range 1 10)) 9) "(range 1 10)")
# iterating over generator
(assert-no-error "iterate over coro 1" (values (generate [x :range [0 10]] x)))
(assert-no-error "iterate over coro 2" (keys (generate [x :range [0 10]] x)))
(assert-no-error "iterate over coro 3" (pairs (generate [x :range [0 10]] x)))
(end-suite)