Making life easier with just types and functions

21
MAKING LIFE EASIER WITH JUST TYPES AND FUNCTIONS CHANDAN SINGH ThoughtWorks

Transcript of Making life easier with just types and functions

Page 1: Making life easier with just types and functions

MAKING LIFE EASIER WITH JUST TYPES AND

FUNCTIONSCHANDAN SINGH

ThoughtWorks

Page 2: Making life easier with just types and functions
Page 3: Making life easier with just types and functions

the curse of functional programming!

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

Page 4: Making life easier with just types and functions

why (typed) functional programming?

it looks cool on your résumé!

Page 5: Making life easier with just types and functions

complaints against FP?

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

Page 6: Making life easier with just types and functions

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

Page 7: Making life easier with just types and functions

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.

Page 8: Making life easier with just types and functions

what does FP bring to the table?

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

Page 9: Making life easier with just types and functions

insanity!

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

--Albert Einstein

launchMissiles();=> destroys enemy units

launchMissiles();=> throws OutOfMissilesException

Page 10: Making life easier with just types and functions

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

Page 11: Making life easier with just types and functions

confused? let’s make it simple!

pure functions, no side effectsalso, higher order functions

Page 12: Making life easier with just types and functions

what’s a function?

a mapping,each input mapped to exactly one output,

domain co-domain

Page 13: Making life easier with just types and functions

functions are inherently typed

meaningful mappings,transformation rules => constraints,

capture constraints precisely

Page 14: Making life easier with just types and functions

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

Page 15: Making life easier with just types and functions

impure stuff

setters,

void/unit methods,

in-memory updates,

random()

Page 16: Making life easier with just types and functions

what’s wrong with impure stuff?

contextual dependencystate changes

order of execution is importantfar too much guessing to be done

Page 17: Making life easier with just types and functions

stop talking, show some code!

Page 18: Making life easier with just types and functions

so why is this FP suddenly a rage?

concurrency ...immutability, ftw!

Page 19: Making life easier with just types and functions

code, code, more code...

Page 20: Making life easier with just types and functions

so again, why (typed) FP?

making things easier,programming with less effort,

developer happiness!

Page 21: Making life easier with just types and functions

thank you!