32

Pune Clojure Course Outline

Embed Size (px)

DESCRIPTION

These are the outline slides that I used for the Pune Clojure Course. The slides may not be much useful standalone, but I have uploaded them for reference.

Citation preview

Page 1: Pune Clojure Course Outline
Page 2: Pune Clojure Course Outline
Page 3: Pune Clojure Course Outline

  Programming Language Nerd

  Co-founder & CTO, Infinitely Beta

  Clojure programmer since the early days

  Curator of Planet Clojure

  Author of “Clojure in Practice” (ETA Sep, 2011)

Page 4: Pune Clojure Course Outline

  History

  Data Structures

  Syntax

  Functions

  Sequences

  Namespaces

  Metadata

  Java Inter-op

  Concurrency

  Multi-methods

  Macros

  Clojure Contrib

Page 5: Pune Clojure Course Outline

  Created by Rich Hickey in 2007

  Open Sourced in 2008

  First large deployment in Jan, 2009

  Second in Apr, same year

  We’ve come a long way since then!

Page 6: Pune Clojure Course Outline

  Programming languages haven’t really changed much

  Creating large-scale, concurrent software is still hard

  Functional Programming rocks

  Lisp is super power

  OO is overrated

  Polymorphism is good

  Multi-core is the future

  VMs are the next-gen platforms

  Ecosystem matters

  Dynamic development

Page 7: Pune Clojure Course Outline

  Numbers 1  3.14  22/7  

  Booleans true  false  

  Strings “foobar”  

  Symbols thisfn  

  Keywords :this  :that  

  RegEx Patterns #“[a-­‐zA-­‐Z0-­‐9]+”  

  Characters \a  \b  \c  

  Comments ;;  Ignore  

  Nothing nil  

Page 8: Pune Clojure Course Outline

  Lists (1  2  3)  (list  “foo”  “bar”  “baz”)  

  Vectors [1  2  3]  (vector  “foo”  “bar”  “baz”)  

  Maps {:x  1,  :y  2}  (hash-­‐map  :foo  1  :bar  2)  

  Sets #{\a  \e  \i  \o  \u}  (hash-­‐set  “cat”  “dog”)  

Page 9: Pune Clojure Course Outline

  There is no other syntax!

  Data structures are the code

  No other text based syntax, only different interpretations

  Everything is an expression (s-exp)

  All data literals stand for themselves, except symbols & lists

Page 10: Pune Clojure Course Outline

  Function calls (function  arguments*)  

(def  hello  (fn  []  “Hello,  world!”))  -­‐>  #’user/hello  (hello)  -­‐>  “Hello,  world!”  

(defn  hello      ([]  (hello  “world”))      ([name]  (str  “Hello,  ”  name  “!”)))  

Page 11: Pune Clojure Course Outline

“It is better to have 100 functions operate on one data structure than 10 functions on 10 data-structures.”

Alan J. Perlis

Page 12: Pune Clojure Course Outline

  An abstraction over traditional Lisp lists

  Provides an uniform way of walking through different data-structures

  Sample sequence functions seq  first  rest  filter  remove  for  partition  reverse  sort  map  reduce  doseq

Page 13: Pune Clojure Course Outline

  Analogous to Java packages, but with added dynamism

  A mapping of symbols to actual vars/classes

  Can be queried and modified dynamically

  Usually manipulated via the ns macro

Page 14: Pune Clojure Course Outline

  Data about data

  Can annotate any symbol or collection

  Mainly used by developers to mark data structures with some special information

  Clojure itself uses it heavily

(def  x  (with-­‐meta  {:x  1}  {:source  :provider-­‐1}))  -­‐>  #’user/x  (meta  x)  -­‐>  {:source  :provider-­‐1}  

Page 15: Pune Clojure Course Outline

  Wrapper free interface to Java

  Syntactic sugar makes calling Java easy & readable

  Core Clojure abstractions are Java interfaces (will change)

  Clojure functions implement Callable & Runnable

  Clojure sequence lib works with Java iterables

  Near native speed

Page 16: Pune Clojure Course Outline

(ClassName.  args*)  (instanceMember  instance  args*)  (ClassName/staticMethod  args*)  ClassName/STATIC_FIELD  

(.toUpperCase  “clojure”)  -­‐>  “CLOJURE”  (System/getProperty  “java.vm.version”)  -­‐>  “16.3-­‐b01-­‐279”  Math/PI  -­‐>  3.14…  (..  System  (getProperties)  (get  “os.name”))  -­‐>  “Mac  OS  X”  

Page 17: Pune Clojure Course Outline

  A technique of doing structural binding in a function arg list or let binding

(defn  list-­‐xyz  [xyz-­‐map]      (list  (:x  xyz-­‐map)  (:y  xyz-­‐map)  (:z  xyz-­‐map)))  

(list-­‐xyz  {:x  1,  :y  2  :z  3})  -­‐>  (1  2  3)  

Page 18: Pune Clojure Course Outline

//  From  Apache  Commons  Lang,  http://commons.apache.org/lang/      public  static  int  indexOfAny(String  str,  char[]  searchChars)  {              if  (isEmpty(str)  ||  ArrayUtils.isEmpty(searchChars))  {                  return  -­‐1;              }              for  (int  i  =  0;  i  <  str.length();  i++)  {                  char  ch  =  str.charAt(i);                  for  (int  j  =  0;  j  <  searchChars.length;  j++)  {                      if  (searchChars[j]  ==  ch)  {                          return  i;                      }                  }              }              return  -­‐1;      }  

Page 19: Pune Clojure Course Outline

(defn  indexed  [coll]  (map  vector  (iterate  inc  0)  coll))  

Page 20: Pune Clojure Course Outline

(defn  indexed  [coll]  (map  vector  (iterate  inc  0)  coll))  

(defn  index-­‐filter  [pred  coll]          (for  [[idx  elt]  (indexed  coll)  :when  (pred  elt)]              idx))  

Page 21: Pune Clojure Course Outline

(defn  indexed  [coll]  (map  vector  (iterate  inc  0)  coll))  

(defn  index-­‐filter  [pred  coll]          (when  pred                (for  [[idx  elt]  (indexed  coll)  :when  (pred  elt)]  idx)))  

(index-­‐filter  #{\a  \e  \i  \o  \o}  "The  quick  brown  fox")  -­‐>  (2  6  12  17)  

(index-­‐filter  #(>  (.length  %)  3)  ["The"  "quick"  "brown"  "fox"])  -­‐>  (1  2)  

Page 22: Pune Clojure Course Outline

for  doseq  if  cond  condp  partition  loop  recur  str  map  reduce   filter   defmacro   apply   comp   complement    defstruct   drop   drop-­‐last   drop-­‐while   format   iterate  juxt   map   mapcat   memoize   merge   partial   partition  partition-­‐all   re-­‐seq   reductions   reduce   remove   repeat  repeatedly  zipmap  

Page 23: Pune Clojure Course Outline

  Simultaneous execution

  Avoid reading; yielding inconsistent data

Synchronous Asynchronous

Coordinated ref  Independent atom   agent  Unshared var  

Page 24: Pune Clojure Course Outline

  Generalised indirect dispatch

  Dispatch on an arbitrary function of the arguments

  Call sequence   Call dispatch function on args to get dispatch value

  Find method associated with dispatch value   Else call default method

  Else error

Page 25: Pune Clojure Course Outline

  Encapsulation through closures

  Polymorphism through multi-methods

  Inheritance through duck-typing

(defmulti  interest  :type)  (defmethod  interest  :checking  [a]  0)  (defmethod  interest  :savings  [a]  0.05)  

(defmulti  service-­‐charge            (fn  [acct]  [(account-­‐level  acct)  (:tag  acct)]))  (defmethod  service-­‐charge  [::Basic  ::Checking]      [_]  25)    (defmethod  service-­‐charge  [::Basic  ::Savings]        [_]  10)  (defmethod  service-­‐charge  [::Premium  ::Checking]  [_]  0)  (defmethod  service-­‐charge  [::Premium  ::Savings]    [_]  0)  

Page 26: Pune Clojure Course Outline

  A facility to extend the compiler with user code

  Used to define syntactic constructs which would otherwise require primitives/built-in support

(try-­‐or      (/  1  0)      (reduce  +  [1  2  3  4])      (partition  (range  10)  2)      (map  +  [1  2  3  4]))    

Page 27: Pune Clojure Course Outline

  clojure.contrib.http.agent

  clojure.contrib.io

  clojure.contrib.json

  clojure.contrib.seq-utils

  clojure.contrib.pprint

  clojure.contrib.string

Page 28: Pune Clojure Course Outline

  Compojure

  ClojureQL

  Incanter

  Leiningen

  FleetDB

  clojure-hadoop

  Cascalog

  Enlive

  Congomongo

  Pallet

  Many more!

Page 29: Pune Clojure Course Outline

  Clojure http://clojure.org

  Clojure group http://bit.ly/clojure-group

  IRC #clojure on irc.freenode.net

  Source http://github.com/clojure

  Wikibook http://bit.ly/clojure-wikibook

Page 30: Pune Clojure Course Outline

http://infinitelybeta.com/jobs/

Page 31: Pune Clojure Course Outline
Page 32: Pune Clojure Course Outline