Clojure all the way down: Finally a useful LISP

Clojure all the way down: Finally a useful LISP

0286822f506fc4621bd3ea0bcbfef238?s=128

Bucharest FP

October 21, 2015
Tweet

Transcript

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

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

    founder, Numergent • Clojure articles at http://numergent.com/articles/ • Twitter: @ArgesRic
  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
  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.
  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.
  6. @ArgesRic OTHER OPTIONS? (Event though it aims to solve the

    same problems as Clojure)
  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.
  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.
  9. @ArgesRic THE SCARY BITS

  10. @ArgesRic IT WILL SEEM WEIRD

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

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

    use different words, • Clojure’s mechanism for making meaning is different.
  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)
  14. @ArgesRic … WHICH ARE… • Mutability • Inconsistency • Massive

    syntax
  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.
  16. @ArgesRic DOCUMENTATION

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

    than helpful” — Radu (Related authority on the learning curve)
  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.
  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.
  20. @ArgesRic DOCUMENTATION, FULL OF EXAMPLES

  21. @ArgesRic THEM’S THE SCARY BITS

  22. @ArgesRic PROBLEMS THAT CLOJURE SOLVES

  23. @ArgesRic SOLVING… • Mutability • Inconsistency • Massive syntax

  24. @ArgesRic IMMUTABILITY AND FUNCTIONAL PROGRAMMING Show of hands… how many

    are doing functional programming?
  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.
  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.
  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.
  28. @ArgesRic IMMUTABILITY AND FUNCTIONAL PROGRAMMING

  29. @ArgesRic IMMUTABILITY AND FUNCTIONAL PROGRAMMING

  30. @ArgesRic IMMUTABILITY AND FUNCTIONAL PROGRAMMING Even assuming you can keep

    all those side effects in mind… why would you want to?
  31. @ArgesRic IMMUTABILITY AND FUNCTIONAL PROGRAMMING

  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.
  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?
  34. @ArgesRic MASSIVE SYNTAX

  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.
  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.
  37. @ArgesRic THE LEARNING PROCESS

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

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

    Verb goes first. • Verb is followed by anything it acts upon.
  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))
  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))
  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.
  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
  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.
  45. @ArgesRic LEARNING CURVE “I am to going to fetch the

    value..”
  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.
  47. @ArgesRic LEARNING CURVE The tricky thing about Clojure’s grammar is

    that it’s much simpler than you expect.
  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.
  49. @ArgesRic BOOKS Daniel Higginbotham, Clojure for the Brave and True

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

  51. @ArgesRic THE CLOJURE WAY

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

    • Templates over frameworks. See Luminus (http:// www.luminusweb.net/) • No automagic
  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.
  54. @ArgesRic CLOJURE ALL THE WAY DOWN

  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.
  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.
  57. @ArgesRic REPL - CLOJURE, WHILE EXPLORING

  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.
  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.
  60. @ArgesRic CLOJURESCRIPT COMPONENTS:
 REAGENT • React wrapper: https://reagent-project.github.io/ • Clean,

    straightforward, excellent examples.
  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.
  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.
  63. @ArgesRic FINAL SUGGESTIONS

  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/
  65. @ArgesRic QUESTIONS?

  66. @ArgesRic THANKS!