when life gives you functions make functional programs!
TRANSCRIPT
![Page 1: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/1.jpg)
When Life Gives you Functions
Make Functional Programs!aaron levin07.09.2016
![Page 2: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/2.jpg)
Thank you Ellen, Duana, and Open Tech School!
![Page 3: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/3.jpg)
Outline
1. Who is Aaron Levin?
2. What is Functional Programming?
3. How Functional Programming Can Benefit You
4.🎉🎉🎉🎉
![Page 4: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/4.jpg)
Who is Aaron Levin
MSc Pure Mathematics
Started Programming at age 30 👴
Building Recommender Systems at SoundCloud
But also have worked as:Taco Conjurer @ TacoTime 🌮Telemarketer @ The WeedMan 🌿Rare Record Dealer @ eBay ⏺Music Director @ Community Radio Station 📻
![Page 5: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/5.jpg)
Who is Aaron Levin (really)
First programming job in Scala.
Was once paid actual Real World™ money to write Haskell
Wrote authentication framework for http library servant.
Author of a few Haskell libraries:- free-vl- haskell-kubernetes
![Page 6: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/6.jpg)
What is Functional Programming?
![Page 7: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/7.jpg)
Wait, what is a function?
![Page 8: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/8.jpg)
What is a function?
Maths: a function maps input to output.
f : R -> Rf(x) = x * x
Programming: a function receives input, performs actions, returns output.
int main( int argc, const char* argv[] ) {printf( "\nHello World\n\n" );
}
OO Programming: a function is a method on an object
class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); // Display the string. }}
![Page 9: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/9.jpg)
How can we program with functions?
![Page 10: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/10.jpg)
Functional Programming LanguagesFunctional Languages
LISP
Clojure
Erlang
Haskell
OCaml
SML
Languages with Functional Features
Javascript
Ruby
Python
Java (8)
C++Who can know what to make of this one?
![Page 11: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/11.jpg)
With so many functional languages, what makes a language functional?
![Page 12: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/12.jpg)
Let’s ask celebrity programmers!“You are programming with functions? Then you are doing functional programming. And your language is a functional programming language.”- @raganwald
“A programming paradigm, a coding style, a mindset, a sexy, buzz-wordy trend”- @AnjanaVakil
“Functional programming is a style of programming which models computations as the evaluation of expressions.”- Haskell.org
![Page 13: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/13.jpg)
Let’s Ask (more) Celebrity Programmers
“Functional programming is about writing pure functions, about removing hidden inputs and outputs as far as we can, so that [our code just describes] a relationship between inputs and outputs.”- @krisajenkins
“What if instead of telling the computer what to do, we told the computer what things are.”- @_K_E_L_S_E_Y
“2+2 is functional programming [...] the proper OO way is:x = new int(2); x.add(2); return x;”- @chris__martin
![Page 14: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/14.jpg)
To make matters worse...
![Page 15: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/15.jpg)
Things Associated With Functional Programming
Higher-Order Functions
Immutability
Purity
Pattern Matching
Lazy Evaluation
Recursion
Type Inference
Algebraic Data Types
![Page 16: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/16.jpg)
...
![Page 17: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/17.jpg)
Take a step backbreatheLet’s start over
![Page 18: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/18.jpg)
What is Functional Programming?
![Page 19: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/19.jpg)
There are lots of things to program with:
objects, statements, relations, instructions, messages
![Page 20: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/20.jpg)
There are lots of things to program with:
functions!
![Page 21: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/21.jpg)
Functional Programming Is
1. Programming with Functions that are Higher-Order
2. Programming with Data that is Immutable
3. Designing Systems that minimize the location of Side-Effects
![Page 22: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/22.jpg)
Programming with Higher-Order Functions
A Higher-Order Function may take another function as an argument.
Allows you to pass functions to functions!
Most prevalent example: call-backs.
![Page 23: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/23.jpg)
Programming with Higher-Order Functions
Traditional
var nums = [1, 2, 3];var result = [];
for(num in nums) { result.push(num * 10);}
Functional
var nums = [1, 2, 3];
var result = nums.map(function(num) { return num * 10; });
[1, 2, 3] ---> [10, 20, 30]
![Page 24: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/24.jpg)
Programming with Higher-Order Functions
Benefits:
1. No indexing / out-of-bounds issues
2. Don’t care about array structure
3. Don’t care about how to iterate through structure
4. Could work for anything that can be traversed! Trees, etc.
![Page 25: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/25.jpg)
Programming with Immutable Data
Immutable data cannot be changed after it is created.
Cannot score++ with an immutable score!
![Page 26: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/26.jpg)
Programming with Immutable Datadef collision_points(self, other): if self.collide(other) and other.is_coin: state.high_score++ elif self.collide(other): state.high_score = 0
def collision_points(self, other, state): new_state = copy(state) if self.collide(other) and other.is_coin: new_state.high_score++ elif self.collide(other): new_state.high_score = 0 return new_state
![Page 27: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/27.jpg)
Programming with Immutable Datadef collision_points(self, other): if self.collide(other) and other.is_coin: state.high_score = state.high_score + 1 elif self.collide(other): state.high_score = 0
def collision_points(self, other): if self.collide(other) and other.is_coin: return {‘mutate’: ‘increment’, ‘amount’: 1} elif self.collide(other): return {‘mutate’: ‘reset’, ‘amount’: 0} else: return None
return facts!
a description of how to change the world!
![Page 28: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/28.jpg)
Programming with Immutable Data
Benefits
1. Know exactly where and how state is modified
2. Easier to test
3. Can share data with concurrent threads / processes safely
4. Easily parallelizable
5. Easily persisted to disk
6. Basically everything is 100000000% satisfaction guaranteed better when things are immutable
![Page 29: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/29.jpg)
Minimize Location of Side-Effects
A function is said to have a Side-Effect if it mutates state or has an observable interaction.
public static void main(String[] args) { System.out.println("Hello World!"); // ← Side Effect! }
Pure functions don’t have Side Effects (Referential Transparency)
Handling side-effects is necessary for interacting with the real world!
![Page 30: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/30.jpg)
Minimize Location of Side-Effects
![Page 31: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/31.jpg)
Values
Pure function
Function interfacing with outside world
Minimize Location of Side-Effects
The Outside World™
Manage Program State Open filesConnect to DBReceive HTTP Req.Receive Mouse InputTalk to GPURead stdinOutput to stdoutLaunch programsSend UDP packets
request.body.rewind
request.body
![Page 32: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/32.jpg)
Minimize Location of Side-Effects
Benefits
1. Don’t have to remember things
2. How do I know if someone “already read it”? Don’t have to understand the entire program!
3. Test core algorithms more easily
![Page 33: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/33.jpg)
RecapWhat is Functional Programming?
![Page 34: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/34.jpg)
Functional Programming Is
1. Programming with Functions that are Higher-Order
2. Programming with Data that is Immutable
3. Designing Systems that minimize the location of Side-Effects
![Page 35: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/35.jpg)
Why don’t we always program with functions?
![Page 36: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/36.jpg)
Why Don’t We Always Program With Functions?
Some languages don’t support it (Java <7)
Requires thinking very deeply about program architecture.
Is not always performant
Many APIs are written in a non-FP style (e.g. java’s old Time library)
![Page 37: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/37.jpg)
What are the benefits of Functional Programming?
![Page 38: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/38.jpg)
Benefits of Functional Programming
Safer
Easier to understand your programs
Easier to debug your programs
Easier to test your programs
Make APIs easier to use
Make static typing systems more powerful
It’s fun!
It’s simple!
It’s hard!
![Page 39: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/39.jpg)
What next?
![Page 40: When life gives you functions make functional programs!](https://reader035.vdocuments.us/reader035/viewer/2022062902/58ef150a1a28abad7f8b46c9/html5/thumbnails/40.jpg)
What Next?
1. Try re-writing programs in a more functional style!
2. Learn a functional language! (Haskell or OCaml)
3. How to encode data (numbers, lists, strings) as functions! (Church-encoding)
Thanks!
Ende.