Functional programming *isn't* scary

Functional programming *isn't* scary

A talk I gave on the 15th of May at the Computer Science Dept of UCT.


Robert Stuttaford

May 15, 2013


  1. The trouble with having an open mind, of course, is

    that people will insist on coming along and trying to put things in it. Terry Pratchett Wednesday 15 May 13
  2. I’m Robert Stuttaford I like coding, sleep, and thinking about

    thinking Chief Technical at Cognician Talk notes and code here: Wednesday 15 May 13
  3. Functional programming isn’t scary (today’s thing) Wednesday 15 May

  4. So. Just what is it? Quite simply, functional programming

    concerns and facilitates the application and composition of functions. Yes. That’s it. Almost. Wednesday 15 May 13
  5. So. Just what is it? For a language to

    be considered functional, its notion of ‘function’ must be first-class: The functions of a language must be able to be stored, passed, and returned just like any other piece of data within that language. Wednesday 15 May 13
  6. Clojure, a modern Lisp Wednesday 15 May 13

  7. Lisp? Err, what? • From 1958. Yes, 1958. •

    Designed by John McCarthy. Thanks John! • Lisp code is expressed as Lisp data structures. Allows for macros, where you can put code in your code so you can code as you code. This is ‘homoiconicity’. Wednesday 15 May 13
  8. Lisp? Err, what? • Expressed as s-expressions, which represent

    code as a series of nested lists, or tree structures. Wednesday 15 May 13
  9. Lisp? Err, what? • Progenitor of: • If, then,

    else conditional. • Automatic garbage collection. • An astonishing amount of parenthesis. • Didn’t take off because hardware sucked back then. Wednesday 15 May 13
  10. Why is Clojure ‘modern’? • General-purpose, dynamic, functional language.

    • Designed for concurrency - use multiple cores. • Symbiotic with JVM (and CLR, JavaScript). • Just 6 years old. • AWESOME open-source community. Seriously. Wednesday 15 May 13
  11. The Clojure philosophy • Keep it simple. Ok, how?

    • Don’t complect state and behaviour. • Compose small, usually pure functions. • Immutable, persistent data structures. • Be explicit about identity, values, and state. Wednesday 15 May 13
  12. The truth may be out there, but the lies are

    inside your head. Terry Pratchett, Hogfather Wednesday 15 May 13
  13. OOPs vs FP • Defines classes with properties, and methods

    that alter them in place. • Composition requires work: interfaces. Yay. • Hides data away inside type hierarchy. • Noun-centric approach to problem solving. • Defines functions that transform generic data structures. • Composition is simple and natural. • Exposes data. • Verb-centric approach to problem solving. Wednesday 15 May 13
  14. ‘Pure’ functions? • Pure functions are simply functions that

    operate only on their inputs without causing side-effects. • A pure function will always return the same result for the same inputs. • Any time a function alters state or performs I/O, it is impure; it causes side-effects. Wednesday 15 May 13
  15. Functional Composition A demo. But first: who’s read Pratchett?

    Wednesday 15 May 13
  16. Immutable Data Wednesday 15 May 13

  17. Immutable Data Consider this JavaScript: We can’t know that

    for sure! doSomethingWith might have doneSomethingTo our dear witches! Wednesday 15 May 13
  18. Immutable data • In Clojure, locals are immutable. •

    This means, once a value exists, it can not be changed. • You can change what value your local binding pointing at, but you can’t alter that value. • Clojure’s let special form provides the ability to create lexical bindings. Wednesday 15 May 13
  19. Immutable data A let special form demo. Wednesday 15

    May 13
  20. Immutable data • “So, I can’t alter data. How

    do I, you know, alter data?” • Functions return a copy of the data you provide with the transformations applied. • With the capability let provides, you can keep (or discard) successive values of your data as you transform it, without affecting that data for any other consumers of it. Great for concurrency! Wednesday 15 May 13
  21. Immutable data • “Altered copies? Whoa. That must eat

    memory! That’s a performance no-no!” • Clojure’s immutable data structures are persistent; because they are immutable, they can share internal structure! Check it out: Wednesday 15 May 13
  22. Ok, fine. But what about mutable data? Wednesday 15

    May 13
  23. “Look, that's why there's rules, understand? So that you think

    before you break 'em.” Terry Pratchett, Thief of Time Wednesday 15 May 13
  24. Mutable data, or ‘state’ • By using an immutable-first

    data orientation, Clojure forces you to think carefully about how and when your state changes. • When you can’t avoid it, Clojure provides four distinct mechanisms - the reference types - which allow changing state in a controlled manner. Wednesday 15 May 13
  25. In Summary • Functional programming is objectively simpler than

    OOPs, and is therefore easier to do. • Clojure is a great modern FP language that you can use for just about anything. • Cats are awesome. And bacon. Bacon is awesome. Wednesday 15 May 13
  26. Phew. Ok. I’m done! Any questions? :-) Wednesday 15

    May 13
  27. Cape Town Clojure User Group • We’re having a

    meet this Saturday, May 18th from 9am at • We’ll properly introduce Clojure, live-code a small web app, and take a look at ClojureScript and Datomic - a new database built on the same principles as Clojure! • Come join us! • Follow @clj_ug_ct on Twitter or visit Wednesday 15 May 13