Introduction to Clojure and why it's hot for Sart-Ups

  • 1,577 views
Uploaded on

Introduction to Clojure Concepts and why it's loved by start-ups (also for big-data processing). …

Introduction to Clojure Concepts and why it's loved by start-ups (also for big-data processing).
Talk at Big Data Beers Meetup Berlin 13th Feb 2013

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,577
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
16
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. DISCLAIMER:1 YOU MIGHT ALREADY KNOW!2 I AM NOT A ‚TYPOLOGIST‘TAKE IT AS A JOURNEY WITHANOTHERS EYES
  • 2. OVERVIEW• Teil 1: Prolog und Philosophie• Teil 2: Core Language Concepts• Teil 3: (Language Specifics)• Teil 4: Epilog Recently Cedric Beust said something like: „Whenever there is a new paradigm or language learn from it. Don‘t fall in love.“ POLY
  • 3. TEIL 1PROLOGUE & PHILOSOPHY
  • 4. MY HISTORY…• 1980-1990• Prof. Dr. Peter Pepper• Language OPAL• Tutor “Opal is a strongly typed, higher-order, strict, pure functional language, and as such can be classified alongside ML, Haskell, and other modern functional programming languages. However, the language also has a distinctive algebraic flavour in the tradition of languages such as CIP-L, Obj, and others.”
  • 5. SCHEME: 1990…Carsten Bormann, Oliver LaumannScheme, Elk, Emacs Lisp, … imgs.xkcd.com/comics/real_programmers.png
  • 6. RESULT LISP
  • 7. LUCKY IN MY LITTLE CHESSWORLD…Hence God must be a OO Programmer…
  • 8. CHESSPROG
  • 9. 2006 SOMEONE READ…
  • 10. complex flow complex state+flow
  • 11. INSIGHTS FROM THE PAPER• OOP re-use does rarely work!• komplexity increases exponentiell („boot it…!“)• We „humans“ are not able to master many statesSolution: • No State! = No Vars! • less code (e.g. powerful comprehensions) • Higher-Order Functions • everything super abstract
  • 12. FIRST LANGUAGE FROM A 3RD PARTYPHILOSOPHY!Together with Scala strong market penetration in Big-Data (Analytics)• BigML• Storm• Cascalog• Nokia• Twitter• Akamai• Factual• Prismatic!!• Groupon• Heroku!! As PaaS!• Top 1 TechRadar von ThoughtWorks (with Scala)
  • 13. THOUGHTWORKS RADAR http://thoughtworks.fileburst.com/assets/technology-radar-october-2012.pdf
  • 14. CLOJURE TOP #121. Functional => pure / first-class / Higher Order Functions2. Immutable => safe and correct Functions3. Super Expressiveness => smart with fewer Brackets4. DSL ready (e.g. parsley)5. OO or logic concepts easy to integrate (core.logic)6. Java hosted => fast & libs7. perfect Java Integration & integration easier then Scala (at least to me)8. Multicore Ready / multi-CPU with MVCC STM=> always Thread Safe9. Strong Macros (Scala is currently integrating this) => dream a language10. dynamic and strong typed (to me d, to vm s)11. super abstract Programming (composable)12. Tons of Books, Docs, Links, Videos, Trainings, etc.
  • 15. SOME OLD PERFORMANCENUMBERS => 24/42 => 57%
  • 16. Performance Tipps:- use native Types & generic collections- use the array wrapper of Clojure- use Java
  • 17. LESS CODE = LESS ERRORSBest Expressiveness (rosettacode.org)73 C#41 Clojure115 D202 Go68 Groovy88 Java26 Prolog .. ups?? (how can this be?)83 Python100 Ruby129 ScalaCore logic does it in 33 lines:http://dosync.posterous.com/friendlier-shorter
  • 18. NO MUTABLE STATE = NO VARS INPUT files, dbs, userAdvantage: Functions can be pure! Disadvantage: sometimes hard
  • 19. PART IICORE LANGUAGE CONCEPTS
  • 20. Neil Ford, Robert „Bob Martin“, Howard Lewis Ship,… „Clojue is a dynamic, strongly typed, functional, high-performanceimplementation of a lisp on the JVM.“ NF on WJax
  • 21. Moving from Syntax…object.methodName(arg2, arg3);methodName(object, arg2, arg3);function(arg1, arg2, arg3);function(arg1 arg2 arg3)(function, arg1, arg2, arg3) to Data Structure! we AST!(function, arg1, arg2, arg3) => :value © Edmund Jackson
  • 22. INFINITE ARGSNO OPERATOR PRECEDENCE!HOMOICONIC : extend your language! = Metaprogramming! © aphyr.com
  • 23. BASIC DATA STRUCTURES © http://codingndesign.com
  • 24. ABSTRACIONS EVERYWHEREThe world consists of Lists! => Sequences• or even lazy Lists! (lazy sequences)Even Trees and Graphs are listsMostly Closuje doesn„t care if it gets• a List `()• a Vector []• a (sorted) (hash) Map {}• a (sorted) Set #{}
  • 25. DON‘T MIX IDENTITY WITH VALUE! The first ref is a time machine! © http://codingndesign.com
  • 26. CEREMONY YIELDS CEREMONY package a.c.b public class Person { public / private / protected vars … private vars … protected vars … constructors … getters / setters … visibility public / private / protected methods …attributes getters settersconstructors • All mixed together! namespaces • Leads to more code! methods • Leads to more accidential complexity! and more… • Leads to unneccesary Design Patterns!
  • 27. List Map Sequence + A function SetMOSTLY INHERENT COMPLEXITY
  • 28. MDD : MAP DRIVEN DESIGNThis leads partially to a different design approach:• Identify problem domains• Start with defining your data in terms of maps,lists, seqs,…• The protocol can be the data• Not the API in terms of functions which are already bound to Classes and Types!• Connect these domains loosely by passing data • e.g. with pipes / queues / events / etc.Sam Aaron: „A map key often speaks more and better then a documented interface / class“
  • 29. DESIGN PATTERNS VANISH! Peter Norvig demonstrated some time agohttp://www.norvig.com/d esign-patterns/ thatLisps in particular either simplify or make invisible most design patterns!
  • 30. HIGHER ORDER FUNCTIONS MACROS comp partial, apply map, reduce, sort, filter, super-for, range repear, iterate, drop, cycle interleave, interpose, take, split some?, not-any?, not-every?, … abstract continue for new switch und 1000 mehr assert default if package synchronized boolean do goto private this break double implements protected throw byte else import public throws case enum instanceof return transient catch char extends int final short interface static try void def, if, do, let, ´, fn, loop, recur,… class finally long strictfp volatile const float native super while Lists, Lambda Kalkül Keywords: imparativ
  • 31. CONCLUSION PART II• Clojure is a cleaned up lisp• introduction of a sequence abstraction• powered by the JVM• easier symbol resolution / syntax quoting• convenient literal syntax => less parantheses • params in a vector• Commas are whitespaces in data• Avoid many nestings…(cond ((< x 10) „less“) (cond (< x 10) „less“ (> x 10) „more“)) (> x 10) „more“)
  • 32. PART IIILANGUAGE SPECIFICS PEEK IN…
  • 33. CLOJURE AREAS Datatypes Functions map, reduce, Java Interop pmap Control Sequences / sequable map / reduce / reduce++ Laziness Concurrency / Reference Types (STM) Uses fork / join for easy seq processing on all Datatypes & Protocols cores you have. Multimethods Macros
  • 34. MAPS ARE FUNCTIONSKEYS ARE FUNCTIONS* IS A FUNCTION > (def edlich {:name „edlich“ :age 42})> (edlich :age) => 42> (:age edlich) => 42
  • 35. HAMMOCK DRIVEN DEVELOPMENT - HDDhttp://blip.tv/clojure/hammock-driven-development-4475586Don„t move your fingers fast…Think & Sleep!
  • 36. 4CLOJURE• Not a competition about the shortest unreadable code• but shorter code!• more consise code!• craftable code! „there is a simpler solution“• more abstract code!see http://tapestryjava.blogspot.de/2013/02/crafting-code-in-clojure.html
  • 37. PART VIEPILOG
  • 38. TONS OF MAGIC… PIPES! or let for even more readability
  • 39. BETTER REPL THEN *Always run & change your env...! Nointerpretation. Recompile & run in no time. Idea: Bret Victor “Inventing on Principle” http://www.youtube.com/watch?v=PUv66718DII
  • 40. FOR / LOOPS = A CODE SMELL!Do a recursive reverse withouth ‚reverse„ 95% no loop needed!
  • 41. WHY GOOD 4 STARTUPS• Truly Composable• Fast• Correct• Concurrent Access Solved: Atoms, Refs & Agents (Actors e.g. via Akka)• Multicore: • Lazy Sequences / Caching / Old Reducers • New Reducers fold combine fork . . . join } like Map / Reduce split and calc on N cores
  • 42. DRAWBACKS?• Too many brackets? Random Code from Apache Commons (defn blank? [s] (every? #(Character/isWhitespace %) s))
  • 43. DRAWBACKS?• Eclipse, IntelliJ, NetBeans, IDEs are good but far from JavaIDEs • Many reasons for a IDE vanish (lucky with emacs, vim, sublimetext)• Debugging is not mature • visual VM is ok. By the way we do not debug state! • We copy snippet in REPL and have the solution!• Static is more safe? Type mismatch is rare• Leiningen Build 2.0 is ok (uses Maven internally and feels like Maven)• List add Back, Vector add front => because… • Don„t mix abstract and concrete world• Namespaces are hard  • But super powerful…
  • 44. THEN WHEN HAVING A BEER HE TOLD ME:„Well since 2010 I am working on a database!“ ...whow
  • 45. DATOMIC !Could be the next talk! © Datomic
  • 46. STEFAN KAMPHAUSENChapter 1.3Ein Wort der Warnung ist angebracht. Die Benutzung von Lisp-artigen Programmiersprachen kann süchtig machen und / oder dieEntwicklung mit anderen Sprachen unangenehm bis schmerzhafterscheinen lassen. Sollten Sie sich nach der Lektüre diesesBuches und ggf. einiger Projekte nicht mehr in der Lage sehen, mitanderen Sprachen zu arbeiten, können wir dafür keineVerantwortung übernehmen.“(It‘s time for a warning. The usage of Lisp-alike languages can leadto addiction. The usage of other language then becomes awkwardand painful. We can not take any responsibility if you – afterreading this book – become unable to work with other languages!)
  • 47. QUOTES…„Clojure does not feel like hacking. It feels like a very analyticalprecision surgery of a problem into its component parts. You canwrite big, ugly Clojure. But good Clojure is readable andmaintainable, and a pleasure to write in.” Eric Normand“If you try to build abstractions in Java code it tends to get bigger. Ifyou write abstract code in Clojure it tends to get shorter.”“The other day, I was working on a little bit of code in Clojure, justtouching up some exception reporting, when I was suddenly struckby one of the fundamental reasons that Clojure is so enjoyable tocode in. Clojure is craftable: that is, in Clojure you have the option tocraft at your code to make it more concise, easier to read, and easierto maintain. That is not the case for all, or perhaps even most,programming languages.” Howard Lewis Ship (Tapestry)http://tapestryjava.blogspot.de/2013/02/crafting-code-in-clojure.htmland others converted as Uncle ‚Bob‘ Martin, Neil Ford, etc…
  • 48. 2 GERMAN EVENTS!