Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

More Decks by Robert Stuttaford

Other Decks in Programming


  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: http://goo.gl/b9JkF Wednesday 15 May 13
  3. http://goo.gl/b9JkF 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
  4. http://goo.gl/b9JkF 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
  5. http://goo.gl/b9JkF 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
  6. http://goo.gl/b9JkF Lisp? Err, what? • Expressed as s-expressions, which represent

    code as a series of nested lists, or tree structures. Wednesday 15 May 13
  7. http://goo.gl/b9JkF 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
  8. http://goo.gl/b9JkF 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
  9. http://goo.gl/b9JkF 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
  10. The truth may be out there, but the lies are

    inside your head. Terry Pratchett, Hogfather http://goo.gl/b9JkF Wednesday 15 May 13
  11. 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. http://goo.gl/b9JkF Wednesday 15 May 13
  12. http://goo.gl/b9JkF ‘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
  13. http://goo.gl/b9JkF Immutable Data Consider this JavaScript: We can’t know that

    for sure! doSomethingWith might have doneSomethingTo our dear witches! Wednesday 15 May 13
  14. http://goo.gl/b9JkF 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
  15. http://goo.gl/b9JkF 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
  16. http://goo.gl/b9JkF 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
  17. “Look, that's why there's rules, understand? So that you think

    before you break 'em.” Terry Pratchett, Thief of Time http://goo.gl/b9JkF Wednesday 15 May 13
  18. http://goo.gl/b9JkF 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
  19. http://goo.gl/b9JkF 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
  20. http://goo.gl/b9JkF Cape Town Clojure User Group • We’re having a

    meet this Saturday, May 18th from 9am at codebridge.co.za. • 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 http://www.siliconcape.com/group/clojure-ct Wednesday 15 May 13