Last active
December 30, 2021 03:49
-
-
Save jacekschae/ddffcdcd981ecf80dbe66fbef8b54719 to your computer and use it in GitHub Desktop.
ClojureScript and JavaScript Comparison - ClojureScript Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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
Thank you @mfikes!