How to extract 5 elements from a sequence?
Weird there isn’t a sequence-take
I’m guessing you just want the first five elements?
(for/list ([element a-sequence]
[n (in-range 5)])
element)
@samdphillips Thanks for your answer! I just came across an article about “C++20 Ranges Library” with an example. https://medium.com/@tjsw/%E6%BD%AE-c-20-ranges-views-%E4%BD%BF%E7%94%A8-stl-%E7%9A%84%E5%85%A8%E6%96%B0%E5%A7%BF%E5%8B%A2-8007777a8da6\|https://medium.com/@tjsw/%E6%BD%AE-c-20-ranges-views-%E4%BD%BF%E7%94%A8-stl-%E7%9A%84%E5%85%A8%E6%96%B0%E5%A7%BF%E5%8B%A2-8007777a8da6
And I want to implement the same program in Racket. I thought it would be more elegant and concise, like following.
(map sqr)
(filter (curry divides? 4))
(take 5))])
(displayln i))
But I just can’t achieve the same level as I thought without using Generic Collections. https://docs.racket-lang.org/collections/index.html?q=generic\|https://docs.racket-lang.org/collections/index.html?q=generic
Perhaps @notjack has a solution?
(require rebellion/streaming/reducer
rebellion/streaming/transducer
fancy-app)
(transduce (in-naturals 1)
(mapping sqr)
(filtering (divides? 4 _))
(taking 5)
#:into (into-for-each displayln))
and now, back to sleep :zzz: