Skip to content

Instantly share code, notes, and snippets.

@iantruslove
Last active April 9, 2021 01:37
Show Gist options
  • Save iantruslove/860aec352c8271e20827824ae6d1a0d0 to your computer and use it in GitHub Desktop.
Save iantruslove/860aec352c8271e20827824ae6d1a0d0 to your computer and use it in GitHub Desktop.
Den of Clojure - Intro to Clojure part 1

A gentle introduction to Clojure

Lisp Cycles (XKCD 297 / https://xkcd.com/297/)

Ian Truslove

Den of Clojure

2016-04-21


A gentle introduction to Clojure

"Lisp is over half a century old and it still has this perfect timeless air about it". Clojure is a Lisp. It's a dynamic programming language that targets the JVM and JavaScript.

It's also awesome.

In this talk I'll give you a really quick dive into Clojure programming, show you some of the most important language features and tools, and hopefully inspire you to dig a little deeper yourself.


Intro

  • My background
  • It's gonna be quick in places - stop me at any time and ask for clarifications.
  • Links (also at end):

About Clojure

  • It's (primarily) functional
  • It's a Lisp
    • It's not John McCarthy's Lisp
  • Dynamic
  • Things you can do in Clojure / things inspired by Clojure
    • Distributed systems and web services
    • Apache Storm
    • Om - react.js but faster
    • Immutable.js
    • Overtone

Functional Programming


Leiningen


The REPL

  • Read Evaluate Print Loop

Editors

  • A wealth of choices:
  • Must-have features:
    • nREPL integration
    • Syntax highlighting
    • Auto paren matching
    • Structural navigation and editing

The first hurdle

(Source: http://www.thejach.com/view/2011/8/short_guide_to_lisp_syntax)


Hurdle? A curb, surely no more than that...

(Source: http://emacslife.com/how-to-read-emacs-lisp.html)

My advice: read from the inside out.


Data and basic operations

  • Numbers, strings, keywords
  • def and immutability

Collections and basic operations

  • Lists, vectors, maps, sets
  • seq ops
    • first, rest
    • conj
    • for, doseq
    • recursion
  • map ops
    • assoc, dissoc
  • map, filter
  • reduce

Immutable data

  • Data don't change
  • Structural sharing used for efficiency

(source: http://debasishg.blogspot.com/2010/05/grokking-functional-data-structures.html)


Functions

  • fn
  • defn
  • Higher order functions
    • e.g. map takes a function as its second argument

Common Code Structures

  • Threading macros: -> and ->>
  • let - local "variables"
  • if, when, cond
  • Destructuring

Infinite computation

  • Lazy seqs
  • Recursion with (explicit) tail call optimization
    • Example: max of a list of numbers

Organisation

  • Namespaces, vars
  • require to make other code available for use.

Defining "things"

  • plain maps
  • deftype
  • defrecord

Polymorphism

  • Multimethods
    • user-defined dispatch - typically on values
  • Protocols
    • Type-based dispatch
    • Can extend (closed) Java classes to participate

Unit testing


Immutability By Default

  • Design choices making state and concurrency easy to reason about pervade the language.
  • Persistent data structures
    • "Persistent" as in it always preserves previous versions of itself, not as in a database.
  • STM (Software Transactional Memory)

Concurrency and Parallelism

  • Lots of choices:
    • Threads
    • Futures
    • Promises
    • Agents
    • pmap (parallel map)
    • Reducers
    • JVM platform options, e.g. executors
    • core.async

Interop

  • Platform interop for Java and JavaScript

Resources

Websites

Books


More Resources

Other

Conferences

  • Clojure/conj
  • Clojure/west
  • Strange Loop

End

@iantruslove
Copy link
Author

You're welcome - how on earth did you stumble upon this?

@dimovich
Copy link

Was searching on google images for persistent data structures and came across this gist. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment