Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hillkim/305ecbb7fc0822392e44f16bb83ea37b to your computer and use it in GitHub Desktop.
Save hillkim/305ecbb7fc0822392e44f16bb83ea37b to your computer and use it in GitHub Desktop.
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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment