Clojure - an introduction (and some CFML)

1,056 views
951 views

Published on

This is a talk I gave at CFCAMP 2012 in Munich. It's an introduction to Clojure for CFML developers and how to leverage Clojure from a CFML developer's point of view.

There are various examples and demo code that's not on the slides, contact me if you're interested in it.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,056
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • The functional paradigm has its root in the Lambda calculus, a method for notations and manipulation of mathematical functions introduced by Alonso Church around 1930.Lambda calculus was then rediscovered as a versatile programming tool by McCarthy in 1958 when Lisp was introduced as a programming language able to deal with mathematical notation.\n\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Moore’s law recently failed on one of its axis. CPU speed is not doubling or tripling every year anymore. It used to be the case in the late ’90. But the demand of computing power is growing as usual.\nHardware manufacturer resorted to other means to increase power, such as transistor density. Right now is not uncommon to see cheap laptops sold with 4 cpu cores out of the box. Change on chip density requires a change in software scalability. Software must be written for concurrency to take advantage of multi-core architectures.\n\n
  • \n
  • Serious question: Who feels confident that they know how to PROPERLY use CFLOCK\n
  • \n
  • What about Clojure in specific? Clojure is a relatively young programming language based on Lisp but running on the JVM.\nIt comes out from the frustration of Rich Hickey developing highly concurrent systems as a Java and .NET consultant.\nRich Hickey developed dotLisp for the .NET environment before Clojure.\nHe spent then 2.5 years designing and implementing Clojure. Clojure was officially announced in 2007.\n\n
  • Learning another language stimulates your brain\nIt’s interesting\nIt’s a new community\n\nFunny enough - Adobe kicking Flex in the butt stimulated this\n\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Clojure - an introduction (and some CFML)

    1. 1. Clojure - an introduction Kai Koenig CFCAMP Munich 2012*and some CFML
    2. 2. Me
    3. 3. Kai KoenigWorking with CFML since 1999Developing for mobile since 2003Recovering Flex developer being sucked into JS deeper and deeperRecently rediscovered Functional Programmingbloginblack.de2ddu.comtwitter.com/agentK
    4. 4. Agenda
    5. 5. 1. Why Functional Programming?2. Why Clojure?3. Some demos and (probably failing :-) live coding inthe Clojure REPL4. A quick look at Clojure and CFML integration
    6. 6. Why FunctionalProgramming?
    7. 7. Who of you has atraditional, old-school CompSci or Maths degree?
    8. 8. The ones among you who answered with “yes”probably have dabbled with either of:MLSchemeHaskellLispAt some point during their degree.
    9. 9. I did. Haskell and ML ** Kind of confused me, I couldn’t see the point and we had Java anyway - which wasmuch more awesome & could do OO & was the hipster language of the day ** &you could write AWT and Swing apps and just because.** The day was 1996; Java was at version 1.0.2; and I was in my first year of myCompSci and Maths degree
    10. 10. What is it?A programming paradigm based on functions as first-class citizens and a theoretical concept called “LambaCalculus”.Depending on the FP language you’re looking at,features might vary, but there are a few commonthemes in functional programming.
    11. 11. First-class and higher- order functions
    12. 12. Function that can take other functions as arguments orreturn them as results (e.g. d/dx - differential operatorin calculus to return the derivative of a function).Higher-order vs. first-class: just a subtle difference -previous is a mathematical concept, latter is a CompSciterm.
    13. 13. Pure functions
    14. 14. Functions without I/O and memory side effects.1. result is constant with respect to the parameter list2. function call can be performed in parallel, they can’tinterfere with each other; thread safety3. compiler has much more freedom in the evaluationstrategy of expressions provided the whole language isside effect-free.
    15. 15. Recursion
    16. 16. In functional languages, iteration (loops) are prettymuch exclusively done via recursion.Recursion functions invoke themselves all over again.Intermediate state is kept on a stack.
    17. 17. Other
    18. 18. Evaluation strategy (eager vs. lazy)Type system (untyped lambda calculus vs. type lambdacalculus)
    19. 19. But hang on. The world is full of “state”.And side-effects.I mean - WTF?
    20. 20. State can (and has to) be simulated in FP.Different languages use different concepts that still keepthe functional paradigm pure.Monads (Haskell)State passing style (A bunch of other FP languages)Managed references (Clojure)
    21. 21. How does FP “feel”?
    22. 22. Fibonacciiterative in Java
    23. 23. public static long itFibN(int n){ if (n < 2) return n; long ans = 0; long n1 = 0; long n2 = 1; for(n--; n > 0; n--) { ans = n1 + n2; n1 = n2; n2 = ans; } return ans;}
    24. 24. Fibonaccirecursive in Haskell
    25. 25. -- describe an infinite list based on the recurrencerelation for Fibonacci numbersfibRecurrence first second = first : fibRecurrencesecond (first + second)-- describe fibonacci list as fibRecurrence with initialvalues 0 and 1fibonacci = fibRecurrence 0 1-- describe action to print the 10th element of thefibonacci listmain = print (fibonacci !! 10)
    26. 26. Fibonaccirecursive in Clojure
    27. 27. (def fib (lazy-cat [0 1] (map + fib (rest fib))))execute:(take 10 fib)
    28. 28. Moore’s law failedNeed for concurrency
    29. 29. State in imperative and OO languages
    30. 30. OOP encapsulates stateChanges overwrite stateState has multiple observersConcurrency requires lockingLocking is complicated
    31. 31. Let’s look at Clojure
    32. 32. Clojure: dynamically typed, JVM-based Lisp dialect (by Rich Hickey in 2007)
    33. 33. But... why?
    34. 34. REPL
    35. 35. ReadEvalPrintLoop
    36. 36. Some Clojure features
    37. 37. Code is data
    38. 38. Literals
    39. 39. Java Interop
    40. 40. List,Vector, Map and Set
    41. 41. Life without for-loops
    42. 42. What is it good for?
    43. 43. Big data & AnalyticsPattern MatchingHeavy concurrency, scalable systemsHard problems (often related to formal math oralgorithmic work)
    44. 44. Clojure and CFML
    45. 45. CFML is Java, right?
    46. 46. There is cfmljure!https://github.com/seancorfield/cfmljure
    47. 47. Where and how would you use it?
    48. 48. One idea: Model in MVC appsWhy? Because Clojure allows you to express businesslogic and model data extremely efficiently.Or: when you need to be able to run certain code in ahighly parallelised environment.
    49. 49. Anyway - it’s fun :)
    50. 50. Get in touchTwitter: @AgentKBlog: http://bloginblack.dePodcast (2 Developers Down Under): http://2ddu.comAbout me: http://about.me/agentk

    ×