new newton’s laws of motion - lispcast · 2018. 2. 26. · newton’s laws of motion 1. inertia...
TRANSCRIPT
Newton’s Laws of Motion
1. Inertia
2. Acceleration
3. Action-reaction
Force
Mass
Distance
Time
Aristotelian Physics(excerpt)
Ideal speed
Natural place
Natural motion
Unnatural motion
Why use Functional Programming?
What is Functional Programming?
A Theory of Functional
Programming
Eric Normand
LambdUp December 6, 2017
paradigm
a philosophical and theoretical framework of a scientific school or discipline within which theories, laws, and generalizations and the experiments performed in support of them are formulated
Merriam-Webster
philosophical or theoretical framework,
world view
theories, laws, generalizations
basic assumptions, ways of thinking, methodology
Why use Functional Programming?
What is Functional Programming?
Goals of my Theory
Goals of my Theory• Explain what it is we (functional programmers) actually do
Goals of my Theory• Explain what it is we (functional programmers) actually do
• in terms we can all understand
Goals of my Theory• Explain what it is we (functional programmers) actually do
• in terms we can all understand
• Explain why it has advantages over other paradigms
Goals of my Theory• Explain what it is we (functional programmers) actually do
• in terms we can all understand
• Explain why it has advantages over other paradigms
• to people who haven’t done FP
Goals of my Theory• Explain what it is we (functional programmers) actually do
• in terms we can all understand
• Explain why it has advantages over other paradigms
• to people who haven’t done FP
• Avoid focusing on features
Goals of my Theory• Explain what it is we (functional programmers) actually do
• in terms we can all understand
• Explain why it has advantages over other paradigms
• to people who haven’t done FP
• Avoid focusing on features
• Give explanatory and predictive power
Goals of my Theory• Explain what it is we (functional programmers) actually do
• in terms we can all understand
• Explain why it has advantages over other paradigms
• to people who haven’t done FP
• Avoid focusing on features
• Give explanatory and predictive power
• Self-described functional programmers should agree
My Theory of FP
My Theory of FP
Actions
My Theory of FP
Actions Data
My Theory of FP
Actions Data Calculations
Actions
Actionsthe process of doing something, typically to achieve an aim
Actionsthe process of doing something, typically to achieve an aim
• Typically called Effects or Side-effects
Actionsthe process of doing something, typically to achieve an aim
• Typically called Effects or Side-effects
• Depend on when you run them or how many times you run them
Actionsthe process of doing something, typically to achieve an aim
• Typically called Effects or Side-effects
• Depend on when you run them or how many times you run them
• Examples
Actionsthe process of doing something, typically to achieve an aim
• Typically called Effects or Side-effects
• Depend on when you run them or how many times you run them
• Examples
• Sending a message over the network
Actionsthe process of doing something, typically to achieve an aim
• Typically called Effects or Side-effects
• Depend on when you run them or how many times you run them
• Examples
• Sending a message over the network
• Writing to file system — other programs can see the change
Actionsthe process of doing something, typically to achieve an aim
• Typically called Effects or Side-effects
• Depend on when you run them or how many times you run them
• Examples
• Sending a message over the network
• Writing to file system — other programs can see the change
• Changing or reading mutable state
Data
Datafactual information used as a basis for reasoning, discussion, or calculation
Datafactual information used as a basis for reasoning, discussion, or calculation
• Inert
Datafactual information used as a basis for reasoning, discussion, or calculation
• Inert
• Serializable
Datafactual information used as a basis for reasoning, discussion, or calculation
• Inert
• Serializable
• Requiring interpretation
Datafactual information used as a basis for reasoning, discussion, or calculation
• Inert
• Serializable
• Requiring interpretation
• Examples
Datafactual information used as a basis for reasoning, discussion, or calculation
• Inert
• Serializable
• Requiring interpretation
• Examples
• Numbers
Datafactual information used as a basis for reasoning, discussion, or calculation
• Inert
• Serializable
• Requiring interpretation
• Examples
• Numbers
• Bytes
Datafactual information used as a basis for reasoning, discussion, or calculation
• Inert
• Serializable
• Requiring interpretation
• Examples
• Numbers
• Bytes
• Strings
Datafactual information used as a basis for reasoning, discussion, or calculation
• Inert
• Serializable
• Requiring interpretation
• Examples
• Numbers
• Bytes
• Strings
• Collections
Calculations
Calculationscomputation from inputs to outputs
Calculationscomputation from inputs to outputs
• Mathematical functions
Calculationscomputation from inputs to outputs
• Mathematical functions
• Eternal — outside of time
Calculationscomputation from inputs to outputs
• Mathematical functions
• Eternal — outside of time
• Referentially transparent
Calculationscomputation from inputs to outputs
• Mathematical functions
• Eternal — outside of time
• Referentially transparent
• Examples
Calculationscomputation from inputs to outputs
• Mathematical functions
• Eternal — outside of time
• Referentially transparent
• Examples
• List concatenation
Calculationscomputation from inputs to outputs
• Mathematical functions
• Eternal — outside of time
• Referentially transparent
• Examples
• List concatenation
• Summing numbers
Contrast with OOP
OOP
OOP
Objects
OOP
Objects References
OOP
Objects References Messages
ImplementationHaskell
• Data — built-in types and defined types
• Calculations — functions
• Actions — IO type
ImplementationClojure
• Data — built-in types
• Calculations — pure functions
• Actions — impure functions
Further down the rabbit hole
• Everything “First-class”
• Data
• Calculations
• Actions
• Minimum necessary to program functionally in a language
Domains are separate
Domains are separateData
Domains are separateData
Data + Data => Data
Domains are separateData
Data + Data => Data
Examples
Domains are separateData
Data + Data => Data
Examples
• Addition
Domains are separateData
Data + Data => Data
Examples
• Addition
• Concatenation
Domains are separateData
Data + Data => Data
Examples
• Addition
• Concatenation
Calculations
Domains are separateData
Data + Data => Data
Examples
• Addition
• Concatenation
Calculations
Calc + Calc => Calc
Domains are separateData
Data + Data => Data
Examples
• Addition
• Concatenation
Calculations
Calc + Calc => Calc
Examples
Domains are separateData
Data + Data => Data
Examples
• Addition
• Concatenation
Calculations
Calc + Calc => Calc
Examples
• compose
Domains are separateData
Data + Data => Data
Examples
• Addition
• Concatenation
Calculations
Calc + Calc => Calc
Examples
• compose
• juxtapose
Domains are separateData
Data + Data => Data
Examples
• Addition
• Concatenation
Calculations
Calc + Calc => Calc
Examples
• compose
• juxtapose
Actions
Domains are separateData
Data + Data => Data
Examples
• Addition
• Concatenation
Calculations
Calc + Calc => Calc
Examples
• compose
• juxtapose
Actions
Actn + Actn => Actn
Domains are separateData
Data + Data => Data
Examples
• Addition
• Concatenation
Calculations
Calc + Calc => Calc
Examples
• compose
• juxtapose
Actions
Actn + Actn => Actn
Examples
Domains are separateData
Data + Data => Data
Examples
• Addition
• Concatenation
Calculations
Calc + Calc => Calc
Examples
• compose
• juxtapose
Actions
Actn + Actn => Actn
Examples
• in sequence
Domains are separateData
Data + Data => Data
Examples
• Addition
• Concatenation
Calculations
Calc + Calc => Calc
Examples
• compose
• juxtapose
Actions
Actn + Actn => Actn
Examples
• in sequence
• in parallel
Actions
Actions• Contagious!
Actions• Contagious!
• Calculation + Action => Action
Actions• Contagious!
• Calculation + Action => Action
• Data + Action => Action
Actions• Contagious!
• Calculation + Action => Action
• Data + Action => Action
• Examples
Actions• Contagious!
• Calculation + Action => Action
• Data + Action => Action
• Examples
• Print the square of a number — square => print!
Actions• Contagious!
• Calculation + Action => Action
• Data + Action => Action
• Examples
• Print the square of a number — square => print!
• Parse the input as a number — read! => parse
Calculations
Calculations• Algebraic manipulation
Calculations• Algebraic manipulation
• Turing complete
Calculations• Algebraic manipulation
• Turing complete
• implies the Halting problem
Calculations• Algebraic manipulation
• Turing complete
• implies the Halting problem
• Opaque
Calculations• Algebraic manipulation
• Turing complete
• implies the Halting problem
• Opaque
• What is this code going to do?
Calculations• Algebraic manipulation
• Turing complete
• implies the Halting problem
• Opaque
• What is this code going to do?
• Only way to know is to run it
Data
• Can represent something else
• Structure
• Known Big-O complexities
RefactoringsActions
• Action => Action + Calculation
• Action => Action + Data
• Action => Action + Action
Calculations
• Calculation => Calculation + Data
• Calculation => Calculation + Calculation
Actions are universal
Actions
Calculations
Data
What counts as an Action?
Calculations
Timeless
Actions
Bound in time
What counts as an Action?
Calculations
Timeless
Actions
Bound in time
Pure function
What counts as an Action?
Calculations
Timeless
Actions
Bound in time
Pure function Read/write to disk
What counts as an Action?
Calculations
Timeless
Actions
Bound in time
Pure function Read/write to disk
Pure function
What counts as an Action?
Calculations
Timeless
Actions
Bound in time
Pure function Read/write to disk
Pure functiontakes 24 hours to compute
What counts as an Action?
Calculations
Timeless
Actions
Bound in time
Pure function Read/write to disk
Pure functiontakes 24 hours to compute
What counts as an Action?
Calculations
Timeless
Actions
Bound in time
Pure function Read/write to disk
Pure functiontakes 24 hours to compute
Read/write to temp file as buffer
What counts as an Action?
Calculations
Timeless
Actions
Bound in time
Pure function Read/write to disk
Pure functiontakes 24 hours to compute
Read/write to temp file as buffer
Actions how many times they run
Actions how many times they run
always matters - 0≠1≠more
Actions how many times they run
always matters - 0≠1≠morelaunching a missile
Actions how many times they run
always matters - 0≠1≠morelaunching a missilesending an email
Actions how many times they run
always matters - 0≠1≠morelaunching a missilesending an email
idempotent - 0≠1=more
Actions how many times they run
always matters - 0≠1≠morelaunching a missilesending an email
idempotent - 0≠1=moresetting public flag to true
Actions how many times they run
always matters - 0≠1≠morelaunching a missilesending an email
idempotent - 0≠1=moresetting public flag to true
free of side-effects - 0=1=more
Actions how many times they run
always matters - 0≠1≠morelaunching a missilesending an email
idempotent - 0≠1=moresetting public flag to true
free of side-effects - 0=1=moreGET request
Actions how many times they run
always matters - 0≠1≠morelaunching a missilesending an email
idempotent - 0≠1=moresetting public flag to true
free of side-effects - 0=1=moreGET request
reading mutable state
Actions when they run
Actions when they run
transactional read
Actions when they run
transactional readguaranteed to be consistent
Actions when they run
transactional readguaranteed to be consistent
transactional+serialized writes
Actions when they run
transactional readguaranteed to be consistent
transactional+serialized writesOrder matters, but at least it’s some order
Actions when they run
transactional readguaranteed to be consistent
transactional+serialized writesOrder matters, but at least it’s some order
exactly once reads
Actions when they run
transactional readguaranteed to be consistent
transactional+serialized writesOrder matters, but at least it’s some order
exactly once readsCommunicating Sequential Processes