ThoughtWorks
Functional Programming
Coz it looks cool on a Resume
ThoughtWorks
The Curse of Functional Programming
Once you understand it,
you lose the ability to explain it to others.
ThoughtWorks
Where is it used?
• ThoughtWorks (Scala, Clojure)
• LinkedIn (Scala)
• Twitter (Scala)
• Facebook (Haskell, Clojure)
• SoundCloud (Scala)
• Akamai (Clojure)
AND MANY MORE
ThoughtWorks
Domain Co-Domain
-1
0
1
2
1
0
4
y = f(x)  Mathematical function
y = x^2  Square each element of domain
Function f(x) maps an Int to Int using square operation
ThoughtWorks
Jargon
Pure functions
Immutability
Higher Order Functions
Category Theory
Type StrictnessCurrying
Recursion
No Side Effects
Lambda Calculus
AND MANY MORE…
ThoughtWorks
Functional Programming
Programming with functions as building blocks
Programming with pure functions as building blocks
ThoughtWorks
Definition/Origins
• Based on Lamdba calculus invented in 1930s by Alonzo Church.
It states that a mathematical operation consists purely of functions.
• It’s also a style of programming that follows certain conventions, like some
of the jargon we saw earlier, viz. Immutability, Type strictness etc.
• Functional programming is programming without assignment statements
(Uncle Bob)
• An Expression based programming paradigm, viz. Using Expressions as
opposed to Statements, and combining expressions to form functions and
combining functions to form complex behaviours.
ThoughtWorks
Courtesy: Scott WlaschinThoughtWorks
Pure Functions
• For a valid input there’s one and only one valid output.
• Doesn’t modify variables out of its scope.
• In general, functions that have no side effects are pure functions.
• Advantages
a.Consistent reproducible results independent of the environment
b.Easier parallelization
c.Memoization or caching
d.Laziness
e.REFERENTIAL TRANSPARENCY
e.g.
f(x) = Math.sqrt(x)
i.e. -1 and Math.sin(90) is substitutable by 1, aka Referentially transparent
ThoughtWorks
Types
• Types are not classes!!
• Grouping of related values under a name.
• E.g. Int type groups all the mathematical Integers. Boolean groups
True and False.
• Provide compile time checks for correctness of the program.
• E.g. If you say 5 + Dog, Compiler will be upset.
ThoughtWorks
Recursion
• Because Loops have mutable nature and hence have side effects
• Recursion helps traverse tree structures more intuitively, e.g. listing
files
• Recursion is usually more concise
• Recursion not suited for all situations and uses more memory.
Scoped mutable state works just fine.
ThoughtWorks
First class citizens/Higher Order Functions
• Functions are first class like data, viz. literals and objects
• Functions can take other functions as arguments
• Functions can return a function as result
• Main advantage is partial application of function
ThoughtWorks
Currying with HOF
• A technique of transforming a multi-argument function in such a way
that it can be called as a chain of functions, each with a single/sub-set
of argument(s).
• Named after ....
• Better explained through examples
• Show me (pseudo)code!!
Haskell Curry
ThoughtWorks
Examples
ThoughtWorks
Advantages
• Easier reasoning while programming – More predictable
• Less cognitive overload – Less to keep in mind – Code Brevity
• Smaller solution space
• Easier debugging
• Cleaner/more readable code
• Saner Parallelization/Concurrency
• Better Modularization/Reuse
• Sharing data due to immutability
• Mapping, filtering, reducing, folding >>> cooler than looping
AND MANY MOREThoughtWorks
Limitations
• Real-world useful programs will have side effects.
• High barrier to entry
• it is very difficult to predict the nature of lazy programs(From
StackOverflow)
• Lack of good tools for debugging – Perhaps due to the nature of it
• Many a times FP is CPU/Memory intensive
• Unsuitable for low level – hardware bound programming and
scripting
• Functional fanatics are snobby
FEW MOREThoughtWorks
Questions
ThoughtWorks
Thank You
Siddharth Kulkarni
@sid_thinketh
ThoughtWorks

Basics of functional Programming by Siddharth Kulkarni

  • 1.
  • 2.
    Functional Programming Coz itlooks cool on a Resume ThoughtWorks
  • 3.
    The Curse ofFunctional Programming Once you understand it, you lose the ability to explain it to others. ThoughtWorks
  • 4.
    Where is itused? • ThoughtWorks (Scala, Clojure) • LinkedIn (Scala) • Twitter (Scala) • Facebook (Haskell, Clojure) • SoundCloud (Scala) • Akamai (Clojure) AND MANY MORE ThoughtWorks
  • 5.
    Domain Co-Domain -1 0 1 2 1 0 4 y =f(x)  Mathematical function y = x^2  Square each element of domain Function f(x) maps an Int to Int using square operation ThoughtWorks
  • 6.
    Jargon Pure functions Immutability Higher OrderFunctions Category Theory Type StrictnessCurrying Recursion No Side Effects Lambda Calculus AND MANY MORE… ThoughtWorks
  • 7.
    Functional Programming Programming withfunctions as building blocks Programming with pure functions as building blocks ThoughtWorks
  • 8.
    Definition/Origins • Based onLamdba calculus invented in 1930s by Alonzo Church. It states that a mathematical operation consists purely of functions. • It’s also a style of programming that follows certain conventions, like some of the jargon we saw earlier, viz. Immutability, Type strictness etc. • Functional programming is programming without assignment statements (Uncle Bob) • An Expression based programming paradigm, viz. Using Expressions as opposed to Statements, and combining expressions to form functions and combining functions to form complex behaviours. ThoughtWorks
  • 9.
  • 10.
    Pure Functions • Fora valid input there’s one and only one valid output. • Doesn’t modify variables out of its scope. • In general, functions that have no side effects are pure functions. • Advantages a.Consistent reproducible results independent of the environment b.Easier parallelization c.Memoization or caching d.Laziness e.REFERENTIAL TRANSPARENCY e.g. f(x) = Math.sqrt(x) i.e. -1 and Math.sin(90) is substitutable by 1, aka Referentially transparent ThoughtWorks
  • 11.
    Types • Types arenot classes!! • Grouping of related values under a name. • E.g. Int type groups all the mathematical Integers. Boolean groups True and False. • Provide compile time checks for correctness of the program. • E.g. If you say 5 + Dog, Compiler will be upset. ThoughtWorks
  • 12.
    Recursion • Because Loopshave mutable nature and hence have side effects • Recursion helps traverse tree structures more intuitively, e.g. listing files • Recursion is usually more concise • Recursion not suited for all situations and uses more memory. Scoped mutable state works just fine. ThoughtWorks
  • 13.
    First class citizens/HigherOrder Functions • Functions are first class like data, viz. literals and objects • Functions can take other functions as arguments • Functions can return a function as result • Main advantage is partial application of function ThoughtWorks
  • 14.
    Currying with HOF •A technique of transforming a multi-argument function in such a way that it can be called as a chain of functions, each with a single/sub-set of argument(s). • Named after .... • Better explained through examples • Show me (pseudo)code!! Haskell Curry ThoughtWorks
  • 15.
  • 16.
    Advantages • Easier reasoningwhile programming – More predictable • Less cognitive overload – Less to keep in mind – Code Brevity • Smaller solution space • Easier debugging • Cleaner/more readable code • Saner Parallelization/Concurrency • Better Modularization/Reuse • Sharing data due to immutability • Mapping, filtering, reducing, folding >>> cooler than looping AND MANY MOREThoughtWorks
  • 17.
    Limitations • Real-world usefulprograms will have side effects. • High barrier to entry • it is very difficult to predict the nature of lazy programs(From StackOverflow) • Lack of good tools for debugging – Perhaps due to the nature of it • Many a times FP is CPU/Memory intensive • Unsuitable for low level – hardware bound programming and scripting • Functional fanatics are snobby FEW MOREThoughtWorks
  • 18.
  • 19.