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

More Decks by Bucharest FP

Other Decks in Programming


  1. @ArgesRic WHO AM I? • Ricardo J. Méndez • Entrepreneur,

    founder, Numergent • Clojure articles at http://numergent.com/articles/ • Twitter: @ArgesRic
  2. @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
  3. @ArgesRic HOW DID WE GET TO CLOJURE? • First encountered

    LISP over 20 years ago • Genetic programming! • Fascinated with the language… • Limited tooling and libraries.
  4. @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.
  5. @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.
  6. @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.
  7. @ArgesRic IT WILL SEEM WEIRD • Not because of grammar…

    • Not because it’s a different vocabulary… • But because it has different semantics.
  8. @ArgesRic … BECAUSE OF ITS SEMANTICS • Clojure doesn’t just

    use different words, • Clojure’s mechanism for making meaning is different.

    solve problems by using the same kind of thinking we used when we created them.” — Albert Einstein (Totally not an appeal to unrelated authority)
  10. @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.
  11. @ArgesRic DOCUMENTATION “the site is fucking useless, its more confusing

    than helpful” — Radu (Related authority on the learning curve)
  12. @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.
  13. @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.
  14. @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.

    sorta gets you there… • Java is finally catching up… • … but neither enforces immutability. • You’re still juggling objects. Some will methods will change properties.
  16. @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.
  17. @ArgesRic IMMUTABILITY AND FUNCTIONAL PROGRAMMING Even assuming you can keep

    all those side effects in mind… why would you want to?
  18. @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.
  19. @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?
  20. @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.
  21. @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.
  22. @ArgesRic NATURAL LANGUAGES • English: Trivial grammar, pronunciation is a

    mess. • Spanish: straightforward, clean, pronunciation; grammar is more elaborate than English. • Romanian: …
  23. @ArgesRic CLOJURE GRAMMAR • Weight is on the verbs. •

    Verb goes first. • Verb is followed by anything it acts upon.
  24. @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))
  25. @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))
  26. @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.
  27. @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
  28. @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.
  29. @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.
  30. @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.
  31. @ArgesRic THE CLOJURE WAY • Small, focused, easily replaceable libraries

    • Templates over frameworks. See Luminus (http:// www.luminusweb.net/) • No automagic
  32. @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.
  33. @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.
  34. @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.
  35. @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.

    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.
 RE-FRAME • Reactive pattern using Reagent •

    https://github.com/Day8/re-frame • Tiny surface area affected when modifying front end code.
 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.
  39. @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/