230 likes | 311 Views
A review of the Clojure Programming Language. Full Disclojure. Background – who, when, and why. Overview of Language Design Names, Binding and Scope Data Structures Expression and Assignment Control Structures Subprograms Abstraction/Encapsulation. Outlining Clojure.
E N D
A review of the Clojure Programming Language Full Disclojure
Background – who, when, and why. • Overview of Language Design • Names, Binding and Scope • Data Structures • Expression and Assignment • Control Structures • Subprograms • Abstraction/Encapsulation Outlining Clojure
Object Orientation • Concurrency • Exceptions and Events • Evaluation of Clojure • Readability • Writability • Reliability • Cost • Discussion Outlining Clojure (Cont’…)
Author Rich Hickey first released in 2007. Essentially a unified JVM-based LISP. Supposed to make programming, specifically with regard to concurrency, easier. Aims at combatting the perceived overuse of object-orientation and its implications. Pragmatism trumps idealism: Not purely functional, OO borrowed from where useful. Clojure’s story
Slightly different naming scheme • As usual case-sensitive and no leading numeric • Alphanumerics, “+”, “-”, “*”, “/”, “!”, “?”, “.”, “_” • Everything is a symbol (details later) • Scope is lexical with ‘let’, dynamic with ‘bind’. • Var is Clojure’s variable (but not really). • Keywords always evaluate to themselves. Names, Bindings, and Scope
Data structures are immutable (no changes). “Nil” is Java Null, representing all of nothing. False is either “false” or “nil”, all else is true. Has Java primitives plus the Ratio Type. Operators don’t autopromote overflowing values, but they have complements that do. Coercion is widening (as with Java). Data Structures
Collections: lists, vectors, array-maps, sets. • Count gets the collection size • Conj adds to the collection • Seq sequences the collection according to its type (different behaviors, but all use it) • Supports set operations and some relations. Data Structures (Cont’…)
Forms are Clojure Expressions. Parenthetical prefix notation using forms. Leftmost form is the function, while the remainder are arguments of the function. No declarations – Clojure is Dynamic No interpreter required: items are examined, and if needed compiled and evaluated. Expression and Assignment
Evaluation resolves literals and keywords to themselves, and symbols to their value. • Symbol resolution is path-dependent • Namespace-qualified gives its global value • Package-qualified symbol gives it Java-class • Special forms get applied with their rules. • Class-mapping, local binding, var, are checked. • Nonexistence will generate an error. Expression and Assignment (Cont’…)
The form comprises everything. • Literals are forms that are their own values. • Symbols are the next lowest, being nonliterals and nonsyntactic characters. • Composites are defined by enclosing brackets and represent vectors, maps, or lists. • Special forms are composites whose definitions are built directly into Clojure. • Macros are used to extend code internally. Expression and Assignment (Cont’…)
“do” executes a sequence of forms in order. “if” takes the comparison, the true-return, and the optional else-return as parameters. “if-not” is a macro inverting the “if” special form. “when” is a macro of “if” with an implicit “do”. “when-not” is an inversion of “when”. “cond” approximates if-elseif structure. “condp” approximates the switch structure. Control Structures
There are no loops in Clojure. Recursion is the only way to iterate. A function call containing a control statement allows recursion to emulate typical looping. Unbounded or infinite loops are achievable using recur to eliminate memory growth, but it must be explicitly called. Control Structures (Cont’…)
Clojure programs are built up like trees from functions as nodes and values as leaves. Invites too-clever coding and the associated inscrutable maladies arising therefrom. All functions are first-class objects, so they can appear as values to other functions. Functions needn’t be predefined for use. OO behavior can be achieved with layers. Subprograms
Record, Protocol, and Type enable custom data-structure definition. Records assign a type to a map or hash-map using “defrecord” to specify the type. Protocols resemble Java interfaces, and are implemented using “extends”. Types are specified with “deftype” which is essentially “defrecord” sans implementation. Abstraction/Encapsulation
Macros take unevaluated arguments and instead of values return forms. This allows direct manipulation of code within the program, by the program itself. Since Clojure is homoiconic (i.e. data and code have identical structuring) , this enables the powerful metaprogramming for which LISP is known. Abstraction/Encapsulation (Cont’…)
Clojure is Anti-OOP, in that it attempts to deobjectify programming. Polymorphism is maintained, but decoupled from the state-and-behavior object model. The desirable OO tools are inherently available, and object-equivalent structures can be synthesized as needed. Ref and Agent allow for identity modeling. Object Orientation
Clojure’s mutable state is rarely-needed evil. It uses Software Transactional Memory (STM) to eliminate concurrency issues. Locks, semaphores, and mutexes aren’t studied, they are automatic to the language. Concurrent operations are given snapshots, each does its work, and then changes are merged back together, retrying as needed. Concurrency
Reference types: Ref, Agent, Atom, and Var A “ref” requires interaction within transaction. An “agent” value is handed to a function, and the return is then assigned to the agent. An “atom” is based on java.util.atomic, the classes aren’t race or locking sensitive. A “var” is a global variable used for tracking runtime settings and the like. Concurrency (Cont’…)
Multimethods are Clojure’s prime polymorphism construct. The idea is that a function designator calls the appropriate function based on the received value. The equivalent of normal object behavior would have the value equal “this”. Implementation inheritance is unsupported. Concurrency (Cont’…)
Java requires explicit exception handling, and its design works down into the JVM. Clojure doesn’t require checked exception handling by the programmer, but must deal with the JVM requirements. To cope, all functions throw root (Runtime) Exception, and the program passes exceptions out from the caller. Exceptions and Events
Naturally, Java’s event-handlers were designed to deal with mutable state. Dealing with this uses dynamic bindings. Since the handlers are typically single-threaded, this rarely threatens concurrency. Several GUI frameworks for Clojure exist, perhaps most notable Seesaw. Exceptions and Events (Cont’…)
Readability benefits from the syntactic sugar, but suffers from the unfamiliar LISP style. Writability also benefits from brevity, but again the alien qualities hinder this as well. Reliability is benefitted by the JVM, pace of development, and Clojure’s general ideals. Cost is lessened by having the JVM, but the learning curve can be a training obstacle. Evaluating Clojure
Thank you for your time, patience, or interest (whichever you happen to have). Coming up next: An implementation of a boundary-free “Game of Life”. Before we go to code, any questions first? Discussion