My “collections” package makes fairly heavy use of streams, so it provides a number of utilities to help with what you want to do. The obvious function here would be `generate-sequence`[1], which creates a stream from a generator. You can call `take` on the resulting sequence, since my generic implementation of `take` works on any collection type.
There are other ways to accomplish what you’re looking for in data/collection that don’t use generators. For example, you could use `for/sequence`[2] to create an infinite stream using for loop syntax. I’ve been planning to add a `build-sequence` function, too, which would be ideal for this, but I haven’t done that yet. Alexis [1]: http://pkg-build.racket-lang.org/doc/collections/collections-api.html#%28def._%28%28lib._data%2Fcollection..rkt%29._generate-sequence%29%29 [2]: http://pkg-build.racket-lang.org/doc/collections/collections-api.html#%28form._%28%28lib._data%2Fcollection..rkt%29._for%2Fsequence%29%29 > On Dec 20, 2015, at 18:48, mark <probe...@gmail.com> wrote: > > > Hi, there. I have just started playing with Racket and would like some help > with using streams/generators (I am not particularly versed in programming in > a functional style and am attempting to learn :-)) > > So I have a function called (new-name) that creates a random name, like "Bob > Brown" or "Jill Xi". It looks something like > > (define (new-name ethnicity sex) > ; return a string like "First LAST" based on ethnicity and sex) > > This is all good and dandy and works well. What I would like to do now is > something like > > (define name-gen > (infinite-generator > (yield (new-name ethnicity sex)))) ; won't work... > > (get-some-names (name-gen "anglo" "male") 10) ; not sure about this... > > Which would, from a generated list or stream, return ten names. I can do > this using something like > > (for/list ([i 10]) > (new-name "anglo" "male")) > > but that seems to miss out on the goodness of streams/generators. > > Any help/tips appreciated. > > TIA, .. mark. > > -- > 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+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.