Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Why Functional Programming Matters
Search
αλεx π
January 10, 2014
290
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Why Functional Programming Matters
Ramblings on the subject of "why FP matters"
αλεx π
January 10, 2014
More Decks by αλεx π
See All by αλεx π
Scalable Time Series With Cassandra
ifesdjeen
1
430
Bayesian Inference is known to make machines biased
ifesdjeen
2
410
Cassandra for Data Analytics Backends
ifesdjeen
7
460
Stream Processing and Functional Programming
ifesdjeen
1
790
PolyConf 2015 - Rocking the Time Series boat with C, Haskell and ClojureScript
ifesdjeen
0
530
Clojure - A Sweetspot for Analytics
ifesdjeen
8
2.1k
Going Off Heap
ifesdjeen
3
1.9k
Always be learning
ifesdjeen
1
190
Learn Yourself Emacs For Great Good workshop slides
ifesdjeen
3
350
Featured
See All Featured
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
400
Scaling GitHub
holman
464
140k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.4k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
4
500
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
220
My Coaching Mixtape
mlcsv
0
300
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
580
Mobile First: as difficult as doing things right
swwweet
225
10k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
450
Transcript
Why FP Matters and everyone should learn more of it
None
Major Concepts
Expressions, not assignments FP encourages thinking in results of evaluation,
rather than assigning results to re-assignable variables
Example Recursive computation
(def map! (fn [f coll]! (if (empty? coll) (quote ())!
(cons (f (first coll))! (map f (next coll))))))!
Outcome Easier composition Less errors (no way to modify result
in-between calls)
How to do, not what to do FP discourages operational
reasoning, promotes algorithmical reasoning
Example Evaluation
In Java You write a sequences of steps requiring for
solving the problem
In Haskell You write resolution formulas
In Haskell You write resolution formulas
eval closure sym@(LispSymbol _) eval :: [LispEnvironment] -> LispExpression ->
State LispEnvironment LispExpression eval closure (LispList[(ReservedKeyword FnKeyword), LispVector bindings, form])
Outcome Obvious problem description, based on signatures Matching logic doesn’t
involve if/else statements Logic is inherently recursive
Function composition FP encourages function, rather than object composition
Example Routing: URL parsing + handler logic
(compojure/defroutes main-routes! (GET root "/" request (index request))! (GET search
"/search" request (search request)))
Outcome Both handlers and parsing logic is reusable Testing handlers
doesn’t involve any additional object composition
Primitives FP encourages using language primitives, such as keywords (lightweight
strings), lists, maps instead of heavy objects
Outcome Adapter logic is just a transformation Reusability increases Easier
composition
Powerful collection fns All Functional languages have amazingly powerful collection
manipulation primitives, such as filter/map/reduce, which always involves conditionals, mutable state, loops and so on.
Outcome Obvious results Thinking in patterns Reusability, describing processes as
sequence of steps Many opportunities instead of a single “for” loop
Inherently concurrent FP languages do not let you modify state,
which prevents using locking structures, synchronisation primitives and allows thinking of state as “value at a time”
Outcome Easier to scale Everything is paralellisable
Different polymorphism In different languages, there are different means. For
example, in Clojure you have protocols and multimethods, in Haskell - algebraic data types, type classes and pattern matching
Outcome Dispatch rarely involves any if/else statements It’s easier to
refactor functions to use a common protocol or implement a type class / ADT
Partial evaluation / currying Concept that requires lots of bells
and whistles in Java In both Clojure and Haskell, you can split a function to sequence of partial evaluation calls
Outcome It’s possible to return a partial evaluation result, and
act on it as if it was a future, evaluating it till the end as the rest of args pop in
Lazy evaluation Despite the fact the concept is extremely easy,
it’s seldom used in imperative languages. Most FP languages have built-in primitives for `next` calculation
Example Byte Buffer position generation
(defn positions! "Returns a lazy sequence containing positions of elements"!
[types]! ((fn rpositions [[t & more] current-pos]! (when t! (cons current-pos ! ! ! ! ! (lazy-seq ! ! ! ! ! (rpositions more (+ current-pos (size t))))))) types 0))
Outcome Infinite data structures No mutable state during `next` generation
Generator is easy to read and modify, knowing the semantics No leaks, no accidental captures
Predictable output Because of lack of side-effects, functions generally produce
single output for same input
Outcome Easier to test Easier to debug Smaller error rate
Determinism / potential for optimisation Easier to prove program correctness
Why it matters for us?
Patterns In Consulting, it’s important to identify problem fast and
provide an easily explainable solution
Knowing it before them Adoption of FP languages is growing
every day. Not being able to provide services will leave us with limited business opportunities.
Rapid Prototyping FP languages allow you to express a problem
fast, with minimal effort and provide a working result quicker than imperative one
Mind Flexibility Being able to approach the problem from different
perspective never hurts
FP In Java Reactor is one of my favourite examples
of using FP paradigms even in Java 7. Java 8 and lambdas is a step forward. Being able to use them knowing the context of where they’re coming from opens up a lot of possibilities.
Reactive programming Reactive programming also grows from FP. Having a
function as a callback was used singe ages in FP.
Having more fun @ work Self-explanatory
Questions?