Skip to content

Instantly share code, notes, and snippets.

@BrianLitwin
Created April 23, 2019 08:55
Show Gist options
  • Save BrianLitwin/bf8714af93ef148e2ed4a1479e58af5a to your computer and use it in GitHub Desktop.
Save BrianLitwin/bf8714af93ef148e2ed4a1479e58af5a to your computer and use it in GitHub Desktop.

Literal Collections

'(1 2 3)     ; list
[1 2 3]      ; vector
#{1 2 3}     ; set
{:a 1, :b 2} ; map

Test your knowledge: 2. Rewrite the following algebraic expression as a Clojure expression: ( 7 + 3 * 4 + 5 ) / 10

(/ (+ (* 3 4) 5 7) 10)
  1. Using REPL documentation functions, find the documentation for the rem and mod functions. Compare the results of the provided expressions based on the documentation.

  2. Using find-doc, find the function that prints the stack trace of the most recent REPL exception.

Creating Functions

Clojure is a functional language. Functions are first-class and can be passed-to or returned-from other functions. Most Clojure code consists primarily of pure functions (no side effects), so invoking with the same inputs yields the same output.

defn defines a named function:

;;    name   params         body
;;    -----  ------  -------------------
(defn greet  [name]  (str "Hello, " name) )

This function has a single parameter name, however you may include any number of arguments in the params vector. Invoke a function with the name of the function in "function position" (the first element of a list):

Variadic Functions

Functions may also define a variable number of arguments - this is known as a "variadic" function. The variable arguments must occur at the end of the argument list. They will be collected in a sequence for use by the function. The beginning of the variable arguments is marked with &.

(defn hello [greeting & who]
  (println greeting who))

This function takes an argument greeting and a variable number of arguments (0 or more) that will be collected in a list named who. We can see this by invoking it with 3 arguments:

user=> (hello "Hello" "world" "class")
Hello (world class)

You can see that when println prints who, it is printed as a list of two elements that were collected.

Ananymous Functions

An anonymous function can be created with fn:

;;    params         body
;;   ---------  -----------------
(fn  [message]  (println message))

Because the anonymous function has no name, it cannot be referred to later. Rather, the anonymous function is typically created at the point it is passed to another function.

defn vs fn It might be useful to think of defn as a contraction of def and fn. The fn defines the function and the def binds it to a name. These are equivalent: (defn greet [name] (str "Hello, " name)) (def greet (fn [name] (str "Hello, " name)))

Ananymous function syntax There is a shorter form for the fn anonymous function syntax implemented in the Clojure reader: #(). This syntax omits the parameter list and names parameters based on their position.

% is used for a single argument %1, %2, %3, etc are used for multiple arguments %& is used for any remaining (variadic) arguments

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