Upload
metosin
View
413
Download
7
Embed Size (px)
DESCRIPTION
Tech Talk slides from 17.10.
Citation preview
Clojure in Real Life���Tommi Reiman / Metosin
17.10.2014
Agenda • Clojure basics • In Real Life • Getting started • Q & A
Clojure Basics
Clojure • Clojure is a dynamic, functional, general purpose language
– Compiled, strong (dynamic) typing • with optional static typing (core.typed)
– Targets JVM, CLR and JavaScript – Immutable, persistent data structures – Interactive development (REPL) – Concurrency, software transactional memory & core.async – Pragmatism over purism – Runtime polymorphism – A lisp, meta-programming with macros
Syntax
Syntax
• Heterogeneous read-only data structures • All data literals stand for themselves, except Lists & Symbols
Simple program
Immutable, persistent Data
Structural sharing
h)p://eclipsesource.com/blogs/2009/12/13/persistent-‐trees-‐in-‐git-‐clojure-‐and-‐couchdb-‐data-‐structure-‐convergence/
Macros • Macros are evaluated at compile-time (to generate code) • One can also (eval) code at runtime è Great tools for reducing boilerplate & do concise DSLs
unless?
Business DSL
Java Interoperability
math speed / hiphip (array)!
Concurrency • Immutable data structures,
pure functions • State & Identity separation
via References – Vars, Refs, Atoms, Agents
• futures, promises, delays • Libraries: Avout, Lamina,
Pulsar, Onyx, (Akka) • core.async
STM example
h)p://sw1nn.com/blog/2012/04/11/clojure-‐stm-‐what-‐why-‐how/
core.async • Library for channel-based async processing – Channels, go-blocks, transducers
• Communicating sequential processes • https://golang.org/ • Both for threaded (JVM) & non-threaded (JS)
environments – http://swannodette.github.io/2013/08/02/100000-processes/
core.async, example • Boiling Sous-Vide Eggs using Clojure's Transducers
– http://blog.eikeland.se/2014/10/06/pid-transducer/?utm_content=buffer75806
Didn’t cover • Protocols, Multimethods • Destructuring • Dynamic binding • Walkers • Records • Metadata
Missed these too • core.match – pattern matching • core.typed – gradual (static) typing • core.logic – logic programming
Ok, this is just another academic…
In real life
Leiningen • Declarative build tool, uses Maven internally
Testing • clojure.test (bundled), Midje (TDD/BDD), expectations • Clj-webdriver (browser tests) • test.check for generative tests • Leiningen plugins to generate (junit-)reports, run coverage etc.
Integration testing
Editors • We (6) are currently using: – IDEA + Cursive – Eclipse + CCW – Vim + Fireplace – Emacs + Cider – LightTable
• Not yet as good as the Java tools, but getting better every day
REPL-driven workflow • Evaluate code as you go, instant feedback – Develop in Editor, evaluate in REPL – Ridiculously fast (single-pass) compiler
• Massive boost in productivity • nRepl-servers • Browser-Repls
Web stacks • Mostly micro-libraries on top of Ring
– Request, Response, Adapters, Middlewares, Handlers – https://github.com/ring-clojure/ring
• Packaged either : – standalone web servers (uberjar) – As Servlet Apps (and deploy as wars)
• Some popular libs – Compojure (https://github.com/weavejester/compojure) – Liberator (http://clojure-liberator.github.io/liberator/) – Pedestal (https://github.com/pedestal/pedestal)
• Lot’s of support libs (clostache, hiccup, enlive, …)
Simple web server
Schema Sausage?
Sc elements
Demo: a REST API • with Compojure-api & Ring-Swagger
... something like this
Don’t trust the benchmark
h)p-‐kit 137k
Compojure 123k
Scalatra 107k
Play 98k
Spring 42k
Go 211k
h)p://www.techempower.com/benchmarks/#secKon=data-‐r9&hw=i7&test=json
Async web - Scala & Clojure
h)p://www.slideshare.net/colinlogaan/clojure-‐at-‐a-‐post-‐office-‐36333973
Persistence (of time) • Idiomatic wrappers on top of Java Libraries – RDB: Clojure.java.jdbc, Yesql, Korma – Monger (MongoDB), Welle (Riak), Cassaforte
(Cassandra), Neocons (Neo4j), Carmine (Redis), …
• Datomic
Demo
MongoDB sample
Utils • Use Java libraries or idiomatic clj-wrappers on
top of them – clj-time, emailer, clj-ssh, timbre (logging) & tons of other
goodnesses
Data formats • JSON maps 1:1 to Clojure – All core functions available, no need for external DSLs
• XML, lazy parsers, zippers, selectors • Excel, use Apache POI directly • EDN, Transit • …
Demo
Clojurescript • Clojure compiled into JavaScript – No threads, some different implementation classes
• Google Closure compiler • Browser-repls (http://clojurescript.net/) – Same dynamic development flow as with Clojure
Demo: Flappy Bird
h)ps://github.com/bhauman/flappy-‐bird-‐demo
OM / Reagent • A ClojureScript interface to Facebook's React.js
Game of Life
h)p://clj-‐me.cgrand.net/2011/08/19/conways-‐game-‐of-‐life/
Einstein / Zebra
h)ps://github.com/swannode)e/logic-‐tutorial
Sudoku Solver
h)ps://gist.github.com/swannode)e/3217582
So, do people use it?
Thoughtworks Technology Radar 2014
Scala, adopt!
Where’s my
clojure?
In Finland? • Zenrobotics – “the largest clojure codebase in the world”?
• Metosin – Internally, for small & large enterprises, Finnish Authorities
(with friends) & abroad – Training for others (Solita, Gofore, Futurice & others)
• Solita (with friends) • Others • (lots of startups)
:stats (1.5y project, 5+ developers)
Flushing the Brain • (Mutable) Objects
à Immutable Data, (pure) functions, references • Design Patterns
à mostly not needed (builder, strategy, visitor,…), new functional design patterns • Domain Driven Design
à Anemic Domain Model is a Functional Domain Model • TDD
à RDD (with tests) • Dependency Injection
à Thought not needed, but still good for large apps (via Stuart Sierra’s Component-library, collecting app state into one place)
Goods and Bads • Good
– Simple and beautiful code – Awesome Development speed – Great & friendly ecosystem (libs, people & support) – The JVM Backup (can use Java anywhere, performance) – Clojurescript(!!!) – Extensibility (mini languages on top)
• Bad – Startup time – CLJS Build tools (vs Gulp) – Refactoring in large projects (with current IDE support) – 10% times, missing the static type system
I would use Clojure for… • Everything, replacing both Java & Scala! • Just kidding, not for : – Android development (object apis, startup) à Scala/Java – Command line apps (slow startup) à whatever – Maybe not for distributed computing à Spark/Akka/Scala
• But Clojure for mostly everything else.
Clojure @ Metosin • Open source – Ring-Swagger & Compojure-api (Swagger 2.0) – kekkonen.io (a snappy CQRS-lib) – Lein-simulflow
• Community – Tech talks, seminars, dojos
• Projects & Training • Fun stuff
Getting started
Some resources • Books: – Clojure Programming – The Joy of Clojure, 2nd
• Online – 4clojure (https://www.4clojure.com/) – ClojureDocs (http://clojuredocs.org/) – FP with Clojure (http://mooc.cs.helsinki.fi/clojure)
FP Seminar at TTY
ClojuTRE 2014 • Third annual Clojure seminar at Tampere 25.11.2014
– Free beer, pizza & brackets – Follow @metosin for updates
h)p://clojutre.org/2014/
Thanks. Tommi Reiman / @ikitommi
http://www.metosin.fi