Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ClojureScript and JavaScript Comparison - ClojureScript Examples
;; Functions
(defn sum [x y] (+ x y))
;; Shorthand Functions
(def sum #(+ %1 %2))
;; Function Invocation
(sum 3 4)
;; Anonymous Functions
(fn [x y] (+ x y))
;; Anonymous Shorthand Functions
#(+ %1 %2)
;; map
(map inc [1 2 3])
;; filter
(filter odd? [1 2 3])
;; reduce
(reduce + 0 [1 2 3])
;; if without else
(when (= lang "de") "german")
;; if with else
(if (= lang "de")
"german"
"english")
;; if with multiple eles
(defn which-lang
[lang]
(cond
(= lang "de") "german"
(= lang "es") "spanish"
:else "english"))
;; switch
(defn which-lang
[lang]
(case lang
"de" "german"
"es" "spanish"
"english"))
;; falsy
false
nil
;; State
(atom {:one 1, :two 2})
(volatile! {:one 1, :two 2})
;; Interop
;; Method call
(.log js/console "Hello")
;; Property access
(.-PI js/Math)
;; Property setting
(set! (.-title js/document) "Hello")
;; window method call
(js/alert "Hello")
@mfikes

This comment has been minimized.

Copy link

@mfikes mfikes commented Feb 22, 2018

The which-lang cond example should be

(defn which-lang
  [lang]
  (cond
   (= lang "de") "german"
   (= lang "es") "spanish"
   :else "english"))

or, using condp,

(defn which-lang
  [lang]
  (condp = lang
   "de" "german"
   "es" "spanish"
   "english"))
@jacekschae

This comment has been minimized.

Copy link
Owner Author

@jacekschae jacekschae commented Feb 22, 2018

Thank you @mfikes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.