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:
parent
70a467d469
commit
d9605c2856
@ -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))
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user