Replace "project" with actual project name.
(require 'project.core :reload)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;primitive types ;; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
42 ;;java.lang.Long | |
42.0 ;;java.lang.Double | |
"foo" ;;java.lang.String | |
true ;;java.lang.Boolean | |
false | |
:bla ;;clojure.lang.Keyword | |
;;unique identifiers, much like symbols in Ruby | |
;;used for e.g. map keys | |
'foo ;;clojure.lang.Symbol | |
;;like Symbol but used for identifying bound names | |
;;prefix with ' to prevent automatic dereferencing | |
;;leaving out ' will return the value bound to the symbol | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;data structures (literals) ;; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
[1 2 3] ;;"vector" clojure.lang.PersistentVector | |
{:foo 1 :bar 2} ;;"map" clojure.lang.PersistentArrayMap | |
{42.0 "bar" :x 2} ;;keys can be of any type, they just have to be unique in the map | |
'() ;;"list" clojure.lang.PersistentList | |
(+ 1 2) ;;lists not prefixed with ' are treated as function calls | |
#{1 2 3 4} ;;"set", can only contain unique elements | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;calling functions ;; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
(+ 1 2) | |
(println "foo") | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;binding to a name ;; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
(def foo 42) ;;bind value (42) to a name (foo) | |
;;name will be added to current namespace | |
(def core:foo 1) ;;name will be added to "core" namespace | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;defining functions ;; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
(fn [x y] (+ x y)) ;;anonymous function | |
#( %1 %2) ;;shorthand with just positional arguments | |
#(* % 2) ;;shorthand with just one argument (no numbering needed) | |
(def foo (fn [x] (* x x))) ;;bind an anonymous function to a name | |
(defn foo [x] (* x x)) ;;shorthand for function binding | |
(defn "doc string" foo [x] (* x x)) ;;providing a documentaiton string for the function | |
(doc foo) ;;get the doc string from an existing function | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;threading macros ;; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;refactor this: | |
(println | |
(map (fn [x] (* x x)) | |
(map inc [1 2 3]))) | |
;;into this: | |
(->> [1 2 3] | |
(map inc) | |
(map (fn [x] (* x x))) | |
println) | |
;; ->> inserts the payload as the last argument of the provided forms | |
;; -> inserts the payload as the first argument (or more precisely: the second list element) of the forms | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Namespaces ;; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;declare current namespace and create it if it doesn't exist | |
(in-ns foo.bar.bla) ;;foo.bar.bla maps to source file location src/foo/bar/bla.clj | |
;load stuff from other namespaces | |
(require 'foo.bar) ;;loads the namespace from foo/bar.clj | |
(foo.bar/bla 1 2 3) ;;call a function from another namespace directly | |
(refer 'foo.bar) ;;import all symbols from a namespace | |
(bla 1 2 3) ;;now they are usable lie this, but may collide with existing bindings | |
(refer '[foo.bar :only bla]) ;;cherry pick items from the referred namespace to avoid collisions | |
(alias 'things 'foo.bar) ;;alias a namspace to different name | |
(use 'foo.bar) ;;shorthand for (require ...)(refer ...) | |
(use '[foo.bar :as things]) ;;shorthand for (require ...)(refer ...)(alias ...) | |
;;remeber: (use ...) still produces collisions with existing bindings! | |
(use '[foo.bar :as things :only [bla]]) ;;import a namespace, alias it and cherry pick stuff from it, all in one call | |
;; the (ns) macro | |
(ns awesome.project ;;switch to and create awesome.stuff | |
(:require [foo.bar :as things] ;;load and alias two namespaces without importing the 1st one's names | |
[foo.blurb :as stuff :refer [one-thing]]) ;;while importing cetain names from the 2nd one | |
;;note: don't quote (' prefix) the names in the ns macro |