Skip to content

Instantly share code, notes, and snippets.

@tilakpatidar
Last active January 24, 2017 11:41
Show Gist options
  • Save tilakpatidar/bfd58d8e547ede0f0759c78374ae09f0 to your computer and use it in GitHub Desktop.
Save tilakpatidar/bfd58d8e547ede0f0759c78374ae09f0 to your computer and use it in GitHub Desktop.
clojure basics
;Clojure is compiled language but has a eval function like lisp which allows to run compiler within execution
;Visit https://learnxinyminutes.com/docs/clojure/ for more gists
5 ; ⇒ 5
"hi" ; ⇒ "hi"
[1 2 3] ; evaluates to the vector `[1 2 3]`
(+ 1 2) ; evaluates to the sum of 1 and 2
(if true "yes" "no") ; evaluates to the string "yes"
(println "hello!") ; evaluates to nil (but also prints "hello!")
(+ 1
(* 2 3)
(/ 10 2)) ; ⇒ 1 + (2 * 3) + (10 / 2) = 12
(doc print)
(source print)
(def the-answer 42)
#"^foo\d?$" ; A regular expression.
:foo ; A keyword.
;Clojure's keywords are same as symbols in Ruby.
;Keywords are just scalars that evaluate to themselves and are useful where in other languages you might use little strings as identifiers (for example, as the keys in a hashmap).
;create a keyword from a string using the keyword method
(keyword "blues")
(type :blues) ;this is a keyword
::blues ;this is a namespace
(symbol 'foo)
(symbol :foo) ;This won't work as keywords cannot be converted to symbols in clojure
(eval :blues) ;Lisp like eval
;defs are evaluated only once whereas defns (with or without arguments) are evaluated (executed) every time
;defs cannot have any arguments
;defns should have arguments
;type returns type after evaluation
(def blue "color")
(type blue) ;will show java.lang.String
(defn add [a b] (+ a b))
(type add) ;user$add
(type 'tilak')
;clojure.lang.Symbol
(type "tilak")
;java.lang.String
;if statement with if and else branch
(if true "tilak" "patidar")
;do lets you to use multiple lines in if and else branch
(if true
(do (+ 5 2))
(do (-5 2))
)
;when is like if but no else branch
(when true (println "tilak"))
(nil? 1)
;false
(nil? nil)
;true
(= 1 1)
;just single = is a equality operator for any kind of clojure object data type
(or true false)
(and true false)
;clojure does not have assignments with = instead use def to define variable names
(def my-name "tilak")
;clojure does not concat strings with + use str instead
(str "tilak" " " "patidar")
(def a [1,2,3])
(get a 2)
(contains? a 2)
(+ 1 2 3 4)
(* 1 2 3 4)
(first [1 2 3 4])
(last [1 2 3 4])
#{12 3 5} ;hash set
(hash-set 1 2 3); or another way of making a hash set
(inc 1.1)
(map inc [1 2 3])
;number of arguments a function has is it's arity
(defn sum
"Adds two numbers"
[a b]
(+ a,b)
)
;functions having multiple arity
(defn sum
([a b]
(println "two args")
(+ a b)
)
([a b c]
(println "three args")
(+ a b c)
)
)
;anonyoumous functions
(fn [a] (* a 3))
((fn [a] (* a 3)) 8) ;24
;; Function call
(* 8 3)
;; Anonymous function
#(* % 3)
;multiple arguments in anonyoumous function
(#(str %1 " and " %2) "cornbread" "butter beans")
(defn pos-neg-or-zero
"Determines whether or not n is positive, negative, or zero"
[n]
(cond
(< n 0) "negative"
(> n 0) "positive"
:else "zero"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment