On Github canweriotnow / bohconf.clojure
function(){ return 5; }
(function() return 5; )
(fn [] return 5; )
(fn [] 5)Welcome to Clojure.
someFunction(arg1, arg2, arg3);Move the left parenthesis over a bit more...
(someFunction arg1 arg2 arg3)Done.
{:key1 5, :key2 nil} [1 2 3 4 "five"]Don't freak out
[1 [2] #{3} {4 4} (constantly 5)]DON'T FREAK OUT
=> (range 10) (0 1 2 3 4 5 6 7 8 9) => (take 11 (range)) (0 1 2 3 4 5 6 7 8 9 10) => (last (range)) ;;Hope you don't mind waiting a long time.
;; semicolons are comments, commas are ignored, ;; check out this weird hash-map {:a-keyword 5, "a string key" "a string value", ["a" :vector "acting" :as [:a :compound] "key"] (fn [] "a no-arg function that returns this multi-line string, the function itself is the value"), + '(functions can be keys too, and when you quote symbols, you just have symbols, not what they represent)}Evals to...
{:a-keyword 5, "a string key" "a string value", ["a" :vector "acting" :as [:a :compound] "key"] #<user$eval331$fn__332 user$eval331$fn__332@a585ef>, #<core$_PLUS_ clojure.core$_PLUS_@20a12d8f> (functions can be keys too and when you quote symbols you just have symbols not what they represent)}
(defn inc-last [val] (conj val (inc (last val)))) ;; We make a sequence of 10 inc-last tasks, ;; then follow-up with a 'println' task (def tasks (concat (repeat 10 inc-last) [(fn [val] (println val) val)]))
;; starts off with a value of [0] (let [a (agent [0])] (doseq [t tasks] (send a t))) ;; prints: [0 1 2 3 4 5 6 7 8 9 10]
(let [f (future (do-a-bunch-of-stuff))] ;; in another thread (do-stuff-in-this-thread) ;; return the value in f, blocking if it's not finished (deref f))
(class (read-string "(+ 1 2)")) ;; clojure.lang.PersistentList (map class (read-string "(+ 1 2)")) ;; (clojure.lang.Symbol java.lang.Long java.lang.Long)
(defn only-even! [val] (if (and (integer? val) (odd? val)) (inc val) val)) (map only-even! (read-string "(+ 1 2)")) ;; '(+ 2 2) (eval (map only-even! (read-string "(+ 1 2)"))) ;; 4This is only the beginning
$("#p1").css("color","red").slideUp(2000).slideDown(2000);How is this implemented? Is this reusable?
(use 'clojure.string) ;; These are equivalent (map trim (split (upper-case "hola, world") #",")) ;; ("HOLA" "WORLD") (-> "hola, world" upper-case (split #",") (->> (map trim))) ;; ("HOLA" "WORLD")
(->> (range) (filter even?) (map (partial * 2)) (take 10) (into [])) ;; [0 4 8 12 16 20 24 28 32 36] ;; versus (into [] (take 10 (map (partial * 2) (filter even? (range)))))I find the flat one easier to think about. Semantically equivalent. No burden on implementing code. Functions don't care about how they're used.
Giving the user choices is more effective with more powerful languages. Leads to simple, composable libraries.
(defmacro lazy-seq "Takes a body of expressions that returns an ISeq or nil, and yields a Seqable object that will invoke the body only the first time seq is called, and will cache the result and return it on all subsequent seq calls. See also - realized?" {:added "1.0"} [& body] (list 'new 'clojure.lang.LazySeq (list* '^{:once true} fn* [] body))) ;; simply returns a list, allocates a Java object (LazySeq) and wraps ;; your expressions in a function (macroexpand-1 '(lazy-seq ANYTHING1 ANYTHING2)) ;; '(new clojure.lang.LazySeq (fn* [] ANYTHING1 ANYTHING2))
(defn square-wave "t is the period for a half-cycle" [t] (letfn [(osc [cur-value so-far] (let [so-far (mod so-far t) next-val (if (zero? so-far) (- cur-value) cur-value)] (cons next-val (lazy-seq (osc next-val (inc so-far))))))] (osc 1 0)))
(take 10 (square-wave 3)) ;; (-1 -1 -1 1 1 1 -1 -1 -1 1)No mutable variables