Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

@ArgesRic WHO AM I? • Ricardo J. Méndez • Entrepreneur, founder, Numergent • Clojure articles at http://numergent.com/articles/ • Twitter: @ArgesRic

Slide 3

Slide 3 text

@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

Slide 4

Slide 4 text

@ArgesRic HOW DID WE GET TO CLOJURE? • First encountered LISP over 20 years ago • Genetic programming! • Fascinated with the language… • Limited tooling and libraries.

Slide 5

Slide 5 text

@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.

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

@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.

Slide 8

Slide 8 text

@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.

Slide 9

Slide 9 text

@ArgesRic THE SCARY BITS

Slide 10

Slide 10 text

@ArgesRic IT WILL SEEM WEIRD

Slide 11

Slide 11 text

@ArgesRic IT WILL SEEM WEIRD • Not because of grammar… • Not because it’s a different vocabulary… • But because it has different semantics.

Slide 12

Slide 12 text

@ArgesRic … BECAUSE OF ITS SEMANTICS • Clojure doesn’t just use different words, • Clojure’s mechanism for making meaning is different.

Slide 13

Slide 13 text

@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)

Slide 14

Slide 14 text

@ArgesRic … WHICH ARE… • Mutability • Inconsistency • Massive syntax

Slide 15

Slide 15 text

@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.

Slide 16

Slide 16 text

@ArgesRic DOCUMENTATION

Slide 17

Slide 17 text

@ArgesRic DOCUMENTATION “the site is fucking useless, its more confusing than helpful” — Radu (Related authority on the learning curve)

Slide 18

Slide 18 text

@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.

Slide 19

Slide 19 text

@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.

Slide 20

Slide 20 text

@ArgesRic DOCUMENTATION, FULL OF EXAMPLES

Slide 21

Slide 21 text

@ArgesRic THEM’S THE SCARY BITS

Slide 22

Slide 22 text

@ArgesRic PROBLEMS THAT CLOJURE SOLVES

Slide 23

Slide 23 text

@ArgesRic SOLVING… • Mutability • Inconsistency • Massive syntax

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

@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.

Slide 26

Slide 26 text

@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.

Slide 27

Slide 27 text

@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.

Slide 28

Slide 28 text

@ArgesRic IMMUTABILITY AND FUNCTIONAL PROGRAMMING

Slide 29

Slide 29 text

@ArgesRic IMMUTABILITY AND FUNCTIONAL PROGRAMMING

Slide 30

Slide 30 text

@ArgesRic IMMUTABILITY AND FUNCTIONAL PROGRAMMING Even assuming you can keep all those side effects in mind… why would you want to?

Slide 31

Slide 31 text

@ArgesRic IMMUTABILITY AND FUNCTIONAL PROGRAMMING

Slide 32

Slide 32 text

@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.

Slide 33

Slide 33 text

@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?

Slide 34

Slide 34 text

@ArgesRic MASSIVE SYNTAX

Slide 35

Slide 35 text

@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.

Slide 36

Slide 36 text

@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.

Slide 37

Slide 37 text

@ArgesRic THE LEARNING PROCESS

Slide 38

Slide 38 text

@ArgesRic NATURAL LANGUAGES • English: Trivial grammar, pronunciation is a mess. • Spanish: straightforward, clean, pronunciation; grammar is more elaborate than English. • Romanian: …

Slide 39

Slide 39 text

@ArgesRic CLOJURE GRAMMAR • Weight is on the verbs. • Verb goes first. • Verb is followed by anything it acts upon.

Slide 40

Slide 40 text

@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))

Slide 41

Slide 41 text

@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))

Slide 42

Slide 42 text

@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.

Slide 43

Slide 43 text

@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

Slide 44

Slide 44 text

@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.

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

@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.

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

@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.

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

@ArgesRic BOOKS Dmitri Sotnikov, Web Development with Clojure

Slide 51

Slide 51 text

@ArgesRic THE CLOJURE WAY

Slide 52

Slide 52 text

@ArgesRic THE CLOJURE WAY • Small, focused, easily replaceable libraries • Templates over frameworks. See Luminus (http:// www.luminusweb.net/) • No automagic

Slide 53

Slide 53 text

@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.

Slide 54

Slide 54 text

@ArgesRic CLOJURE ALL THE WAY DOWN

Slide 55

Slide 55 text

@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.

Slide 56

Slide 56 text

@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.

Slide 57

Slide 57 text

@ArgesRic REPL - CLOJURE, WHILE EXPLORING

Slide 58

Slide 58 text

@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.

Slide 59

Slide 59 text

@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.

Slide 60

Slide 60 text

@ArgesRic CLOJURESCRIPT COMPONENTS:
 REAGENT • React wrapper: https://reagent-project.github.io/ • Clean, straightforward, excellent examples.

Slide 61

Slide 61 text

@ArgesRic CLOJURESCRIPT, REACTIVE:
 RE-FRAME • Reactive pattern using Reagent • https://github.com/Day8/re-frame • Tiny surface area affected when modifying front end code.

Slide 62

Slide 62 text

@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.

Slide 63

Slide 63 text

@ArgesRic FINAL SUGGESTIONS

Slide 64

Slide 64 text

@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/

Slide 65

Slide 65 text

@ArgesRic QUESTIONS?

Slide 66

Slide 66 text

@ArgesRic THANKS!