Dimitry Solovyov - The imminent threat of functional programming

37
The Imminent Threat of Functional Programming

Transcript of Dimitry Solovyov - The imminent threat of functional programming

Page 1: Dimitry Solovyov - The imminent threat of functional programming

The Imminent Threatof Functional Programming

Page 2: Dimitry Solovyov - The imminent threat of 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

Page 3: Dimitry Solovyov - The imminent threat of functional programming

dresdencodak.com

Page 4: Dimitry Solovyov - The imminent threat of functional programming

FUNCTIONALPROGRAMMING

Page 5: Dimitry Solovyov - The imminent threat of functional programming

In programming land,there are two kinds of problems

Page 6: Dimitry Solovyov - The imminent threat of functional programming

In programming land,there are two kinds of problems

memorymanagement

Page 7: Dimitry Solovyov - The imminent threat of functional programming

In programming land,there are two kinds of problems

sequencing(aka do it in what order?)

memorymanagement

Page 8: Dimitry Solovyov - The imminent threat of functional programming

In programming land,there are two kinds of problems

sequencing(aka do it in what order?)

memorymanagement

SOLVED

Page 9: Dimitry Solovyov - The imminent threat of functional programming

In programming land,there are two kinds of problems

sequencing(aka do it in what order?)

memorymanagement

SOLVED

* Except for null pointer exceptions ;-)

*

Page 10: Dimitry Solovyov - The imminent threat of functional programming

In programming land,there are two kinds of problems

sequencing(aka do it in what order?)

memorymanagement

SOLVED

* Except for null pointer exceptions ;-)

*?

Page 11: Dimitry Solovyov - The imminent threat of functional programming

Sequencing in Java

while cond stmtdo stmt while cond

for init cond step stmtfor x : xs stmt

Page 12: Dimitry Solovyov - The imminent threat of functional programming

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...

Page 13: Dimitry Solovyov - The imminent threat of functional programming

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!

Page 14: Dimitry Solovyov - The imminent threat of functional programming

Imperative programming in a nutshell

10 BEGIN20 DO SOMETHING30 DO THAT OTHER THING40 GOTO 20

INSTRUCTIONS

✔RESULT

Page 15: Dimitry Solovyov - The imminent threat of functional programming

Declarative programming in a nutshell

Dear computer,I want a pony.

DESCRIPTION

✔RESULT

MAGIC

Page 16: Dimitry Solovyov - The imminent threat of functional programming

INSTANTSUCCESS

Page 17: Dimitry Solovyov - The imminent threat of functional programming

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

Page 18: Dimitry Solovyov - The imminent threat of functional programming

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

*

Page 19: Dimitry Solovyov - The imminent threat of functional programming

Some GoF pattern analogsprovided at semantic level

“Design Patterns in Haskell”http://goo.gl/T0Evt

+

Page 20: Dimitry Solovyov - The imminent threat of functional programming

You can get away withoutencapsulating in objects

Data types

CombinatorsImmutable

data structuresoperate on

designed fo

rformed into

Page 21: Dimitry Solovyov - The imminent threat of functional programming

You can get away withoutencapsulating in objects

Data types

CombinatorsImmutable

data structuresoperate on

designed fo

rformed into

Page 22: Dimitry Solovyov - The imminent threat of functional programming

You can get away withoutencapsulating in objects

Data types

CombinatorsImmutable

data structuresoperate on

designed fo

rformed into

Ready for the age of multicore

Page 23: Dimitry Solovyov - The imminent threat of functional programming

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

Page 24: Dimitry Solovyov - The imminent threat of functional programming

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

Page 25: Dimitry Solovyov - The imminent threat of functional programming

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

Page 26: Dimitry Solovyov - The imminent threat of functional programming

LISP ML

Common LispScheme

DylanRacketClosure

Standard MLOCaml

F#HaskellFrege

Scala *JVM JVM

JVM

* (cough) Close enough.

Origin of the “functional style”

Page 27: Dimitry Solovyov - The imminent threat of functional programming

LISP ML

DynamicCode = data

Metaproggin'

Strong typesType inference

ADTs

First-class functionsHigher-order functions

Pattern matching

Page 28: Dimitry Solovyov - The imminent threat of functional programming

LISP ML

DynamicCode = data

Metaproggin'

Strong typesType inference

ADTs

First-class functionsHigher-order functions

Pattern matching

Page 29: Dimitry Solovyov - The imminent threat of functional programming

Linus says:

“Words are cheap, show me the code.”

Page 30: Dimitry Solovyov - The imminent threat of functional programming

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)); } };}

Page 31: Dimitry Solovyov - The imminent threat of functional programming

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

Page 32: Dimitry Solovyov - The imminent threat of functional programming

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

Page 33: Dimitry Solovyov - The imminent threat of functional programming

REFERENTIALTRANSPARENCY

Page 34: Dimitry Solovyov - The imminent threat of functional programming

How logic works

x = 1y = 2x + y = 3

Page 35: Dimitry Solovyov - The imminent threat of functional programming

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

Page 36: Dimitry Solovyov - The imminent threat of functional programming

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

Page 37: Dimitry Solovyov - The imminent threat of functional programming

❏ 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

[email protected]