Clojure in real life 17.10.2014

62
Clojure in Real Life Tommi Reiman / Metosin 17.10.2014

description

Tech Talk slides from 17.10.

Transcript of Clojure in real life 17.10.2014

Page 1: Clojure in real life 17.10.2014

Clojure in Real Life���Tommi Reiman / Metosin  

17.10.2014  

Page 2: Clojure in real life 17.10.2014

Agenda •  Clojure basics •  In Real Life •  Getting started •  Q & A

Page 3: Clojure in real life 17.10.2014

Clojure Basics

Page 4: Clojure in real life 17.10.2014

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

Page 5: Clojure in real life 17.10.2014

Syntax

Page 6: Clojure in real life 17.10.2014

Syntax

•  Heterogeneous read-only data structures •  All data literals stand for themselves, except Lists & Symbols

Page 7: Clojure in real life 17.10.2014

Simple program

Page 8: Clojure in real life 17.10.2014

Immutable, persistent Data

Page 9: Clojure in real life 17.10.2014

Structural sharing

h)p://eclipsesource.com/blogs/2009/12/13/persistent-­‐trees-­‐in-­‐git-­‐clojure-­‐and-­‐couchdb-­‐data-­‐structure-­‐convergence/    

Page 10: Clojure in real life 17.10.2014

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?  

Page 11: Clojure in real life 17.10.2014

Business DSL

Page 12: Clojure in real life 17.10.2014

Java Interoperability

Page 13: Clojure in real life 17.10.2014

math speed / hiphip (array)!

Page 14: Clojure in real life 17.10.2014

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

Page 15: Clojure in real life 17.10.2014

STM example

h)p://sw1nn.com/blog/2012/04/11/clojure-­‐stm-­‐what-­‐why-­‐how/    

Page 16: Clojure in real life 17.10.2014

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/

Page 17: Clojure in real life 17.10.2014

core.async, example •  Boiling Sous-Vide Eggs using Clojure's Transducers

–  http://blog.eikeland.se/2014/10/06/pid-transducer/?utm_content=buffer75806

Page 18: Clojure in real life 17.10.2014

Didn’t cover •  Protocols, Multimethods •  Destructuring •  Dynamic binding •  Walkers •  Records •  Metadata

Page 19: Clojure in real life 17.10.2014

Missed these too •  core.match – pattern matching •  core.typed – gradual (static) typing •  core.logic – logic programming

Page 20: Clojure in real life 17.10.2014

Ok, this is just another academic…

Page 21: Clojure in real life 17.10.2014

In real life

Page 22: Clojure in real life 17.10.2014

Leiningen •  Declarative build tool, uses Maven internally

Page 23: Clojure in real life 17.10.2014

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.

Page 24: Clojure in real life 17.10.2014

Integration testing

Page 25: Clojure in real life 17.10.2014

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

Page 26: Clojure in real life 17.10.2014

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

Page 27: Clojure in real life 17.10.2014

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, …)

Page 28: Clojure in real life 17.10.2014

Simple web server

Page 29: Clojure in real life 17.10.2014

Schema Sausage?

Page 30: Clojure in real life 17.10.2014

Sc elements

Page 31: Clojure in real life 17.10.2014

Demo: a REST API •  with Compojure-api & Ring-Swagger

Page 32: Clojure in real life 17.10.2014

... something like this

Page 33: Clojure in real life 17.10.2014

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    

Page 34: Clojure in real life 17.10.2014

Async web - Scala & Clojure

h)p://www.slideshare.net/colinlogaan/clojure-­‐at-­‐a-­‐post-­‐office-­‐36333973    

Page 35: Clojure in real life 17.10.2014

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  

Page 36: Clojure in real life 17.10.2014

MongoDB sample

Page 37: Clojure in real life 17.10.2014

Utils •  Use Java libraries or idiomatic clj-wrappers on

top of them –  clj-time, emailer, clj-ssh, timbre (logging) & tons of other

goodnesses

Page 38: Clojure in real life 17.10.2014

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  

Page 39: Clojure in real life 17.10.2014

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

Page 40: Clojure in real life 17.10.2014

Demo: Flappy Bird

h)ps://github.com/bhauman/flappy-­‐bird-­‐demo    

Page 41: Clojure in real life 17.10.2014

OM / Reagent •  A ClojureScript interface to Facebook's React.js

Page 42: Clojure in real life 17.10.2014
Page 43: Clojure in real life 17.10.2014

Game of Life

h)p://clj-­‐me.cgrand.net/2011/08/19/conways-­‐game-­‐of-­‐life/    

Page 44: Clojure in real life 17.10.2014

Einstein / Zebra

h)ps://github.com/swannode)e/logic-­‐tutorial    

Page 45: Clojure in real life 17.10.2014

Sudoku Solver

h)ps://gist.github.com/swannode)e/3217582    

Page 46: Clojure in real life 17.10.2014

So, do people use it?

Page 47: Clojure in real life 17.10.2014

Thoughtworks Technology Radar 2014

Scala,  adopt!  

Where’s  my  

clojure?  

Page 48: Clojure in real life 17.10.2014
Page 49: Clojure in real life 17.10.2014

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)

Page 50: Clojure in real life 17.10.2014

:stats (1.5y  project,  5+  developers)  

Page 51: Clojure in real life 17.10.2014
Page 52: Clojure in real life 17.10.2014
Page 53: Clojure in real life 17.10.2014
Page 54: Clojure in real life 17.10.2014

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)

Page 55: Clojure in real life 17.10.2014

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

Page 56: Clojure in real life 17.10.2014

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.

Page 57: Clojure in real life 17.10.2014

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

Page 58: Clojure in real life 17.10.2014

Getting started

Page 59: Clojure in real life 17.10.2014

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)

Page 60: Clojure in real life 17.10.2014

FP Seminar at TTY

Page 61: Clojure in real life 17.10.2014

ClojuTRE 2014 •  Third annual Clojure seminar at Tampere 25.11.2014

–  Free beer, pizza & brackets –  Follow @metosin for updates

h)p://clojutre.org/2014/    

Page 62: Clojure in real life 17.10.2014

Thanks. Tommi Reiman / @ikitommi

http://www.metosin.fi