making life easier with just types and functions

Post on 08-Jan-2017

39 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

MAKING LIFE EASIER WITH JUST TYPES AND

FUNCTIONSCHANDAN SINGH

ThoughtWorks

the curse of functional programming!

once you understand it well enough, you lose the ability to explain it to others...

why (typed) functional programming?

it looks cool on your résumé!

complaints against FP?

it’s weird,impractical,why do it when OO/Procedural just works?theoretical/academic nonsense,no one really does it

who’s doing FP?Facebook,Twitter,LinkedIn,Microsoft,Chef,Many banks...also, ThoughtWorks

There’s something about the culture of software that has impeded the use of specification. We have a wonderful way of describing things precisely that has been developed over the last couple of millennia, and it’s called Mathematics.-- Leslie Lamport, Turing Award Winner, 2013.

what does FP bring to the table?

good codeeasy to readeasy to understandeasy to testeasy to maintaineasy to extend

insanity!

doing the same thing over and over again, and expecting different results!

--Albert Einstein

launchMissiles();=> destroys enemy units

launchMissiles();=> throws OutOfMissilesException

what is FP?

lambda calculusid(x) = x

x -> xsquare_sum(x, y) = x * x + y * y

(x, y) -> x * x + y * yx -> (y -> (x * x + y * y))

confused? let’s make it simple!

pure functions, no side effectsalso, higher order functions

what’s a function?

a mapping,each input mapped to exactly one output,

domain co-domain

functions are inherently typed

meaningful mappings,transformation rules => constraints,

capture constraints precisely

public Integer square(Integer n) {

n = n * n;

return n;

}mutation, side effecting => impure

public Integer square(Integer n) {

return n * n;

}

no side effect, does only one thing => pure

impure stuff

setters,

void/unit methods,

in-memory updates,

random()

what’s wrong with impure stuff?

contextual dependencystate changes

order of execution is importantfar too much guessing to be done

stop talking, show some code!

so why is this FP suddenly a rage?

concurrency ...immutability, ftw!

code, code, more code...

so again, why (typed) FP?

making things easier,programming with less effort,

developer happiness!

thank you!

top related