wanpeebaw
2020-10-23 13:21:09

How to extract 5 elements from a sequence?


samdphillips
2020-10-23 14:41:33

Weird there isn’t a sequence-take


samdphillips
2020-10-23 14:41:47

I’m guessing you just want the first five elements?


samdphillips
2020-10-23 14:42:51

(for/list ([element a-sequence] [n (in-range 5)]) element)


wanpeebaw
2020-10-23 15:34:12

@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


soegaard2
2020-10-23 15:39:24

Perhaps @notjack has a solution?


notjack
2020-10-23 15:43:40

(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))


notjack
2020-10-23 15:44:15

and now, back to sleep :zzz: