Created
May 7, 2013 05:32
-
-
Save graph1zzlle/5530464 to your computer and use it in GitHub Desktop.
clojure sh*t
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Fix for the compiling problem on IntelliJ with La-Clojure: | |
;; Settings -> Compiler -> Clojure Compiler -> Copy Clojure source files to output... | |
(filter odd? [1 2 3 4 5 6]) ;; => 1 3 5 | |
(first(filter odd? [1 2 3 4 5 6])) ;; => 1 | |
(first '(1 2 3)) ;; => 1 | |
(first [1 2 3]) ;; => 1 | |
;(first (1 2 3)) ;; => error ! because (1 2 3) is not interpreted as a list here | |
;; the compiler expect a function as first arg of ( ) | |
(+ 1 2 3) ;; => 6 | |
(apply + [1 2 3]) ;; => 6 | |
(def twice | |
(fn [n] | |
(+ n n))) | |
(twice 10) ;; => 20 | |
(def factorial | |
(fn [n] | |
(apply * (range 1 (+ n 1))))) ;; (doc range) => Returns a lazy seq of nums from start (inclusive) to end | |
;; (exclusive) so its like (apply * [1 . . . n]) | |
(factorial 5) ;; => 120 | |
(def add-squares | |
(fn [& numbers] | |
(apply + (map * numbers numbers)))) ;; (doc map) => Returns a lazy sequence consisting of the result of applying f to the | |
;; set of first items of each coll, followed by applying f to the set of | |
;; second items in each coll, until any one of the colls is exhausted. | |
(add-squares 4 4) ;; => 32 | |
(def prefix-of? | |
(fn [candidate list] | |
( = (take (count candidate) list ) | |
candidate ))) | |
(prefix-of? [1 2] [1 2 3 4]) ;; => true | |
(def tails | |
(fn [seq] | |
(map drop | |
(range (inc (count seq))) ;; (doc range) ... | |
(repeat (inc (count seq)) seq )))) | |
(tails [1 2 3 4 5 6 7]) | |
;; => ((1 2 3 4 5 6 7) (2 3 4 5 6 7) (3 4 5 6 7) (4 5 6 7) (5 6 7) (6 7) (7) ()) | |
(def puzzle | |
(fn [list] | |
(list list))) ;; => compile error | |
(def puzzle | |
(fn [seq] | |
(list seq))) ;; => works ! | |
(prn(puzzle '(1 2 3))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment