(defn my-doc
"Like the doc macro, but requires a var argument, as in #'range ."
[v]
(->> v meta ((juxt :name :arglists :doc)) (map println) doall)
(println))
Intro to Clojure(script)
Michelle Lim + Tyler Perkins // Code for Denver // 3-20-17
It all starts with an expression
(function x y z)
*do function to x, y, and z*
Example 1:
(+ 1 2 3)
*adds 1, 2, and 3*
=> 6
returns six
Example 2:
(= 1 2)
*the equality of 1 and 2 is…*
=> false
returns false
Example 3:
(if (= 1 1) "yes" "no")
*if (= 1 1)
is true, return “yes”, otherwise return “no”*
=> "yes"
returns the third item
Expressions within expressions
Example 4:
(= (+ 1 2 3) 5)
Complete the expression below
(+ 1 2 3)
Example 5:
(if (= (+ 1 2 3) 5) "yes" "no")
Complete the expression below
(= (+ 1 2 3) 5)
Parentheses = (hugs) for your code. Perfect for nesting.
(❤)
Clojure is a general purpose language, designed to be hosted on top of another language
- Clojure runs on the Java Virtual Machine (JVM)
- ClojureScript compiles to Javascript
Tools (clickable):
[live demo: codefordenver/owlet-ui]
Clojure is dialect of Lisp: the second oldest high-level programming language widely used today
Lisp timeline:
A different paradigm
Lisps are functional programming languages.
Javascript, Python, Ruby, C#, and C++ are all object-oriented programming languages.
Context: Data can be passed around by value or reference
- A data value is stored in a specific location in memory
- Most languages pass a reference to that location instead of the value itself, manipulating data in place
- In the early days memory was limited, so the focus was on reusing space
Object-oriented approach
A system based on objects
and specific rules for how they can communicate.
- Passes data by reference. Actual object values are hidden from the rest (encapsulated)
- Objects interact by sending messages and calling methods
- Methods often produce side effects
“In order to safely work with the data you have to know all the places where it might be referenced. The complexity grows with the size of the application. The more code has access to a piece of data the more proverbial balls you end up having to juggle in your head.” —Clojure Distilled
Functional approach
Defines standalone actions (expressions) that are reuseable and composeable
- Passes data by value. Every expression returns a new, transformed copy of the data
- Composes expressions within a larger expression, which becomes the system itself
- The language takes care of figuring out which parts can be cleaned up when they’re no longer used
Functions we’ll use in Programming Functionally, next
- str -
(str 2 "legit" 2 "quit")
- rest -
(rest [0 1 2 3])
- cycle -
(cycle ["one" "two" "three"])
- take -
(take 6 (cycle ["one" "two" "three"]))
- map
- a higher-order function; can take functions as inputs, and return functions as output
(map inc [0 1 2 3])
(map + [0 1 2 3] [4 5 6 7])
First, let’s take them for a spin. Evaluate the examples above using the editor below.
; Use my-doc to see the docs for a function:
(my-doc #'str)
A note on laziness
map
, take
, and cycle
return “lazy” sequences;
with a lazy sequence, the function returns both an item and the next function in the sequence,
providing a natural way to produce infinite data structures..!