# Fate and functional programming

50 %
50 %
Information about Fate and functional programming

Published on October 31, 2013

Author: rjose

Source: slideshare.net

## Description

Introduction to functional programming concepts and their application to data. Discuss pros/cons of a functional style. Discuss relationship between functional programming and the nature of the universe.

Fate Free will Functional πrogramming Rino Jose Thursday, October 31, 13 1

Deﬁnitions Thursday, October 31, 13 2

Deﬁnitions Set: Collection of unique elements Thursday, October 31, 13 2

Deﬁnitions Set: Collection of unique elements Function: Rule that associates elements in one set with those in another α β γ π Thursday, October 31, 13 2

Function: digitOfPi Thursday, October 31, 13 3

Function: digitOfPi digitOfPi :: Int -> Digit Thursday, October 31, 13 3

Function: digitOfPi digitOfPi :: Int -> Digit 1 2 3 4 ∶ Thursday, October 31, 13 0 1 2 3 4 5 6 7 8 9 3

Function: digitOfPi digitOfPi :: Int -> Digit 1 2 3 4 ∶ Thursday, October 31, 13 0 1 2 3 4 5 6 7 8 9 3

Algorithm is Irrelevant Thursday, October 31, 13 4

Algorithm is Irrelevant • Only the association/mapping matters • Thursday, October 31, 13 May need an algorithm to ﬁnd the mapping 4

Algorithm is Irrelevant • Only the association/mapping matters • • Once mapping is known, it can be used in place of the algo without changing any behavior • • Thursday, October 31, 13 May need an algorithm to ﬁnd the mapping This is “referential transparency” Implies no side effects when algorithm is run 4

Algorithm is Irrelevant • Only the association/mapping matters • • Once mapping is known, it can be used in place of the algo without changing any behavior • • • This is “referential transparency” Implies no side effects when algorithm is run Example: digits of π • • Thursday, October 31, 13 May need an algorithm to ﬁnd the mapping Algorithms exist for computing digits of π But digits are predetermined by how π is deﬁned 4

π? Thursday, October 31, 13 5

π? Thursday, October 31, 13 5

π? Thursday, October 31, 13 5

π? π= Thursday, October 31, 13 circumference diameter 5

Algo is Irrelevant • Only the association/mapping matters • • But once mapping is known, it can be used in place of the algo without changing any behavior • • • This is “referential transparency” Implies no side effects from algos used Example: digits of π • • Thursday, October 31, 13 May need an algorithm to ﬁnd the mapping Algorithms exist for computing digits of π However, digits of π exist by deﬁnition of π 6

Algo is Irrelevant • Only the association/mapping matters • • But once mapping is known, it can be used in place of the algo without changing any behavior • • • This is “referential transparency” Implies no side effects from algos used Example: digits of π • • Thursday, October 31, 13 May need an algorithm to ﬁnd the mapping Algorithms exist for computing digits of π However, digits of π exist by deﬁnition of π 6

Multiple Arguments? Thursday, October 31, 13 7

Multiple Arguments? sliceOfPi start num -> Int sliceOfPi 2 3 = 141 Thursday, October 31, 13 7

Multiple Arguments? sliceOfPi start num -> Int sliceOfPi 2 3 = 141 1 2 3 ∶ 1 2 ∶ 141 ∶ start Thursday, October 31, 13 1 2 3 ∶ num Int 7

Multiple Arguments? sliceOfPi start num -> Int sliceOfPi 2 3 = 141 sliceOfPi 2 3 = firstNDigitsOfPiStartingAtDigit2 3 = 141 sliceOfPi 2 3 = f2 3 = 141 1 2 3 ∶ start Thursday, October 31, 13 f1 f2 f3 1 2 3 ∶ 1 2 ∶ 141 ∶ num Int 7

Multiple Arguments? What if we called sliceOfPi with only one argument? 1 2 3 ∶ Thursday, October 31, 13 f1 f2 f3 8

Multiple Arguments? What if we called sliceOfPi with only one argument? sliceOfPi 2 = f2 1 2 3 ∶ Thursday, October 31, 13 f1 f2 f3 8

Multiple Arguments? What if we called sliceOfPi with only one argument? sliceOfPi 2 = f2 Partial function application 1 2 3 ∶ Thursday, October 31, 13 f1 f2 f3 8

Multiple Arguments? What if we called sliceOfPi with only one argument? sliceOfPi 2 = f2 Partial function application a.k.a. currying after Haskell Curry 1 2 3 ∶ Thursday, October 31, 13 f1 f2 f3 8

Multiple Arguments? This looks very similar to a unix pipeline 1 2 3 ∶ Thursday, October 31, 13 f1 f2 f3 1 2 3 ∶ 1 2 ∶ 141 ∶ 9

Multiple Arguments? This looks very similar to a unix pipeline 1 2 3 ∶ Thursday, October 31, 13 stdin f1 f2 f3 args 1 2 3 ∶ stdout 1 2 ∶ 141 ∶ 9

Pipelines are Functional Thursday, October 31, 13 10

Pipelines are Functional Each step in a Unix pipeline is a function call: ls . | grep r | grep i | grep n | grep o | wc -l Thursday, October 31, 13 10

Pipelines are Functional Each step in a Unix pipeline is a function call: ls . | grep r | grep i | grep n | grep o | wc -l ls :: Dir -> String grep :: String -> String -> String wc :: String -> String Thursday, October 31, 13 10

Pipelines are Functional Each step in a Unix pipeline is a function call: ls . | grep r | grep i | grep n | grep o | wc -l ls :: Dir -> String grep :: String -> String -> String wc :: String -> String Pipelines are referentially transparent: ls | grep r | grep i > value.txt cat value.txt | grep n | grep o | wc -l Thursday, October 31, 13 10

ETL is a pipeline Thursday, October 31, 13 11

ETL is a pipeline Extract | Transform | Load Thursday, October 31, 13 11

ETL is a pipeline Extract | Transform | Load E Thursday, October 31, 13 T L 11

ETL is a pipeline Extract | Transform | Load E T L Why not just Extract and Load? Thursday, October 31, 13 11

ETL is a pipeline Extract | Transform | Load E T L Why not just Extract and Load? The transform step makes data more regular, reducing the nondeterminism in the input data sources. Thursday, October 31, 13 11

Nondeterminism Thursday, October 31, 13 12

Nondeterminism Nondeterminism: randomness in input and output Thursday, October 31, 13 12

Nondeterminism Nondeterminism: randomness in input and output digitOfPi :: Int -> Digit Thursday, October 31, 13 No uncertainty 12

Nondeterminism Nondeterminism: randomness in input and output digitOfPi :: Int -> Digit n ∈ {1, 2, 4} Thursday, October 31, 13 No uncertainty Input uncertainty 12

Nondeterminism Nondeterminism: randomness in input and output digitOfPi :: Int -> Digit n ∈ {1, 2, 4} digitOfPi n ∈ {1, 3} Thursday, October 31, 13 No uncertainty Input uncertainty Output uncertainty 12

Nondeterminism Nondeterminism: randomness in input and output digitOfPi :: Int -> Digit No uncertainty n ∈ {1, 2, 4} digitOfPi n ∈ {1, 3} Input uncertainty Output uncertainty What is the result? 1 or 3? Thursday, October 31, 13 12

Nondeterminism Nondeterminism: randomness in input and output digitOfPi :: Int -> Digit No uncertainty n ∈ {1, 2, 4} digitOfPi n ∈ {1, 3} Input uncertainty Output uncertainty What is the result? 1 or 3? Domain of s uantum computer q Thursday, October 31, 13 12

Deepank’s Roulette Thursday, October 31, 13 13

Deepank’s Roulette • Thursday, October 31, 13 Deepank is in a room with a roulette wheel 13

Deepank’s Roulette • • Thursday, October 31, 13 Deepank is in a room with a roulette wheel Bets on red 13

Deepank’s Roulette • • • Thursday, October 31, 13 Deepank is in a room with a roulette wheel Bets on red Spins wheel 13

Deepank’s Roulette • • • Deepank is in a room with a roulette wheel Bets on red Spins wheel • Thursday, October 31, 13 If lands on red, 13

Deepank’s Roulette • • • Deepank is in a room with a roulette wheel Bets on red Spins wheel • Thursday, October 31, 13 If lands on red, he takes his winnings home 13

Deepank’s Roulette • • • Deepank is in a room with a roulette wheel Bets on red Spins wheel • • Thursday, October 31, 13 If lands on red, he takes his winnings home If lands on black, 13

Deepank’s Roulette • • • Deepank is in a room with a roulette wheel Bets on red Spins wheel • • Thursday, October 31, 13 If lands on red, he takes his winnings home If lands on black, he dies 13

Deepank’s Roulette • • • Deepank is in a room with a roulette wheel Bets on red Spins wheel • • • Thursday, October 31, 13 If lands on red, he takes his winnings home If lands on black, he dies Close door and spin wheel 13

Deepank’s Roulette • • • Deepank is in a room with a roulette wheel Bets on red Spins wheel • • If lands on red, he takes his winnings home If lands on black, he dies • • Thursday, October 31, 13 Close door and spin wheel Is Deepank richer or dead? 13

Deepank’s Roulette • • • Deepank is in a room with a roulette wheel Bets on red Spins wheel • • • If lands on red, he takes his winnings home If lands on black, he dies Close door and spin wheel \$\$ • Thursday, October 31, 13 Is Deepank richer or dead? 13

Deepank’s Roulette • • • Deepank is in a room with a roulette wheel Bets on red Spins wheel • • • If lands on red, he takes his winnings home If lands on black, he dies Close door and spin wheel • Thursday, October 31, 13 xx ( \$\$ Is Deepank richer or dead? 13

Deepank’s Roulette • • • Deepank is in a room with a roulette wheel Bets on red Spins wheel • • • If lands on red, he takes his winnings home If lands on black, he dies Close door and spin wheel • Is Deepank richer or dead? • Thursday, October 31, 13 xx ( \$\$ Both − depends on the universe 13

Multiple Universes Thursday, October 31, 13 14

Multiple Universes In any given universe, everything that has ever happened or ever will happen exists independently and unchangeable. Thursday, October 31, 13 14

Multiple Universes In any given universe, everything that has ever happened or ever will happen exists independently and unchangeable. Just like the digits of pi 3.141592653589793238462643383279502884197169399375 10582097494459230781640628620899862803482534211706 79821480865132823066470938446095505822317253594081 28481117450284102701938521105559644622948954930381 96442881097566593344612847564823378678316527120190 91456485669234603486104543266482133936072602491412 73724587006606315588174881520920962829254091715364 36789259036001133053054882046652138414695194151160 94330572703657595919530921861173819326117931051185 Thursday, October 31, 13 14

Fate and π Thursday, October 31, 13 15

Fate and π Conjecture: π contains all ﬁnite sequences of digits. Thursday, October 31, 13 15

Fate and π Conjecture: π contains all ﬁnite sequences of digits. Profound if true Thursday, October 31, 13 15

Fate and π Conjecture: π contains all ﬁnite sequences of digits. Profound if true π contains... Thursday, October 31, 13 15

Fate and π Conjecture: π contains all ﬁnite sequences of digits. Profound if true π contains... • ...a transcript of everything you will ever say Thursday, October 31, 13 15

Fate and π Conjecture: π contains all ﬁnite sequences of digits. Profound if true π contains... • ...a transcript of everything you will ever say • ...a tarball of next week’s mobile source code Thursday, October 31, 13 15

Fate and π Conjecture: π contains all ﬁnite sequences of digits. Profound if true π contains... • ...a transcript of everything you will ever say • ...a tarball of next week’s mobile source code • ...a video of you with thought bubbles...in Spanish Thursday, October 31, 13 15

Fate and π Conjecture: π contains all ﬁnite sequences of digits. Profound if true π contains... • ...a transcript of everything you will ever say • ...a tarball of next week’s mobile source code • ...a video of you with thought bubbles...in Spanish • ...all human knowledge and all variations of it Thursday, October 31, 13 15

Fate and π Conjecture: π contains all ﬁnite sequences of digits. Profound if true π contains... • ...a transcript of everything you will ever say • ...a tarball of next week’s mobile source code • ...a video of you with thought bubbles...in Spanish • ...all human knowledge and all variations of it If true, our existence is probably part of an algorithm to compute the digits of π. Thursday, October 31, 13 15

Fate and π Conjecture: π contains all ﬁnite sequences of digits. Profound if true π contains... • ...a transcript of everything you will ever say • ...a tarball of next week’s mobile source code • ...a video of you with thought bubbles...in Spanish • ...all human knowledge and all variations of it If true, our existence is probably part of an algorithm to compute the digits of π. Thursday, October 31, 13 15

Free Will Thursday, October 31, 13 16

Free Will Thursday, October 31, 13 \$\$ xx ( Multiverse Theory There is a universe associated with every combination of nondeterminism. 16

Free Will \$\$ xx ( Multiverse Theory There is a universe associated with every combination of nondeterminism. Terminator principle There is no fate but what we make. Thursday, October 31, 13 16

Free Will \$\$ xx ( Multiverse Theory There is a universe associated with every combination of nondeterminism. Terminator principle There is no fate but what we make. Make good choices We select our universe by making choices. Thursday, October 31, 13 16

Free Will \$\$ xx ( Multiverse Theory There is a universe associated with every combination of nondeterminism. Terminator principle There is no fate but what we make. Make good choices We select our universe by making choices. This may also xplain jinxes e Thursday, October 31, 13 16

Nondeterminism Nondeterminism: randomness in input and output digitOfPi :: Int -> Digit No uncertainty n ∈ {1, 2, 4} digitOfPi n ∈ {1, 3} Input uncertainty Output uncertainty What is the result? 1 or 3? se case for U computers quantum Thursday, October 31, 13 17

Nondeterminism in SW Thursday, October 31, 13 18

Nondeterminism in SW Nondeterminism makes testing and debugging harder Thursday, October 31, 13 18

Nondeterminism in SW Nondeterminism makes testing and debugging harder • More scenarios to set up • More cases to check • More universes to consider Thursday, October 31, 13 18

Nondeterminism in SW Nondeterminism makes testing and debugging harder • More scenarios to set up • More cases to check • More universes to consider Sources of nondeterminism Thursday, October 31, 13 18

Nondeterminism in SW Nondeterminism makes testing and debugging harder • More scenarios to set up • More cases to check • More universes to consider Sources of nondeterminism • Side effects • Un-DRY code • Breaking “Law of Demeter” • Irregular return values Thursday, October 31, 13 18

Nondeterminism in SW Nondeterminism makes testing and debugging harder • More scenarios to set up • More cases to check • More universes to consider Sources of nondeterminism • Side effects • Un-DRY code • Breaking “Law of Demeter” • Irregular return values Ask Yourself Is this function increasing or reducing nondeterminism? Thursday, October 31, 13 18

Reduce Nondeterminism Thursday, October 31, 13 19

Reduce Nondeterminism •Use ETL as a model Thursday, October 31, 13 19

Reduce Nondeterminism •Use ETL as a model •Make functions referentially transparent Thursday, October 31, 13 19

Reduce Nondeterminism •Use ETL as a model •Make functions referentially transparent •Isolate code that changes state Thursday, October 31, 13 19

Reduce Nondeterminism •Use ETL as a model •Make functions referentially transparent •Isolate code that changes state •Ensure function results are regular • Use placeholders so callers won’t need to check • Keep the result set as unvaried as possible • Unleash your unit tests here Thursday, October 31, 13 19

Reduce Nondeterminism •Use ETL as a model •Make functions referentially transparent •Isolate code that changes state •Ensure function results are regular • Use placeholders so callers won’t need to check • Keep the result set as unvaried as possible • Unleash your unit tests here Reducing nondeterminism lets you make stronger assumptions about the code. Thursday, October 31, 13 19

Databending Thursday, October 31, 13 20

Databending Thursday, October 31, 13 20

Databending Extract Raw Data Thursday, October 31, 13 20

Databending Extract Raw Data Condition Data Thursday, October 31, 13 20

Databending Bend Data Extract Raw Data Condition Data Thursday, October 31, 13 20

Databending Load Data Bend Data Extract Raw Data Condition Data Thursday, October 31, 13 20

Databending Load Data Bend Data Extract Raw Data Condition Data Snapshot Service • Data is snapshotted Thursday, October 31, 13 20

Databending Load Data Bend Data Extract Raw Data Condition Data Snapshot Service • Data is snapshotted • Stages provide robustness Thursday, October 31, 13 20

Databending Load Data Bend Data Extract Raw Data Condition Data Snapshot Service • Data is snapshotted • Stages provide robustness • Nondeterminism decreases Thursday, October 31, 13 20

Takeaway Thoughts Thursday, October 31, 13 21

Takeaway Thoughts • Thursday, October 31, 13 Use functional ideas to reduce nondeterminism 21

Takeaway Thoughts • • Thursday, October 31, 13 Use functional ideas to reduce nondeterminism Make good choices to select good universes 21

Takeaway Thoughts • • Make good choices to select good universes • Thursday, October 31, 13 Use functional ideas to reduce nondeterminism Don’t play roulette with Deepank 21

Thursday, October 31, 13 22

uestions? Thursday, October 31, 13 22

## Add a comment

 User name: Comment:

## Related pages

### Fate of lambda, Functional Programming in Python... - Python

Fate of lambda, Functional Programming in Python.... Python Forums on Bytes.

### Functional Programming - ResearchGate

Functional Programming on ResearchGate, the professional network for scientists.

### Fate of itertools.dropwhile() and itertools.takewhile ...

Fate of itertools.dropwhile() and itertools.takewhile(). ... functional programming. Personally, I'd rather you kept them around. I have no FP background,

### glossary - Functional programming and non-functional ...

In my second year of University we were "taught" Haskell, I know almost nothing about it and even less about functional programming. What is functional ...

### Cell fate programming and reprogramming; generation of ...

Denna sida på svenska. Cell fate programming and reprogramming; generation of functional neurons in a dish. Principal investigator; Parmar, Malin, , PhD

### Leveling Waddington: the emergence of direct programming ...

Leveling Waddington: the emergence of direct programming and the loss of cell fate hierarchies. ... Describes the induction of functional neurons, ...

### knowing why functional programming matters - Raganwald

Comments on “Why Why Functional Programming Matters Matters ... a functional (thus, safe ... but its only fate is to yield its length and get eaten ...