Skip to content

Instantly share code, notes, and snippets.

@nihaokid
Created August 30, 2013 02:30
Show Gist options
  • Save nihaokid/6385754 to your computer and use it in GitHub Desktop.
Save nihaokid/6385754 to your computer and use it in GitHub Desktop.
problem:
1. give a list, 1 to 10
2. get even number
3. divided by two
4. print them
@nihaokid
Copy link
Author

ruby

(1..10).select{|x| x % 2 == 0}.map{|x| x / 2}.each{|x| puts x}

@nihaokid
Copy link
Author

clojure

lazy version

(take 5 (for [x (iterate inc 1) :when (zero? (rem x 2))] (/ x 2)))

or

(take 5 (map (fn [x] (/ x 2)) (filter even? (iterate inc 1))))

common version

(print (map (fn [a] (/ a 2)) (filter even? (range 1 11))))

or

(println (map #(/ %1 2)  (filter even? (range 10))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment