Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Introduction to Functional
Programming and Clojure
@sdaas
What to Expect …
• Look at programming in a different way
– Immutable data
– A world without for-loops
• Understand basics...
What NOT to Expect …
• NOT a deep dive into all aspects of the Clojure
programming language
• NOT about installation, debu...
Don’t forget Java yet …
• FP is a programming paradigm or style
• Some languages lend themselves better to
functional prog...
Functional Programming Concepts
• More like Mathematical Functions
– vs. Subroutines/procedures
• Support higher order fun...
What is Clojure ?
• Created by Rich Hickey
• Immutable and Functional Programming style
• Lisp that runs on the JVM
• Inte...
Clojure Syntax
Functions
Lists
• Collection of heterogeneous values
• Operations on lists never modify the original
list. They always return a copy...
Lists
Building Blocks – first & rest
• Favoring recursion over iteration
• Do you see the bomb in the nth-element ?
Tail recursion is your friend
Map, Filter, and Reduce
Composition
• FP encourages you to compose small/simpler
function to create complex functions
What does this code do ?
Sum of square of all odd numbers ..
• How testable and maintainable is this code ?
• Modify this for odd numbers that aren...
Functional Version …
Find the first 20 prime numbers
Laziness is good ...
• Iterate : returns lazy sequence of f(x) f(f(x)) etc ..
• take : returns a lazy sequence of first n ...
Hash Maps
Reading a CSV File …
FROM
Smith,John,45,90210
Frank,Peter,25,90110
Bond,James,38,90110
TO
{:zip 90210, :age 45, :fname "Jo...
12 lines of Clojure Code …
What the %$*&^ was that ?
In Summary …
• FP concepts can be used to create concise,
highly modular, and composable code
• Java 8 and JavaScript have...
Questions ?
Please give your feedback at
http://bit.ly/introfp
Resources
• The SICP Book “Structure and Interpretation of
Computer Programs”
• Paul Graham’s Blogs
• Clojure Home page (c...
Upcoming SlideShare
Loading in …5
×

Introduction to Functional Programming and Clojure

724 views

Published on

A basic introduction to Functional Programming using Clojure

Published in: Software
  • Be the first to comment

Introduction to Functional Programming and Clojure

  1. 1. Introduction to Functional Programming and Clojure @sdaas
  2. 2. What to Expect … • Look at programming in a different way – Immutable data – A world without for-loops • Understand basics of functional programming – In the context of Clojure • A POV on why to consider a functional programming style in your current project • And why Clojure might be a good choice
  3. 3. What NOT to Expect … • NOT a deep dive into all aspects of the Clojure programming language • NOT about installation, debugging, performance, etc. of Clojure • NO bashing of Java or any other language • NO bashing of Object-Oriented Design
  4. 4. Don’t forget Java yet … • FP is a programming paradigm or style • Some languages lend themselves better to functional programming – Clojure, Haskell, Erlang • Most languages support FP to some extent – Java 8 – JavaScript – Python
  5. 5. Functional Programming Concepts • More like Mathematical Functions – vs. Subroutines/procedures • Support higher order functions • Pure functions – with no side effects • Recursion is favored over iteration • Lazy Evaluation • Referential Transparency – Once defined, a “variable” never changes value
  6. 6. What is Clojure ? • Created by Rich Hickey • Immutable and Functional Programming style • Lisp that runs on the JVM • Interoperates with Java • Power of Functional Programming meets the Richness of Java Ecosystem
  7. 7. Clojure Syntax
  8. 8. Functions
  9. 9. Lists • Collection of heterogeneous values • Operations on lists never modify the original list. They always return a copy. • This “immutability” of data structure is a big deal in Functional Programing. • Two fundamental operators – first, & rest • Every data structure and function is built up using these two operators
  10. 10. Lists
  11. 11. Building Blocks – first & rest • Favoring recursion over iteration • Do you see the bomb in the nth-element ?
  12. 12. Tail recursion is your friend
  13. 13. Map, Filter, and Reduce
  14. 14. Composition • FP encourages you to compose small/simpler function to create complex functions
  15. 15. What does this code do ?
  16. 16. Sum of square of all odd numbers .. • How testable and maintainable is this code ? • Modify this for odd numbers that aren’t multiples of 7 ? • How would this work for infinitely long data ?
  17. 17. Functional Version …
  18. 18. Find the first 20 prime numbers
  19. 19. Laziness is good ... • Iterate : returns lazy sequence of f(x) f(f(x)) etc .. • take : returns a lazy sequence of first n from collection • Map and filter can work on lazy sequences • Check out the call to java.lang.Math.sqrt
  20. 20. Hash Maps
  21. 21. Reading a CSV File … FROM Smith,John,45,90210 Frank,Peter,25,90110 Bond,James,38,90110 TO {:zip 90210, :age 45, :fname "John", :lname "Smith"} {:zip 90110, :age 25, :fname "Peter", :lname "Frank"} {:zip 90110, :age 38, :fname "James", :lname "Bond"}
  22. 22. 12 lines of Clojure Code …
  23. 23. What the %$*&^ was that ?
  24. 24. In Summary … • FP concepts can be used to create concise, highly modular, and composable code • Java 8 and JavaScript have good support for FP concepts • Clojure interoperates well with Java, and could be a good way to experiment with FP without abandoning the Java ecosystem
  25. 25. Questions ? Please give your feedback at http://bit.ly/introfp
  26. 26. Resources • The SICP Book “Structure and Interpretation of Computer Programs” • Paul Graham’s Blogs • Clojure Home page (clojure.org) • Clojure for the Brave and True (braveclojure.com) • www.slideshare.net/SoumendraDaas/present ations • www.github.com/sdaas/clojure-talk • https://medium.com/@sdaas/functionally- speaking-f4dba2a1c7ee

×