You must have heard people talk about Functional programming whilst your nod apprehensively, and how cool people do functional programming. You must have heard people talk about functional purity and imperative blasphemy. But what exactly does functional programming entail? Why is it making a comeback? This talk is aimed at providing an objective and practical view of what FP can and cannot do, and how it's nothing to be scared of, what languages support functional style and how it can help alleviate some common programming problems.
3. The Curse of Functional Programming
Once you understand it,
you lose the ability to explain it to others.
ThoughtWorks
4. Where is it used?
• 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
8. 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
10. 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
11. 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
12. 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
13. 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
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
16. 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
17. 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