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

Clojure all the way down: Finally a useful LISP

Clojure all the way down: Finally a useful LISP

Bucharest FP

October 21, 2015
Tweet

More Decks by Bucharest FP

Other Decks in Programming

Transcript

  1. @ArgesRic
    CLOJURE ALL THE WAY DOWN
    Ricardo J. Méndez

    View Slide

  2. @ArgesRic
    WHO AM I?
    • Ricardo J. Méndez

    • Entrepreneur, founder, Numergent

    • Clojure articles at http://numergent.com/articles/

    • Twitter: @ArgesRic

    View Slide

  3. @ArgesRic
    WHAT DO I DO?
    • CTO for hire running projects for large companies

    • Working on our own projects (open source and otherwise)

    • Previously:

    • Data analysis for banking, healthcare

    • Game development

    View Slide

  4. @ArgesRic
    HOW DID WE GET TO CLOJURE?
    • First encountered LISP over 20 years ago

    • Genetic programming!

    • Fascinated with the language…

    • Limited tooling and libraries.

    View Slide

  5. @ArgesRic
    HOW DID WE GET TO CLOJURE?
    • It was different.

    • Had access to the full Java platform…

    • …while still giving you a fully functional approach.

    • New paradigms shake up your thought process.

    View Slide

  6. @ArgesRic
    OTHER OPTIONS?
    (Event though it aims to solve the same problems as Clojure)

    View Slide

  7. @ArgesRic
    WHAT WE’LL TALK ABOUT
    • Not a language intro.

    • For a great one, see Rich Hickey’s “Clojure, Made Simple” https://
    www.youtube.com/watch?v=VSdnJDO-xdg

    • Focus on how design decisions affect your work.

    • Won’t be overloaded with code samples.

    View Slide

  8. @ArgesRic
    BIASES
    • Somewhat personal talk - will focus on why it works for me.

    • I’ve always found the elegance of LISPs appealing.

    • SPOILER ALERT: Unapologetically positive about it…

    • … so we’ll start with the scary bits.

    View Slide

  9. @ArgesRic
    THE SCARY BITS

    View Slide

  10. @ArgesRic
    IT WILL SEEM WEIRD

    View Slide

  11. @ArgesRic
    IT WILL SEEM WEIRD
    • Not because of grammar…

    • Not because it’s a different vocabulary…

    • But because it has different semantics.

    View Slide

  12. @ArgesRic
    … BECAUSE OF ITS SEMANTICS
    • Clojure doesn’t just use different words,

    • Clojure’s mechanism for making meaning is different.

    View Slide

  13. @ArgesRic
    … BECAUSE OF THE PROBLEMS IT
    SOLVES
    “We can't solve problems by using the same kind of
    thinking we used when we created them.”

    — Albert Einstein
    (Totally not an appeal to unrelated authority)

    View Slide

  14. @ArgesRic
    … WHICH ARE…
    • Mutability

    • Inconsistency

    • Massive syntax

    View Slide

  15. @ArgesRic
    DOCUMENTATION
    into
    (into to from) (into to xform from)

    Returns a new coll consisting of to-coll with all of the
    items of from-coll conjoined. A transducer may be
    supplied.

    View Slide

  16. @ArgesRic
    DOCUMENTATION

    View Slide

  17. @ArgesRic
    DOCUMENTATION
    “the site is fucking useless, its more confusing than
    helpful”

    — Radu
    (Related authority on the learning curve)

    View Slide

  18. @ArgesRic
    DOCUMENTATION
    • Doesn’t tell you that you can ignore transducers for now.

    • Doesn’t tell you that there’s a conj verb.

    • Doesn’t tell you anything else that might help a newbie.

    View Slide

  19. @ArgesRic
    DOCUMENTATION
    • It's going to take one collection, and move it into a different collection by
    conjoining items to it.

    • Why conjoining?

    • conjoin as a verb has a specific meaning in Clojure.

    View Slide

  20. @ArgesRic
    DOCUMENTATION, FULL OF EXAMPLES

    View Slide

  21. @ArgesRic
    THEM’S THE SCARY BITS

    View Slide

  22. @ArgesRic
    PROBLEMS THAT CLOJURE SOLVES

    View Slide

  23. @ArgesRic
    SOLVING…
    • Mutability

    • Inconsistency

    • Massive syntax

    View Slide

  24. @ArgesRic
    IMMUTABILITY AND FUNCTIONAL
    PROGRAMMING
    Show of hands… how many are doing functional
    programming?

    View Slide

  25. @ArgesRic
    IMMUTABILITY AND FUNCTIONAL
    PROGRAMMING
    • Functions are not methods by a different name.

    • Take values, returns a value…

    • … but doesn’t change anything in the middle.

    View Slide

  26. @ArgesRic
    IMMUTABILITY AND FUNCTIONAL
    PROGRAMMING
    • C# has LINQ, which sorta gets you there…

    • Java is finally catching up…

    • … but neither enforces immutability.

    • You’re still juggling objects. Some will methods will change properties.

    View Slide

  27. @ArgesRic
    IMMUTABILITY AND FUNCTIONAL
    PROGRAMMING
    • That’s fine when you have a couple classes.

    • Maybe you can keep the side effects in mind as you code.

    • Maybe you know what that ProcessTransactions() returns a value, but
    changes a couple of things…

    • … but we are fallible, with finite mindspace.

    View Slide

  28. @ArgesRic
    IMMUTABILITY AND FUNCTIONAL
    PROGRAMMING

    View Slide

  29. @ArgesRic
    IMMUTABILITY AND FUNCTIONAL
    PROGRAMMING

    View Slide

  30. @ArgesRic
    IMMUTABILITY AND FUNCTIONAL
    PROGRAMMING
    Even assuming you can keep all those side effects in
    mind…

    why would you want to?

    View Slide

  31. @ArgesRic
    IMMUTABILITY AND FUNCTIONAL
    PROGRAMMING

    View Slide

  32. @ArgesRic
    IMMUTABILITY AND PARALLELISM
    • Immutability makes parallelism much simpler.

    • Look ma, no locks!

    • You don’t care where it runs, since nobody will modify the data.

    • Had to do parallelism by hand before. Never going back.

    View Slide

  33. @ArgesRic
    MASSIVE SYNTAX
    • Bigger problem than unfamiliar semantics.

    • You only need to learn semantics once, syntax is an on-going cost.

    • Again… it uses up headspace.

    • Would you rather use it for the syntax, or for the concepts?

    View Slide

  34. @ArgesRic
    MASSIVE SYNTAX

    View Slide

  35. @ArgesRic
    MASSIVE SYNTAX
    • Code is read more than it is written.

    • We need to convey meaning quickly, without unnecessary ceremony…

    • Doesn’t matter if it’s a new programmer, or you six months after you
    wrote it.

    • And that’s before we discuss the baroque class architectures people
    build on top.

    View Slide

  36. @ArgesRic
    INCONSISTENCY
    • You need to experience it.

    • Clojure thinks in terms of collections and encourages you to use
    maps.

    • Same functions act over everything. Even records are map-like.

    View Slide

  37. @ArgesRic
    THE LEARNING PROCESS

    View Slide

  38. @ArgesRic
    NATURAL LANGUAGES
    • English: Trivial grammar, pronunciation is a mess.

    • Spanish: straightforward, clean, pronunciation; grammar is more
    elaborate than English.

    • Romanian: …

    View Slide

  39. @ArgesRic
    CLOJURE GRAMMAR
    • Weight is on the verbs.

    • Verb goes first.

    • Verb is followed by anything it acts upon.

    View Slide

  40. @ArgesRic
    CLOJURE GRAMMAR
    (+ 1 2 3 4)

    (map :val [{:id 1 :val "a"} {:id 2 :val "b"} {:id 3 :val "something else”}])

    (auth/create-auth-token (:username content) (:password
    content))

    View Slide

  41. @ArgesRic
    CLOJURE GRAMMAR
    (+ 1 2 3 4)

    (map :val [{:id 1 :val "a"} {:id 2 :val "b"} {:id 3 :val "something else”}])

    (auth/create-auth-token (:username content) (:password
    content))

    View Slide

  42. @ArgesRic
    MIND THE SEMANTICS, THOUGH
    • It’s not about learning a new function now and then.

    • You have to use it to get acquainted with the semantics.

    • Need to learn how to express yourself differently.

    • It’s not about understanding the language, it’s about grokking its
    semantics.

    View Slide

  43. @ArgesRic
    MIND THE SEMANTICS, THOUGH
    grok:

    “to understand so thoroughly that the observer
    becomes a part of the observed”

    — Robert Heinlein, Stranger in a Strange Land

    View Slide

  44. @ArgesRic
    IT’S ABOUT DOING
    You can’t just subscribe to a running magazine, you
    have to get out there and run.
    4Clojure.com is your friend.

    View Slide

  45. @ArgesRic
    LEARNING CURVE
    “I am to going to fetch the value..”

    View Slide

  46. @ArgesRic
    LEARNING CURVE
    “I am to going to fetch the value..”
    They’re trying to hang the mental constructs of their
    much more complex first language onto the
    simplicity of English grammar.

    View Slide

  47. @ArgesRic
    LEARNING CURVE
    The tricky thing about Clojure’s grammar is that it’s
    much simpler than you expect.

    View Slide

  48. @ArgesRic
    LEARNING CURVE
    • Most of my time goes to figuring out what I want to say.

    • In C# or Java, I spent a lot of time in scaffolding and ceremony before
    getting to that.

    • There’s no boilerplate. It’s just you and what you want to calculate.

    View Slide

  49. @ArgesRic
    BOOKS
    Daniel Higginbotham, Clojure for the
    Brave and True

    View Slide

  50. @ArgesRic
    BOOKS
    Dmitri Sotnikov, Web Development
    with Clojure

    View Slide

  51. @ArgesRic
    THE CLOJURE WAY

    View Slide

  52. @ArgesRic
    THE CLOJURE WAY
    • Small, focused, easily replaceable libraries

    • Templates over frameworks. See Luminus (http://
    www.luminusweb.net/)

    • No automagic

    View Slide

  53. @ArgesRic
    NO AUTOMAGIC
    • No method injection… since we don’t even have objects.

    • I used to love Grails’ approach… until I realized I couldn’t control it.

    • Full control over what’s going on.

    • Libraries are easy to replace, since you don’t have to wonder about
    what goes on behind the scenes.

    View Slide

  54. @ArgesRic
    CLOJURE ALL THE WAY DOWN

    View Slide

  55. @ArgesRic
    REFACTORING - CLOJURE, REVISITED
    • Immutability helps with refactoring.

    • Code is trivial to manipulate, since…

    • … it’s a tree structure

    • … you don’t need to worry about context.

    View Slide

  56. @ArgesRic
    REFACTORING - CLOJURE, REVISITED
    • Yes, code is read more than it is written…

    • … but writing is rewriting.

    • We will not get it perfect the first time.

    View Slide

  57. @ArgesRic
    REPL - CLOJURE, WHILE EXPLORING

    View Slide

  58. @ArgesRic
    REPL - CLOJURE, WHILE EXPLORING
    • Trivial use of your functions as you develop.

    • Excellent for exploring someone else’s functions (remember, no
    context!)

    • I’ve found myself experimenting with my new functions on the REPL,
    then codifying those as tests.

    View Slide

  59. @ArgesRic
    CLOJURESCRIPT - CLOJURE, 

    ON THE BROWSER
    • Gets transpiled to Javascript.

    • All of Clojure’s advantages, on the browser.

    • Straightforward and consistent.

    • Both front and backend on the same code.

    • Enables some amazing tools.

    View Slide

  60. @ArgesRic
    CLOJURESCRIPT COMPONENTS:

    REAGENT
    • React wrapper: https://reagent-project.github.io/

    • Clean, straightforward, excellent examples.

    View Slide

  61. @ArgesRic
    CLOJURESCRIPT, REACTIVE:

    RE-FRAME
    • Reactive pattern using Reagent

    • https://github.com/Day8/re-frame

    • Tiny surface area affected when modifying front end code.

    View Slide

  62. @ArgesRic
    CLOJURESCRIPT, RELOADED:

    FIGWHEEL
    • Created by Bruce Hauman

    • https://github.com/bhauman/lein-figwheel

    • Scans ClojureScript source folders, reloads the application live.

    • “Presses F5 for you, big whoop”

    • No data loss.

    View Slide

  63. @ArgesRic
    FINAL SUGGESTIONS

    View Slide

  64. @ArgesRic
    FINAL SUGGESTIONS
    • Start off small. Unlike going from C# to Java, it’s not just about learning new
    vocabulary.

    • There will be a learning curve.

    • Don’t get too used to blindly hack off templates at first just because you want
    to start big.

    • 5 mistakes Clojure newbies make: http://adambard.com/blog/five-mistakes-
    clojure-newbies-make/

    View Slide

  65. @ArgesRic
    QUESTIONS?

    View Slide

  66. @ArgesRic
    THANKS!

    View Slide