mirror of
https://github.com/janet-lang/janet
synced 2025-01-25 14:46:52 +00:00
Remove :generate verb from loop.
Instead, one case use `:in` as with otehr data structures.
This commit is contained in:
parent
e5242c67ff
commit
87badc71d2
@ -2,6 +2,10 @@
|
|||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
## Unreleased - ???
|
## Unreleased - ???
|
||||||
|
- Add `fiber/last-value` to get the value that was last yielded, errored, or signaled
|
||||||
|
by a fiber.
|
||||||
|
- Remove `:generate` verb from `loop` macros. Instead, use the `:in` verb
|
||||||
|
which will now work on fibers as well as other data structures.
|
||||||
- Define `next`, `get`, and `in` for fibers. This lets
|
- Define `next`, `get`, and `in` for fibers. This lets
|
||||||
`each`, `map`, and similar iteration macros can now iterate over fibers.
|
`each`, `map`, and similar iteration macros can now iterate over fibers.
|
||||||
- Remove macro `eachy`, which can be replaced by `each`.
|
- Remove macro `eachy`, which can be replaced by `each`.
|
||||||
|
@ -442,17 +442,6 @@
|
|||||||
(def ,binding ,i)
|
(def ,binding ,i)
|
||||||
,body))))
|
,body))))
|
||||||
|
|
||||||
(defn- loop-fiber-template
|
|
||||||
[binding expr body]
|
|
||||||
(with-syms [f s]
|
|
||||||
(def ds (if (idempotent? binding) binding (gensym)))
|
|
||||||
~(let [,f ,expr]
|
|
||||||
(while true
|
|
||||||
(def ,ds (,resume ,f))
|
|
||||||
(if (= :dead (,fiber/status ,f)) (break))
|
|
||||||
,;(if (= ds binding) [] [~(def ,binding ,ds)])
|
|
||||||
,;body))))
|
|
||||||
|
|
||||||
(defn- loop1
|
(defn- loop1
|
||||||
[body head i]
|
[body head i]
|
||||||
|
|
||||||
@ -490,7 +479,6 @@
|
|||||||
:pairs (each-template binding object :pairs [rest])
|
:pairs (each-template binding object :pairs [rest])
|
||||||
:in (each-template binding object :each [rest])
|
:in (each-template binding object :each [rest])
|
||||||
:iterate (iterate-template binding object rest)
|
:iterate (iterate-template binding object rest)
|
||||||
:generate (loop-fiber-template binding object [rest])
|
|
||||||
(error (string "unexpected loop verb " verb)))))
|
(error (string "unexpected loop verb " verb)))))
|
||||||
|
|
||||||
(defmacro forv
|
(defmacro forv
|
||||||
@ -564,9 +552,6 @@
|
|||||||
|
|
||||||
* :in -- iterate over the values in a data structure or fiber.
|
* :in -- iterate over the values in a data structure or fiber.
|
||||||
|
|
||||||
* :generate -- iterate over values yielded from a fiber. Can be paired with
|
|
||||||
the generator function for the producer/consumer pattern.
|
|
||||||
|
|
||||||
`loop` also accepts conditionals to refine the looping further. Conditionals are of
|
`loop` also accepts conditionals to refine the looping further. Conditionals are of
|
||||||
the form:
|
the form:
|
||||||
|
|
||||||
@ -614,7 +599,7 @@
|
|||||||
(undef _env)
|
(undef _env)
|
||||||
|
|
||||||
(undef loop1 check-indexed for-template for-var-template iterate-template
|
(undef loop1 check-indexed for-template for-var-template iterate-template
|
||||||
each-template range-template loop-fiber-template)
|
each-template range-template)
|
||||||
|
|
||||||
(defn sum
|
(defn sum
|
||||||
"Returns the sum of xs. If xs is empty, returns 0."
|
"Returns the sum of xs. If xs is empty, returns 0."
|
||||||
|
@ -49,7 +49,7 @@
|
|||||||
# Generators
|
# Generators
|
||||||
(def gen (generate [x :range [0 100] :when (pos? (% x 4))] x))
|
(def gen (generate [x :range [0 100] :when (pos? (% x 4))] x))
|
||||||
(var gencount 0)
|
(var gencount 0)
|
||||||
(loop [x :generate gen]
|
(loop [x :in gen]
|
||||||
(++ gencount)
|
(++ gencount)
|
||||||
(assert (pos? (% x 4)) "generate in loop"))
|
(assert (pos? (% x 4)) "generate in loop"))
|
||||||
(assert (= gencount 75) "generate loop count")
|
(assert (= gencount 75) "generate loop count")
|
||||||
|
@ -309,7 +309,7 @@ neldb\0\0\0\xD8\x05printG\x01\0\xDE\xDE\xDE'\x03\0marshal_tes/\x02
|
|||||||
# Issue 428
|
# Issue 428
|
||||||
(var result nil)
|
(var result nil)
|
||||||
(defn f [] (yield {:a :ok}))
|
(defn f [] (yield {:a :ok}))
|
||||||
(assert-no-error "issue 428 1" (loop [{:a x} :generate (fiber/new f)] (set result x)))
|
(assert-no-error "issue 428 1" (loop [{:a x} :in (fiber/new f)] (set result x)))
|
||||||
(assert (= result :ok) "issue 428 2")
|
(assert (= result :ok) "issue 428 2")
|
||||||
|
|
||||||
# Inline 3 argument get
|
# Inline 3 argument get
|
||||||
|
Loading…
Reference in New Issue
Block a user