Skip to content

Instantly share code, notes, and snippets.

@jacekschae
Last active December 30, 2021 03:49
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jacekschae/ddffcdcd981ecf80dbe66fbef8b54719 to your computer and use it in GitHub Desktop.
Save jacekschae/ddffcdcd981ecf80dbe66fbef8b54719 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")
@mfikes
Copy link

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
Copy link
Author

Thank you @mfikes!

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