slack1
2018-7-24 23:41:51
(for/first ([n (in-naturals)]
            #:when (test? (fn n)))
           (fn n)
)

slack1
2018-7-24 23:42:02

If I have something like this, how do I not repeat the calculation of fn?


greg
2018-7-25 00:11:33

@slack1 If you switch to for*/first you can use in-value


zenspider
2018-7-25 00:11:40

@slack1 try (ormap test? ...)


greg
2018-7-25 00:11:40
#lang racket
(define fn? odd?)
(for*/first ([n (in-naturals)]
             [v (in-value (fn? n))]
             #:when v)
  (cons n v))

zenspider
2018-7-25 00:11:52

oooh! I didn’t know about that one tho


greg
2018-7-25 00:12:20

In this particular example ormap would be simpler for sure.


zenspider
2018-7-25 00:12:21

wait… SINGLE value?


zenspider
2018-7-25 00:12:36

that sounds like it won’t be calculated for eachn ?


zenspider
2018-7-25 00:13:07

just bad wording?


greg
2018-7-25 00:13:12

for* is the nested sequences form. in-value is a sequence of 1 that you can use just as a let-like binding.


greg
2018-7-25 00:14:22

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. ¯_(ツ)_/¯


greg
2018-7-25 00:15:54

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.


zenspider
2018-7-25 00:15:59

well fuck. stream-ormap just returns #t… why does it break contract w/ ormap?


zenspider
2018-7-25 00:16:29

yeah. the “sequence of 1” to me reads like a const stream


zenspider
2018-7-25 00:18:23

oooh. it isn’t breaking contract w/ ormap… I read it wrong


greg
2018-7-25 00:22:06

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.


slack1
2018-7-25 00:30:56

ah I see


slack1
2018-7-25 00:36:03

thanks!


notjack
2018-7-25 00:37:05

I’d like to just be able to put defines there


notjack
2018-7-25 00:37:43
#lang racket
(define fn? odd?)
(for/first ([n (in-naturals)]
            (define v (fn? n))
            #:when v)
 (cons n v))

slack1
2018-7-25 00:38:04

here I feel like classical recursion may look better -_-a


notjack
2018-7-25 00:40:05

I think it often does for simple stream operations. But many real-world stream ops aren’t simple.