when life gives you functions make functional programs!

40
When Life Gives you Functions Make Functional Programs! aaron levin 07.09.2016

Upload: aaron-levin

Post on 13-Apr-2017

63 views

Category:

Science


2 download

TRANSCRIPT

Page 1: When life gives you functions make functional programs!

When Life Gives you Functions

Make Functional Programs!aaron levin07.09.2016

Page 2: When life gives you functions make functional programs!

Thank you Ellen, Duana, and Open Tech School!

Page 3: When life gives you functions make functional programs!

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!

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!

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!

What is Functional Programming?

Page 7: When life gives you functions make functional programs!

Wait, what is a function?

Page 8: When life gives you functions make functional programs!

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!

How can we program with functions?

Page 10: When life gives you functions make functional programs!

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!

With so many functional languages, what makes a language functional?

Page 12: When life gives you functions make functional programs!

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!

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!

To make matters worse...

Page 15: When life gives you functions make functional programs!

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!

...

Page 17: When life gives you functions make functional programs!

Take a step backbreatheLet’s start over

Page 18: When life gives you functions make functional programs!

What is Functional Programming?

Page 19: When life gives you functions make functional programs!

There are lots of things to program with:

objects, statements, relations, instructions, messages

Page 20: When life gives you functions make functional programs!

There are lots of things to program with:

functions!

Page 21: When life gives you functions make functional programs!

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!

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!

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!

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!

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!

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!

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!

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!

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!

Minimize Location of Side-Effects

Page 31: When life gives you functions make functional programs!

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!

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!

RecapWhat is Functional Programming?

Page 34: When life gives you functions make functional programs!

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!

Why don’t we always program with functions?

Page 36: When life gives you functions make functional programs!

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!

What are the benefits of Functional Programming?

Page 38: When life gives you functions make functional programs!

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!

What next?

Page 40: When life gives you functions make functional programs!

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.