Skip to content

Instantly share code, notes, and snippets.

@st
Last active August 29, 2015 14:22
Show Gist options
  • Save st/4388d3dd4acda1a4f584 to your computer and use it in GitHub Desktop.
Save st/4388d3dd4acda1a4f584 to your computer and use it in GitHub Desktop.

Your very first steps with Clojure

;; $> lein repl into-1 ;; ;; This will create a Clojure project (a folder with a common structure) named 'into-1' ;; $>

1 "hello" Sytem/out

;; ;; Java interop ;; System/out

;; a different notation (we'll stress this later on) to call functions ;; (.println System/out "Robert it s up to you") ;; ;; ;; Let's play with the REPL ;; what does the REPL understand? 'hello' 'hello "hello" abc :abc nil "hello" hello :abc 123 (1 2 3) ;; With this (+ 1 2) (+ 1 2 3) (1 2 3) (quote 1 2 3) (quote (1 2 3)) '(1 2 3) (+ 1 2 3) [1 2 3] ; a list ;a list ["a" 1] ["a" 1 '(1 2 3)] '(nil :abc ["a" 1 '(1 2 3)]) {} {:firstname "robert"} '() () [] {:firstname "robert" :age 12} {"a" 1} {"a" 1 "a" 2} {"a" 1 nil 2 } {"a" 1 [1 2 3] 2 } {:firstname "robert" :age 12} {:firstname "robert" :age 12 :friends ["mike" "john"]} #{1 2 3} #{1 2 3 3} ; comment (= nil nil) (= :nil :nil) [1 2 3 4] [1 #2 3 4] ; special comment #

;; Bindings ;; now, we want to associate a symbol with a value ;; 'def' is made for that! ;; (def a 1) a b (def b 2) b ; def ; let (let [x 10 y 2] ) x (let [x 10 y 2] x) (let [x 10 y 2]) (let [x 10 y 2] ) (let [x 10 y 2] (+ x y) ) (fn [x y] 1 ) ((fn [x y] 1)) ( (fn [x y] 1) :abc [] ) ; fn (def one (fn [x y] 1)) (one) (one 1 2) (defn two [x y] 2) (two 3 4) (defn plus [a b c] (+ a b c)) (plus 1 2 3) (if (> 2 1) "YEAH" "booh") (if (> 2 10) "YEAH" "booh") (if (> 2 1) (do (print "waou") "YEAH") "booh") (if (> 2 1) (do (println "waou") "YEAH") "booh") (defn example [a] (println "OK") (println "bye") (+ a 1)) (example 10) (example 100)

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