(for/first ([n (in-naturals)]
#:when (test? (fn n)))
(fn n)
)
If I have something like this, how do I not repeat the calculation of fn
?
@slack1 If you switch to for*/first
you can use in-value
@slack1 try (ormap test? ...)
#lang racket
(define fn? odd?)
(for*/first ([n (in-naturals)]
[v (in-value (fn? n))]
#:when v)
(cons n v))
oooh! I didn’t know about that one tho
In this particular example ormap
would be simpler for sure.
wait… SINGLE value?
that sounds like it won’t be calculated for eachn
?
just bad wording?
for*
is the nested sequences form. in-value
is a sequence of 1 that you can use just as a let
-like binding.
I thought it was a little weird when I first learned about it. But when I need it I just kind of reach for it automatically now. ¯_(ツ)_/¯
https://docs.racket-lang.org/reference/sequences.html#(def._((lib._racket%2Fprivate%2Fbase..rkt)._in-value)) > Returns a sequence that produces a single value: v
. This form is mostly useful for let
-like bindings in forms such as for*/list
.
well fuck. stream-ormap
just returns #t
… why does it break contract w/ ormap
?
yeah. the “sequence of 1” to me reads like a const stream
oooh. it isn’t breaking contract w/ ormap
… I read it wrong
A #:let [id rhs]
binding that works with plain for
& and friends, seems like it would be better ergonomics? But I haven’t given it a lot of thought why that might or might not be a good idea. And using the sequence-of-one “trick” lets for
be simpler to implement I guess.
ah I see
thanks!
I’d like to just be able to put defines
there
#lang racket
(define fn? odd?)
(for/first ([n (in-naturals)]
(define v (fn? n))
#:when v)
(cons n v))
here I feel like classical recursion may look better -_-a
I think it often does for simple stream operations. But many real-world stream ops aren’t simple.