94
NEAL FORD software architect / meme wrangler ThoughtWorks ® [email protected] 3003 Summit Boulevard, Atlanta, GA 30319 www.nealford.com www.thoughtworks.com blog: memeagora.blogspot.com twitter: neal4d Functional Thinking

Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

NEAL FORD software architect / meme wrangler

ThoughtWorks®

[email protected] 3003 Summit Boulevard, Atlanta, GA 30319 www.nealford.com www.thoughtworks.com blog: memeagora.blogspot.com twitter: neal4d

Functional Thinking

Page 2: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

3 metaphors^tortured

Page 3: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

x = x + 1solve for x?

:assign to x

Page 4: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 5: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 6: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

languages are tools.

Learning a new one takes time.

Paradigms

Page 7: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

“functional” is more a way of thinking than

a tool set

“functional” is more a way of thinking than

a tool set

Page 8: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

Execution in the Kingdomof Nouns

Steve Yeggehttp://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html

Page 9: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

v e r b s !

Page 10: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

"OOP makes working with state easier.

FP makes eliminating state

easier"OH on

Page 11: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

OO makes code understandable by

encapsulating moving parts.

FP makes code understandable by minimizing moving

parts.Michael Feathers, author of “Working with Legacy Code”

Page 12: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

numberclassification

Page 13: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

perfect #

(sum of the factors of a #) - # = #

(sum of the factors of a #) = 2#

6: 1 + 2 + 3 + 6 = 12 (2x6)28: 1 + 2 + 4 + 7 + 14 + 28 = 56 (2x28)496: . . .

∑(f(#)) - # = #

Page 14: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

classification

∑(f(#)) = 2# perfect

∑(f(#)) > 2# abundant

∑(f(#)) < 2# deficient

Page 15: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

imperative

Page 16: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 17: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 18: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

2

8

Page 19: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 20: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 21: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

internal state

cohesive

composed

testable

refactorable

Page 22: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

(slightly more)functional

Page 23: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 24: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 25: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 26: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 27: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

no internal state

less need for scoping testablerefactorable

Page 28: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

“functional” is more a way of thinking than

a tool set

“functional” is more a way of thinking than

a tool set

Page 29: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

concepts

1st classfunctions

high-orderfunctions

pure functions

recursion

strict evaluation

1st classfunctions

purefunctions

high-orderfunctionsrecursion

strict evaluation

Page 30: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

high-order functions

Page 31: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

high-order functions

functions that can either take other

functions as arguments or return

them as results

Page 32: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 33: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 34: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 35: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

!

Page 36: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

closuresWhat’s so special about...

Page 37: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 38: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 39: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

let the language

manage state

let the language

manage state

Page 40: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

concurrency

memory allocation

garbage collection

tests

state

languages handle

time

specification-based testing frameworks

Page 41: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

1st-class functions

Page 42: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

1st-class functionsfunctions can

appear anywhere other language constructs can

appear

Page 43: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 44: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 45: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 46: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

think about results, not steps

think about results, not steps

Page 47: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

1 2 3 4 5 6 7 8 9 10 11 12

121 2 3 4 6 12

Page 48: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

think about results, not steps

think about results, not steps

Page 49: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 50: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

list comprehension

(number + 1)range from 1 to for each n in

filter list by criteria my is-factor? functionreturn the numbers that match

return the list as a set

Page 51: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 52: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

composition

Page 53: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

academia alert!

Page 54: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

curryinggiven:

then:

currying takes a function with a particular number of parameters and returns a

function with some of the parameter values fixed, creating a new function

Page 55: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

return a version that always multiplies by 4

==

Page 56: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the
Page 57: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

new, different tools

function reuse

new, different tools

Page 58: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

currying

Page 59: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

pure functions

Page 60: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

pure functions

no memory or i/o side effects

Page 61: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

enables memoization

if the result isn’t used, it can be removed

a particular invocation with a set of parameters returns a constant value

parallel execution

execution order can change

purity

Page 62: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

recursion

Page 63: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

iterative filtering

Page 64: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

recursive filtering

think about results, not steps

think about results, not steps

http://www.scala-lang.org/node/135

Page 65: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

think about results, not steps

think about results, not steps

what about things you want to control?

performance?

new, different tools

new, different tools

Page 66: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

imperative number classifier

optimized!

Page 67: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

optimized factors

think about results, not steps

think about results, not steps

Page 68: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

ingrained imperativity

Google challenged college grads to write code for 100 CPU computers...

...they failed

sound familiar?

learn MapReduce

post-imperative

http://broadcast.oreilly.com/2008/11/warning-x-x-1-may-be-hazardous.html

http://code.google.com/edu/submissions/mapreduce-minilecture/listing.html

Page 69: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

state

garbage collection

concurrency

. . .

tests

languages handle

time

iteration

Page 70: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

strict evaluation

Page 71: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

academia alert!

Page 72: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

all elementspre-evaluated

elements evaluatedas needed

strictevaluation

non-strictevaluation

divByZero

=4

Page 73: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

laziness

Page 74: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

([1001 7927] [1002 7933] [1003 7937] [1004 7949] [1005 7951])

new, different tools

new, different tools

Page 75: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

concurrency

Page 76: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

no effect on the world

depend only on their arguments

given the same arguments, return the same values

no notion of time

functions:

Page 77: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

wait for external events

expect change over time

affect the world

produce different answers at different times

most programs are processes

Page 78: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

what can we add to functional

programming to deal with processes?

Page 79: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

non-composable

assume 1 thread of control, 1 timeline

not atomic

with concurrency: lock & pray

subtle visibility rules

variables

Page 80: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

life w/ variables

variable

???

variable

???

?

Page 81: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

identity

identity value

identity value

value

explicit semantic

state

Page 82: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

identity, state, & time

term meaning

value immutable data in a persistent data structure

identity series of causally related values over time

state identity at a point in time

timerelative: before/simultaneous/after ordering of causal values

Page 83: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

epochal time model

v1

F

v2

F

v3

F

v4

Process events (pure functions)

Observers/perception/memory

States (immutable values)Identity

(succession of states)

Page 84: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

actors

Page 85: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

new, different tools

new, different tools

Page 86: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

thinking functionally

Page 87: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

immutabilityover

state transitions

http://www.ibm.com/developerworks/java/library/j-jtp02183/index.html

Page 88: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

resultsoversteps

Page 89: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

compositionover

structure

Page 90: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

declarative over

imperative

Page 91: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

paradigmover tool

Page 92: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

summary

Page 93: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

immediately beneficial beginning steps

new ways of thinking about design

new tools for extension, reuse, etc.

enables entirely new capabilities

following the general trend in language design

functional thinking

Page 94: Functional Thinking - 33rd Degree33degree.org/pdf/NealFordFuntionalThinking.pdf · OH on . OO makes code understandable by encapsulating moving parts. FP makes code ... affect the

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 License.

http://creativecommons.org/licenses/by-sa/3.0/us/

?’splease fill out the session evaluations

NEAL FORD software architect / meme wrangler

ThoughtWorks®

[email protected] 3003 Summit Boulevard, Atlanta, GA 30319 www.nealford.com www.thoughtworks.com blog: memeagora.blogspot.com twitter: neal4d