webpage using Java library ! ! ;; Imports the java.net.URL class into current namespace (import ‘java.net.URL) ;; Creating an instance of URL. ;; In Java: ;; URL con = new URL(“http://www.reddit.com”) (def con (URL. “http://www.reddit.com”)) ! ! ;; GET the page and print it! (println (slurp con))
by composing over functions EXAMPLE 2 Applying arbitrary function to a collection of data ! ;; Name a collection of numbers (def data [1 2 3 4 5]) ! ;; Creates a function called process-‐data that accepts a collection and an operation to be (defn process-‐data [collection operation] (apply operation collection)) ! ;; Addition (process-‐data data +) ! ;; Multiplication (process-‐data data *) ! Relax if you aren’t following the syntax :-) You’ll learn this in the workshop
State ! Identity is a reference to something. State is the aggregated values of an identity at a particular point in time. In Clojure, an identity’s state is updated by creating a new value and assign it back to the identify. ! Whaaaaat? Isn’t that inefficient???
Processing Problem Problem The Boss just asked you to parse 100K serialized JSON objects into a hash. Each string is ~300KB. The collection/array of strings are stored in the variable problemsCollection. ! Write a snippet that makes use of all cores to process this collection. Don’t worry about storing the result. ! Do this within a minute, or you are fired. ! Time starts now.
Processing Problem Solution ! ! ;; non-‐parallelized version that will get you fired (dorun (map json/read-‐str problemsCollection)) ! ;; parallelized version (dorun (pmap json/read-‐str problemsCollection)) ! ! EXAMPLE 3 (DEMO) ! Note pmap is a parallelized version of map
Concurrent Operations Coordinated Concurrency One or more actors (threads) must coordinate to produce the right computation. e.g. Bank transactions ! Synchronous Concurrency Should the thread that wants to update a reference be blocked until their return?
atom (def counter (atom 0)) Access an atom’s value (dereferencing) (deref counter) ! ;; syntactical sugar @counter Compare and Swap (swap! counter + 2) ! Note: Clojure uses ! in a function’s name to denote that the function mutates state (memory/IO).
(STM) STM: Database-like Transactions for Concurrency ! Clojure controls updates to references via a system called STM. It removes the need for manual locking. ! ! ! ! ! Fulfills the Atomicity, Consistency, and Isolation aspects of the Database Transaction Properties ACID. STM is to concurrency what Garbage Collector is to memory management
(STM) There are more to concurrency Clojure has “data flow variables” too! We’ll cover concurrency in the workshops later. Things we will look at: ! Future Delay Promise
a Lisp? ! ! ! ! ! ! Lisp is invented by John McCarthy in 1958. Lisp pioneered ideas like tree data structures, dynamic typing, garbage collection, higher- order functions, recursion. John McCarthy is quite the hero in Computer Science.
Processing Lisp is written in symbolic expressions, or S- expressions. S-expressions are written as a pair of parenthesis surrounding a sequence of symbols: (f a b c) ! The first position in the list is also known as the functional position. When evaluated, the function f will be called with the rest of the list as arguments.
Collection types in Clojure ! List (a b c d e) Essentially a linked list Vector [a b c d e] Like “arrays” in Ruby/Python ! Wait a second… Didn’t we already see those square brackets and parentheses when we define a function?
Lisp is a language where the structure of the code is represented by standard data structures (list, vectors, hash). Repeat after me ! Code as Data. Data as Code. ! ! (defn wont-‐make-‐you-‐blind [name] (println name ", parentheses won't make you blind"))
a function call with collection manipulation functions ! ;; the quotation mark stops Clojure treating the ;; list as a function call (def arguments '(1 2 3)) ! ;; cons is a traditional lisp function ;; that prepends an object to the head of a list (def function-‐call (cons + arguments)) ! (eval function-‐call) ;; => 6 ! !
Malleable Systems ! As well as being able to write code that “writes code” (metaprogramming), you can even change the evaluation semantics by writing macros! We’ll explore this in the workshops.
The flexible of Clojure allows Clojure and your codebase to stretch and bend to your needs. Notable Clojure extensions: ! core.async Implementation of channels and blocks from the Go programming language. core.logic Implementation of miniKanren for doing relational/constraint logic programming. core.typed Optional typing for Clojure.