Homepage / Notes / Computer Science / Programming Languages / Clojure
A Lisp on the JVM
(print)
to print for humans(println)
to print with newline for humans(pr)
to print for data(prn)
to print with newline for data+ 1 1) (
2
Sum:
reduce + [1 2 3]) (
6
"Damien"
Damien
apply str (reverse "Damien")) (
neimaD
def name "Damien")
(str "Hello " name) (
#'user/name |
"Hello Damien" |
", " ["element 1" "element 2" "element 3"]) (clojure.string/join
element 1, element 2, element 3
"Damien" "en") (clojure.string/includes?
true
def x 42)
( x
#'user/x |
42 |
defonce
defonce
defines variable only if it has never been defined before:
defonce y 9)
( y
9
If you try to re-define it, it won't work:
defonce y 10)
( y
9
let [[a b c] '(1 2 3)] (println a b c)) (
1 2 3
Check if something is nil
:
nil? nil) (
true
Check if something is NOT nil
:
nil) (some?
false
1) (some?
let
can be used to define a variable in a specific scope:
let [z 5] z) (
5
z
is not defined outside of the scope:
z
class clojure.lang.Compiler$CompilerException
"defn" = "def" + "fn" (define function)
defn greet [name] (str "Hello, " name))
("Damien") (greet
#'user/greet |
"Hello, Damien" |
defn greet
("World!"))
([] (greet name] (str "Hello, " name)))
([
"Damien")
(greet (greet)
#'user/greet |
"Hello, Damien" |
"Hello, World!" |
#()
with %
used as arguments (%1
, %2
for first and second arguments)
map #(* 2 %) '(1 2 3)) (
(2 4 6)
1 2 3) '(
(1 2 3)
first '(1 2 3)) (
1
last '(1 2 3)) (
3
some #(= 3 %) '(1 2 3)) (
true
(some)
alone only returns nil
, not false
some #(= 4 %) '(1 2 3))) (some? (
false
get {:a 1 :b 2} :a) (
1
"get" can be omitted
:a 1 :b 2} :b) ({
2
get-in {:error {:code 404 :message "Wrong location"}} [:error :code]) (
404
if true "this is true" "this is false") (
this is true
if false "this is true" "this is false") (
this is false
Only handles "true", and wrapped in an implicit (do)
statement
when true (println "this is true") (println "this is also true")) (
this is true
this is also true
def x 10)
(cond (= x 10) "true") (
#'user/x |
"true" |
Note: for this type of conds, if the first is true, it won't continue
def x (rand-int 50))
(cond
(> x 40) "more than 40"
(> x 30) "more than 30"
(> x 20) "more than 20"
(> x 10) "more than 10"
(:else "neither")
#'user/x |
"more than 30" |
Another example
def x (rand-int 50))
(cond
(> x 40) "more than 40"
(> x 30) "more than 30"
(> x 20) "more than 20"
(> x 10) "more than 10"
(:else "neither")
#'user/x |
"neither" |
let [name "Damien"]
(case name
("Damien" "my real name"
"Émilie" "my wife's name"
"Nicolas" "my brother's name"))
my real name
let [name "Émilie"]
(case name
("Damien" "my real name"
"Émilie" "my wife's name"
"Nicolas" "my brother's name"))
my wife's name
Can provide a default:
let [name "Hugo"]
(case name
("Damien" "my real name"
"Émilie" "my wife's name"
"Nicolas" "my brother's name"
"Unknown name"))
Unknown name
Can "match" multiple values using a list:
let [name "Émilie"]
(case name
("Damien" "Émilie" "Nicolas") "a family member"
("not a family member"))
a family member
equality
of the data.meta #'+)) (pprint (
{:added "1.2",
:ns #namespace[clojure.core],
:name +,
:file "clojure/core.clj",
:inline-arities #function[clojure.core/>1?],
:column 1,
:line 986,
:arglists ([] [x] [x y] [x y & more]),
:doc
"Returns the sum of nums. (+) returns 0. Does not auto-promote\n longs, will throw on overflow. See also: +'",
:inline #function[clojure.core/nary-inline/fn--5606]}
def m ^:hi [1 2 3])
(meta (with-meta m {:bye true})) (
#'user/m |
{:bye true} |
require '[clojure.string :as str]) (
"a b c") #" ") (str/split (str/upper-case
["A" "B" "C"]
Becomes
-> "a b c" (str/upper-case) (str/split #" ")) (
["A" "B" "C"]
Insert as last argument
->> (range 10) (filter odd?)) (
(1 3 5 7 9)
https://lwhorton.github.io/2018/10/20/clojurescript-interop-with-javascript.html
https://google.github.io/closure-library/api/
https://github.com/babashka/nbb
Babahska for ClojureScript / Node.js
https://github.com/babashka/scittle
The Small Clojure Interpreter exposed for usage in browser script tags
https://github.com/thheller/shadow-cljs
ClojureScript compilation made easy
https://github.com/clj-commons/secretary
A client-side router for ClojureScript
https://www.youtube.com/playlist?list=PLUGfdBfjve9VGzp7G1C9FYfH8Yk1Px-11
https://github.com/babashka/babashka
Native, fast starting Clojure interpreter for scripting
https://github.com/clj-kondo/clj-kondo
A linter for Clojure code that sparks joy.
The Fullest Full Stack Clojure Web Framework
https://github.com/weavejester/hiccup
Fast library for rendering HTML in Clojure
https://github.com/Tensegritics/ClojureDart
A port of Clojure that compiles to Dart (mostly for Flutter framework)
https://github.com/tonsky/datascript
Immutable database and Datalog query engine for Clojure, ClojureScript and JS
https://github.com/borkdude/quickdoc
Quick and minimal API doc generation for Clojure
Pedestal is a set of libraries that we use to build services and applications. It runs in the back end and can serve up whole HTML pages or handle API requests.
https://github.com/djblue/portal
A clojure tool to navigate through your data.
Automate Clojure projects without setting your hair on fire.
https://github.com/babashka/neil
A CLI to add common aliases and features to deps.edn-based projects.
https://kimh.github.io/clojure-by-example/
https://lambdaisland.com/episodes/list-comprehension-clojure-for
https://lambdaisland.com/episodes/clojure-seq-seqable
https://lambdaisland.com/episodes/javascript-libraries-clojurescript
https://lambdaisland.com/episodes/clojure-keyword-arguments
https://lambdaisland.com/episodes/clojurescript-interop