The Imminent Threatof Functional Programming
Dimitry Solovyov‣ Haskell enthusiast and FP proponent‣ Heavy user of Underscore (FP library for JavaScript)‣ Former assistant teacher of FP course at TTI‣ Developer at Amber Games and Livesheets
dresdencodak.com
FUNCTIONALPROGRAMMING
In programming land,there are two kinds of problems
In programming land,there are two kinds of problems
memorymanagement
In programming land,there are two kinds of problems
sequencing(aka do it in what order?)
memorymanagement
In programming land,there are two kinds of problems
sequencing(aka do it in what order?)
memorymanagement
SOLVED
In programming land,there are two kinds of problems
sequencing(aka do it in what order?)
memorymanagement
SOLVED
* Except for null pointer exceptions ;-)
*
In programming land,there are two kinds of problems
sequencing(aka do it in what order?)
memorymanagement
SOLVED
* Except for null pointer exceptions ;-)
*?
Sequencing in Java
while cond stmtdo stmt while cond
for init cond step stmtfor x : xs stmt
Sequencing in Ruby
loop stmtwhile cond stmtuntil cond stmtstmt while condstmt until condfor x in xs stmt
xs each stmtn times stmtn upto m stmt
n downto m stmt...
Sequencing in Ruby
loop stmtwhile cond stmtuntil cond stmtstmt while condstmt until condfor x in xs stmt
xs each stmtn times stmtn upto m stmt
n downto m stmt...
How elegant!
Imperative programming in a nutshell
10 BEGIN20 DO SOMETHING30 DO THAT OTHER THING40 GOTO 20
INSTRUCTIONS
✔RESULT
Declarative programming in a nutshell
Dear computer,I want a pony.
DESCRIPTION
✔RESULT
MAGIC
INSTANTSUCCESS
OO makes code understandable by encapsulating moving parts.
FP makes code understandable by minimizing moving parts. ”
“
-- Michael Feathers
“Functional Programming in C++”http://goo.gl/aVSXX
Abstraction and reusability in FP
Mathematically provento be composable
Category theory *Highly reusable
Excitingly composable
Extremely fun
Absolutely abstract
Check out Scalaz for CT in ScalaGuava for other FP patterns in Java
*
Some GoF pattern analogsprovided at semantic level
“Design Patterns in Haskell”http://goo.gl/T0Evt
+
You can get away withoutencapsulating in objects
Data types
CombinatorsImmutable
data structuresoperate on
designed fo
rformed into
You can get away withoutencapsulating in objects
Data types
CombinatorsImmutable
data structuresoperate on
designed fo
rformed into
You can get away withoutencapsulating in objects
Data types
CombinatorsImmutable
data structuresoperate on
designed fo
rformed into
Ready for the age of multicore
Traits instead of inheritance
Inheriting state creates brittle software.Inheriting implementation tends to become useless over time.
Thesis:
“Life without Objects”http://goo.gl/wlOyq
Traits instead of inheritance
Inheriting state creates brittle software.Inheriting implementation tends to become useless over time.
Thesis:
A
B
C
Data type
Expected to behave like A, B, and C“Life without Objects”
http://goo.gl/wlOyq
In the exclusive sense, functional means no side-effects.
In the inclusive sense it means a programming style which composes
functions in interesting ways. ”
“
-- Martin Odersky
“In Defense of Pattern Matching”http://goo.gl/HaKQD
LISP ML
Common LispScheme
DylanRacketClosure
Standard MLOCaml
F#HaskellFrege
Scala *JVM JVM
JVM
* (cough) Close enough.
Origin of the “functional style”
LISP ML
DynamicCode = data
Metaproggin'
Strong typesType inference
ADTs
First-class functionsHigher-order functions
Pattern matching
LISP ML
DynamicCode = data
Metaproggin'
Strong typesType inference
ADTs
First-class functionsHigher-order functions
Pattern matching
Linus says:
“Words are cheap, show me the code.”
Fake first-class functions in Java
interface Func<A, B> { B apply(A x);}
static <A, B, C> Func<A, C> compose(final Func<A, B> f, final Func<B, C> g) { return new Func<A, C>() { public C apply(A x) { return g.apply(f.apply(x)); } };}
Fake higher-order functions in Java
static <A, B> List<B> map(final Iterable<A> xs, final Func<A, B> f) { List<B> ys = new ArrayList<B>(); for (A x : xs) { B y = f.apply(x); ys.add(y); } return ys;}
map
Fake higher-order functions in Java
static <A, B> B fold(final Iterable<A> xs, final B init, final Func2<A, B, B> f) { B acc = init; for (A x : xs) { acc = f.apply(x, acc); } return acc;}
fold
REFERENTIALTRANSPARENCY
How logic works
x = 1y = 2x + y = 3
How logic works
x = 1y = 2x + y = 3*
But only if the moon is still young!
Otherwise the answer is 5.*
in languageswith side-effects
Functional programming on the JVM
Clojure
Fantom
Frege
Java
JRuby
Kotlin
Scala
✔ ✔ ✔ ✘ ✘
✔ ✔ ✔ ✘ ✘
✔ ✔ ✔ ✔ ✔
✔ ✘ ✘ ✘ ✘
✔ ✘ ✘ ✘ ✘
✔ ✔ ✔ ✘ ✔
✔ ✔ ✔ ✘ ✔
closure
s
first-
class
functio
ns
refe
rentia
l
transp
arency
controlle
d
side-e
ffects
pattern
matching
❏ Start learning a functional language:Scala, Clojure, or even Haskell
❏ Get together @ Latvian FP Group:look for it on LinkedIn
❏ Talk to me!
FP exploration checklist
http://goo.gl/YhlJl
Top Related