for/stream and values

36 views
Skip to first unread message

Yury Bulka

unread,
May 2, 2020, 6:37:16 PM5/2/20
to racket...@googlegroups.com
Hello everyone,

I have a question about creating a steam that yields multiple values on
each iteration. Given something like:

(require racket/strema)

(define my-stream (for/stream ([i (in-range 1 10)])
(values "hi" i)))

When I'm trying to:

(let-values ([(a b) (stream-ref my-stream 0)])
(printf "~v: ~v" a b))

I'm getting:

; result arity mismatch;
; expected number of values not received
; expected: 1
; received: 2
; values...:
; "hi"
; 2
; Context:
; /nix/store/z4mpvw1p6whq454lmjshbdr6472sdbr3-racket-7.6/share/racket/collects/racket/repl.rkt:11:26

Similarly:

(for ([(a b) my-stream])
(printf "~v: ~v" a b))

leads to:

; result arity mismatch;
; expected number of values not received
; expected: 1
; received: 2
; values...:
; "hi"
; 1
; Context:
; tcp-accepted:1:1 for-loop
; top-level:1:1 [running body]
; /nix/store/z4mpvw1p6whq454lmjshbdr6472sdbr3-racket-7.6/share/racket/collects/racket/repl.rkt:11:26

Documentation on stream-ref specifically mentions that it can have
multiple values:
https://docs.racket-lang.org/reference/streams.html?q=stream-ref#%28def._%28%28lib._racket%2Fstream..rkt%29._stream-ref%29%29

What am I doing wrong here?

P.S. The context is that I'm trying to create a function that would
return results from an API as a stream that can be iterated with the for
family of forms, and would yield multiple values per iteration.

--
Yury Bulka
https://mamot.fr/@setthemfree
#NotOnFacebook

Yury Bulka

unread,
May 2, 2020, 6:38:46 PM5/2/20
to racket...@googlegroups.com
(sorry for the typos - obviously, it should be racket/stream)

--
Yury Bulka
https://mamot.fr/@setthemfree
#NotOnFacebook



Sorawee Porncharoenwase

unread,
May 2, 2020, 6:57:00 PM5/2/20
to Yury Bulka, Racket list
I just created a package that helps creating a stream of multiple values a couple of weeks ago. You might find it useful: https://docs.racket-lang.org/stream-values/

--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/87ees2nerz.fsf%40privacyrequired.com.

Yury Bulka

unread,
May 3, 2020, 5:06:06 AM5/3/20
to Sorawee Porncharoenwase, Racket list
Thank you for the package! So, do I understand correcty that it is
impossible to produce multi-valued streams with for/stream? If so, maybe
it's worth adding a note about it to the documentation of for/stream?

Also, I notice in the manual of your package that you mention that
generators are inefficient in the default implementation of
Racket. Since I'm using generators too, I'm curious about the
performance implications - where can I read more about it?

Best,
--
Yury Bulka
https://mamot.fr/@setthemfree
#NotOnFacebook



Sorawee Porncharoenwase

unread,
May 3, 2020, 10:12:49 AM5/3/20
to Yury Bulka, Racket list
Thank you for the package! So, do I understand correcty that it is
impossible to produce multi-valued streams with for/stream? If so, maybe
it's worth adding a note about it to the documentation of for/stream?

That is correct. Feel free to submit a PR to improve the documentation. You will need to edit this file: https://github.com/racket/racket/blob/master/pkgs/racket-doc/scribblings/reference/sequences.scrbl#L1186
 
Also, I notice in the manual of your package that you mention that
generators are inefficient in the default implementation of
Racket. Since I'm using generators too, I'm curious about the
performance implications - where can I read more about it?

I don't think it's written anywhere explicitly, but you can take a look at https://blog.racket-lang.org/2020/02/racket-on-chez-status.html in the section "Run-Time Performance Redux", which compares the performance of generators in Racket 3m vs Racket CS.

Yury Bulka

unread,
May 3, 2020, 2:07:12 PM5/3/20
to Sorawee Porncharoenwase, Racket list
Sorawee Porncharoenwase <sorawe...@gmail.com> writes:

>>
>> Thank you for the package! So, do I understand correcty that it is
>> impossible to produce multi-valued streams with for/stream? If so, maybe
>> it's worth adding a note about it to the documentation of for/stream?
>>
>
> That is correct. Feel free to submit a PR to improve the documentation. You
> will need to edit this file:
> https://github.com/racket/racket/blob/master/pkgs/racket-doc/scribblings/reference/sequences.scrbl#L1186

I have created a draft pull request:
https://github.com/racket/racket/pull/3147

>> Also, I notice in the manual of your package that you mention that
>> generators are inefficient in the default implementation of
>> Racket. Since I'm using generators too, I'm curious about the
>> performance implications - where can I read more about it?
>>
>
> I don't think it's written anywhere explicitly, but you can take a look at
> https://blog.racket-lang.org/2020/02/racket-on-chez-status.html in the
> section "Run-Time Performance Redux", which compares the performance of
> generators in Racket 3m vs Racket CS.

Oh, that's useful. Thank you!
Reply all
Reply to author
Forward
0 new messages