Skip to content

Instantly share code, notes, and snippets.

@vinayvinay
Created May 14, 2011 09:09
Show Gist options
  • Save vinayvinay/972059 to your computer and use it in GitHub Desktop.
Save vinayvinay/972059 to your computer and use it in GitHub Desktop.
sharing my first steps to picking-up clojure using 4clojure.com
;;; 25
(= (filter #(= (mod % 2) 1) #{1 2 3 4 5}) '(1 3 5))
(= (filter #(= (mod % 2) 1) [4 2 1 6]) '(1))
(= (filter #(= (mod % 2) 1) [2 2 4 6]) '())
(= (filter #(= (mod % 2) 1) [1 1 1 3]) '(1 1 1 3))
;;; 24
(= (reduce (fn [x y] (+ x y)) [1 2 3]) 6)
(= (reduce (fn [x y] (+ x y)) (list 0 -2 5 5)) 8)
(= (reduce (fn [x y] (+ x y)) #{4 2 1}) 7)
(= (reduce (fn [x y] (+ x y)) '(0 0 -1)) -1)
(= (reduce (fn [x y] (+ x y)) '(1 10 3)) 14)
;;; 23
(= (reduce (fn [x y] (conj x y)) () [1 2 3 4 5]) [5 4 3 2 1])
(= (reduce (fn [x y] (conj x y)) () (sorted-set 5 7 2 7)) '(7 5 2))
(= (reduce (fn [x y] (conj x y)) () [[1 2][3 4][5 6]]) [[5 6][3 4][1 2]])
;;; 22
(= (reduce (fn [x y] (inc x)) 0 '(1 2 3 3 1)) 5)
(= (reduce (fn [x y] (inc x)) 0 "Hello World") 11)
(= (reduce (fn [x y] (inc x)) 0 [[1 2] [3 4] [5 6]]) 3)
(= (reduce (fn [x y] (inc x)) 0 '(13)) 1)
(= (reduce (fn [x y] (inc x)) 0 '(:a :b :c)) 3)
;;; 21
(= (#(first (drop %2 %1)) '(4 5 6 7) 2) 6)
(= (#(first (drop %2 %1)) [:a :b :c] 0) :a)
(= (#(first (drop %2 %1)) [1 2 3 4] 1) 2)
(= (#(first (drop %2 %1)) '([1 2] [3 4] [5 6]) 2) [5 6])
;;;20
(= (#(second (reverse %)) (list 1 2 3 4 5)) 4)
(= (#(second (reverse %)) ["a" "b" "c"]) "b")
(= (#(second (reverse %)) [[1 2] [3 4]]) [1 2])
;;; 19
(= (#(first (reverse %)) [1 2 3 4 5]) 5)
(= (#(first (reverse %)) '(5 4 3)) 3)
(= (#(first (reverse %)) ["b" "c" "d"]) "d")
;;; 18
(= '(6 7) (filter #(> % 5) '(3 4 5 6 7)))
;;; 17
(= '(6 7 8) (map #(+ % 5) '(1 2 3)))
;;; 16
(= (#(str "Hello, " % "!") "Dave") "Hello, Dave!")
;;; 15
(= (#(* % 2) 2) 4)
;;; 14
(= 8 ((fn add-five [x] (+ x 5)) 3))
;;; 13
(= '(20 30 40) (rest [10 20 30 40]))
;;;12
(= 3 (first '(3 2 1)))
;;; 11
(= {:a 1, :b 2, :c 3} (conj {:a 1} [:b 2] [:c 3]))
;;; 10
(= 20 ((hash-map :a 10, :b 20, :c 30) :b))
;;; 9
(= #{1 2 3 4} (conj #{1 4 3} 2))
;;; 8
(= #{:a :b :c :d} (set '(:a :a :b :c :c :c :c :d :d)))
;;; 7
(= [1 2 3 4] (conj [1 2 3] 4))
;;; 6
(= [:a :b :c] (list :a :b :c) (vec '(:a :b :c)) (vector :a :b :c))
;;; 5
(= '(1 2 3 4) (conj '(2 3 4) 1))
;;; 4
(= (list :a :b :c) '(:a :b :c))
;;; 3
(= "HELLO WORLD" (.toUpperCase "hello world"))
;;; 2
(= (- 10 (* 2 3)) 4)
;;; 1
(= true true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment