Clojure_ Functional Con Currency for the JVM Presentation

download Clojure_ Functional Con Currency for the JVM Presentation

of 59

Transcript of Clojure_ Functional Con Currency for the JVM Presentation

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    1/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    2/59

    2009 Formos Software Development

    http://xkcd.com/297/

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    3/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    4/59

    2009 Formos Software Development

    Code is Data

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    5/59

    2009 Formos Software Development

    ReadEvalPrintLoop

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    6/59

    Operating System

    JVM

    Java Libraries

    User Classes

    ClojureRepl Input

    ClojureSource Files

    JavaCompiler

    Source Code

    Evaluator

    2009 Formos Software Development

    There IsNo Interpreter

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    7/59

    2009 Formos Software Development

    Clojure Literals

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    8/59

    2009 Formos Software Development

    Clojure Literals

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    9/59

    2009 Formos Software Development

    Java Interop

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    10/59

    2009 Formos Software Development

    Java Interop

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    11/59

    1

    2

    3

    lst

    2009 Formos Software Development

    Clojure Collections: Lists

    4

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    12/59

    2009 Formos Software Development

    Clojure Collections: Vectors

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    13/59

    2009 Formos Software Development

    Clojure Collections: Map

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    14/59

    2009 Formos Software Development

    Clojure Collections: Sets

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    15/59

    2009 Formos Software Development

    Paul Graham

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    16/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    17/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    18/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    19/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    20/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    21/59

    2009 Formos Software Development

    Functional Programming in Java

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    22/59

    2009 Formos Software Development

    Functional Java Collections

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    23/59

    2009 Formos Software Development

    Functional Clojure Collections

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    24/59

    2009 Formos Software Development

    First Class Functions

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    25/59

    y

    z

    col

    x

    2009 Formos Software Development

    Life without the for loop

    #

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    26/59

    2009 Formos Software Development

    Life without the for loop

    f

    f

    f

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    27/59

    2009 Formos Software Development

    for: list comprehension

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    28/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    29/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    30/59

    2009 Formos Software Development

    Java: Data Encapsulated in Objects

    Person

    firstName: "Howard"lastName: "Lewis Ship"

    age: 42

    Person

    firstName: "Scott"lastName: "Simon"

    age: 44

    Person

    firstName: "Molly"lastName: "Newman"

    age: 29

    Person

    firstName: "David"lastName: "Goldman"

    age: 42

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    31/59

    2009 Formos Software Development

    Clojure: Data in Transformable Collections

    { }:first-name Howard

    :last-name Lewis Ship

    :age 42

    { }:first-name Scott

    :last-name Simon

    :age 44

    { }:first-name Molly

    :last-name Newman

    :age 29

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    32/59

    2009 Formos Software Development

    Paul Graham

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    33/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    34/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    35/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    36/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    37/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    38/59

    2009 Formos Software Development

    Clojure: Software Transactional Memory

    500.

    2500.

    6700.

    2800.

    - 500. + 500. - 300. + 300.

    500.

    2800.

    6700.

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    39/59

    2009 Formos Software Development

    Retries: Transactions Are Speculative

    500.

    2300.

    - 500.

    2500.

    + 500.

    6700.

    - 300.

    + 300.

    500.

    2800.

    6700.6700.

    2800.

    - 300. + 300.

    X

    2500.

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    40/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    41/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    42/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    43/59

    2009 Formos Software Development

    Managing Mutation

    What can change?

    Reference types: atom, var, agent, ref

    When can they change?

    When are changes visible to other threads?

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    44/59

    2009 Formos Software Development

    Atoms

    Shared, Global

    Changes are atomic, synchronous, &non-blocking

    (swap!): Pass value to function yielding new value

    (reset!): Force new value, regardless of existing value

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    45/59

    2009 Formos Software Development

    Vars Per-Thread Mutables

    (def) sets global binding

    (binding) to set up a per-thread override

    (set!) if per-thread binding

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    46/59

    2009 Formos Software Development

    Interfacing with Java APIs

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    47/59

    Function

    Value

    ...

    Namespace Var

    2009 Formos Software Development

    Everything's a Var!

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    48/59

    2009 Formos Software Development

    Functions are stored in Vars

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    49/59

    2009 Formos Software Development

    Agents Single Thread Writes

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    50/59

    2009 Formos Software Development

    Refs Software Transactional Memory

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    51/59

    2009 Formos Software Development

    Concurrency Notes

    All reference types can have a validator

    function

    All refs can have a watcher: an agent notified

    of changes

    (send) inside (dosync) waits until successfulcompletion

    No guarantees when calling Java objects

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    52/59

    2009 Formos Software Development

    Jon Bentley and Doug McIlroy

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    53/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    54/59

    2009 Formos Software Development

    Clojure

    1.0 release: May 4 2009

    Simple, regular syntax

    Improves on Lisp: vectors, maps, sets

    Fully integrates with Java

    Impressive functional & concurrency support

    Manyfeatures not covered here

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    55/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    56/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    57/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    58/59

    2009 Formos Software Development

  • 8/2/2019 Clojure_ Functional Con Currency for the JVM Presentation

    59/59

    F S f D l

    2007 Jon Fife

    http://flickr.com/photos/good-karma/577632972/

    2008 Marcin Wichary

    http://flickr.com/photos/mwichary/2222776430/

    2007 James Manners

    http://flickr.com/photos/jmanners/443421045/

    2008 Daniel Chanhttp://flickr.com/photos/chanchan222/2847443980/

    2005 Jack Keene

    http://www.flickr.com/photos/whatknot/3118124/

    A. Lipson 2003

    http://www.andrewlipson.com/escher/relativity.html

    2007 Alan Chia

    http://flickr.com/photos/seven13avenue/2080281038/

    2007 Woodley Wonderworks

    http://flickr.com/photos/wwworks/2222523486/