Skip to content

Latest commit

 

History

History
115 lines (69 loc) · 6.24 KB

Cheatsheet.md

File metadata and controls

115 lines (69 loc) · 6.24 KB
title geometry output
Cheatsheet - interactive dev (2 pages)
margin=0.7cm
pdf_document

TIP: Starting the REPL, server - see README - Connecting VS Code to the server REPL and starting the server (at the bottom). (In short: run "Calva Start REPL.." with Experience interactive development workshop and then open server.clj, "Calva Load ..", Alt-Enter at the line (-main).)

Clojure - A minimal Clojure subset

General

, is a whitespace - ignored, add for readability where you want

Everything is an expression and returns a value (no need for return ...). The last expression inside a function is its return value.

Data & data literals

{:my-key "value", :another 123} - a (hash)map (~ JS object)

To get the value, use the keyword as a function of the map: (:my-key my-map)

[1 2 :three] - a vector (~ JS array)

nil, :keyword, 1, "string", true, false - primitives

Definitions and functions

def - (def <name> <value>) - give a name to a piece of data or a function (i.e. "define" a "var" in Clojure speak). Treat as a constant! Ex.: (def pi 3.14)

fn - (fn [arg1 arg2 ...] <body>) - define a function

defn - ± same as (def my-name (fn [...] ...))

Catch: We define the arguments of a function inside a vector but when we call it, we include them directly: (defn plus [x y] (+ x y)) -> (plus 1 2)

Collection and sequence function

map - (map <function> <sequence>) - change each element; ex.: (map (fn [n] (+ n 1)) [1 2]) ; => (2 3)

filter - (filter <function> <sequence>) - keep only the elements for which the function does not return nil or false

TIP: When you use map/filter, type all the arguments first before working out the function, i.e. begin with (map (fn [x] x) my-data)

first - (first <sequence>) - return the first element of the sequence

select-keys - make a subset of a map; ex.: (select-keys {:a 1,:b 2,:c 3} [:a :b]) produces {:a 1, :b 2}

Logic and comparison

= - (= arg1 arg2 ...) - are all the arguments equal?

not= - the opposite of =

if - (if condition true-expression false-expression) (you can leave out the false-expression, it defaults to nil)

Flow control

->> - (->> input (fn1) (fn2) ...) - a transformation pipeline, "threading" data through a series of functions (transformations). Invokes fn1 with the input as the last argument, then fn2 with the result of that as the last argument etc. Example:

(->> 6
     (- 10)   ; = (- 10 6)
     (* 2 2)) ; = (* 2 2 4)
;; => 16

let - (let [name1 value1, ...] body) - introduce local constants (bindings) available inside its body, giving names to pieces of data. (Note: the [] here is not really creating a vector of data, it just tells to let "here come the bindings..." similarly as in a defn it tells "here comes the declaration of arguments".) Example below:

(let [five 5,
      ten (* 2 five)]
  (= 15 (+ five ten)))

REPL troubleshooting

println - print the thing(s)

pr-str - print Clojure data into a string so that Clojure can read them back

Golden Rules

Always start with typing a ( when writing code (unless you you just want to refer to a named piece of data) - thus preventing confusing both yourself and Calva :-).

Function name comes always first, right after (: (function-name ...) - even + and similar.

VS Code and Calva

VS Code and Calva shortcuts

You will need these shortcuts during the workshop:

  1. Alt ENTER - evaluate the current top-level expression in the REPL - i.e. a function definition, an expression inside a comment block (your cursor can be ± anywhere at the line; if you experience troubles then move it to the very end)
  2. (Ctrl ENTER - evaluate the thing the cursor is on/right after - similar to nr. 1 but useful if you want to evaluate a smaller thing inside a bigger expression, f.ex. to look at the value a name refers to)
  3. Ctrl-Alt-right arrow (OSX and Win; Linux: . instead of ->) - "slurp" the following element into the current list: (def| x) 42 -> (def| x 42) (ctrl-alt-left arrow (,) does the opposite but we will likely not need it) BEWARE: On OSX it might conflict with a system shortcut. Fix or use Ctrl-W, cut, paste.
  4. Ctrl-W (OSX) / Shift-Alt-right arrow (Win, ?Lin) - expand selection (press repeatedly) - useful to select the thing you will move/change (often in combination with Cut and Paste)
  5. (Bonus: Cmd-| (OSX; Ctrl-Shift-\ on Windows, Ctrl+Shift+\ Linux, cmd "Go to Bracket") jumps between the opening and closing parentheses.)
  6. Escape - hide the inline results of evaluation (as per 1. or 2.)

(If you love shortcuts, check out https://calva.io/commands-top10/ There is also Linux, MacOS, and Windows keyboard shortcut cheatsheet for VS Code.)

Editing tips

Use Ctrl-W / Shift-Alt-right arrow to (select, cut, and) move code around - this ensures you won't end up with mismatched parentheses.

You can only delete empty parentheses/braces/.. so Ctrl-W to select and cut the content, then backspace to delete the opening and thus also closing parenthesis/....

If you screw up, use Ctrl-Z (Cmd-Z on OSX) to back up.