What is the Clojure equivalent of Ruby select?

I want to return a list / collection of all numbers in a range that is a multiple of 3 or 5.

In Ruby, I would do

(1..1000).select {|e| e % 3 == 0 || e % 5 == 0}

In Clojure, I think I can do something like ...

(select (mod 5 ...x?) (range 0 1000))
+3
source share
4 answers
(filter #(or (zero? (mod % 3)) (zero? (mod % 5))) (range 1000))
+5
source

Another way is to create a solution, rather than filtering it:

(set (concat (range 0 1000 3) (range 0 1000 5)))
+5
source
(filter #(or (= (mod % 5) 0) (= (mod % 3) 0)) (range 1 100))

- the most direct translation.

(for [x (range 1 100) :when (or (= (mod x 5) 0) (= (mod x 3) 0))] x)

- This is another way to do it.

Instead of doing (= .. 0), can you use zero? instead of this. Here is the corrected solution:

(filter #(or (zero? (mod % 5)) (zero? (mod % 3))) (range 1 100))
+3
source
+1
source

Source: https://habr.com/ru/post/1755637/


All Articles