Top Banner
The Shape of Functional Programming Saturday, October 5, 13
77

The Shape of Functional Programming

Apr 13, 2017

Download

Technology

Mike Fogus
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The Shape of Functional Programming

The Shapeof

FunctionalProgramming

Saturday, October 5, 13

Page 2: The Shape of Functional Programming

@fogushttp://www.fogus.me

http://github.com/documentcloud/underscore-contrib

Saturday, October 5, 13

Page 3: The Shape of Functional Programming

@fogus

“FunctionalJavaScript”Lemonad

codd.js

Saturday, October 5, 13

Page 4: The Shape of Functional Programming

Aprogram

written in anobject-oriented

style

Saturday, October 5, 13

Page 5: The Shape of Functional Programming

Functional programming

for the MEOW!

Saturday, October 5, 13

Page 6: The Shape of Functional Programming

Saturday, October 5, 13

Page 7: The Shape of Functional Programming

Pattern 1 - Map(ish)var array = [0,1,2,3,4,5];var transformed = []; for (var i = 0; i < array.length; i++) { transformed.push(array[i] + 100);} transformed;//=> [100, 101, 102, 103, 104, 105]

Saturday, October 5, 13

Page 8: The Shape of Functional Programming

Pattern 1 - Map(ish)

// Underscore _.map(array, function(n) { return n + 100;});  // Lemonad L.map(L.add(100), array);

Saturday, October 5, 13

Page 9: The Shape of Functional Programming

Pattern 2 - Filter(ish)var array = [0,1,2,3,4,5];var keepers = []; for (var i = 0; i < array.length; i++) { if (array[i] % 2 === 0) { keepers.push(array[i]); }} keepers;//=> [101, 102, 103, 104, 105]

Saturday, October 5, 13

Page 10: The Shape of Functional Programming

Pattern 2 - Filter(ish)// Underscore _.filter(array, function(n) { if (n % 2 === 0) { return true; } else { return false; }}); // Lemonad L.filter(L.isEven, array);

Saturday, October 5, 13

Page 11: The Shape of Functional Programming

Pattern 3 - Reduce(ish)

var array = [0,1,2,3,4,5];var sum = 0; for (var i = 0; i < array.length; i++) { sum += array[i];} sum;//=> 15

Saturday, October 5, 13

Page 12: The Shape of Functional Programming

Pattern 3 - Reduce(ish)

// Underscore _.reduce(array, function(acc, n) { return acc + n;}, 0);  // Lemonad L.reduce(L.uncurry(L.add), 0, array);

Saturday, October 5, 13

Page 13: The Shape of Functional Programming

APL

life←{↑1 ⍵∨.∧3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵}

0 1 0 0 1 0 0 1 0

0 1 0

0 1 0

0 1 0

Saturday, October 5, 13

Page 14: The Shape of Functional Programming

Fun.js at a glance

• Simple data

• Many small functions

• Larger abstractions built via composition

• OO to supplement (no dogma)

Saturday, October 5, 13

Page 15: The Shape of Functional Programming

Simple data

Saturday, October 5, 13

Page 16: The Shape of Functional Programming

Saturday, October 5, 13

Page 17: The Shape of Functional Programming

#

Saturday, October 5, 13

Page 18: The Shape of Functional Programming

42

Saturday, October 5, 13

Page 19: The Shape of Functional Programming

?

Saturday, October 5, 13

Page 20: The Shape of Functional Programming

Saturday, October 5, 13

Page 21: The Shape of Functional Programming

[ ]Saturday, October 5, 13

Page 22: The Shape of Functional Programming

#

Saturday, October 5, 13

Page 23: The Shape of Functional Programming

#

[1, 2, 3, 4, 5]

Saturday, October 5, 13

Page 24: The Shape of Functional Programming

Saturday, October 5, 13

Page 25: The Shape of Functional Programming

{ }Saturday, October 5, 13

Page 26: The Shape of Functional Programming

#“

Saturday, October 5, 13

Page 27: The Shape of Functional Programming

#“

{x: 0, y: 1}

Saturday, October 5, 13

Page 28: The Shape of Functional Programming

{x: 0, y: 1}

#

Saturday, October 5, 13

Page 29: The Shape of Functional Programming

Saturday, October 5, 13

Page 30: The Shape of Functional Programming

*Saturday, October 5, 13

Page 31: The Shape of Functional Programming

Small functions

Saturday, October 5, 13

Page 32: The Shape of Functional Programming

Saturday, October 5, 13

Page 33: The Shape of Functional Programming

λSaturday, October 5, 13

Page 34: The Shape of Functional Programming

λSaturday, October 5, 13

Page 35: The Shape of Functional Programming

λSaturday, October 5, 13

Page 36: The Shape of Functional Programming

Disclaimer

Saturday, October 5, 13

Page 37: The Shape of Functional Programming

#

Procedure

Saturday, October 5, 13

Page 38: The Shape of Functional Programming

#

Procedure

Saturday, October 5, 13

Page 39: The Shape of Functional Programming

function logNth(array, index) { console.log(array[index]);}

logNth([‘a’, ‘b’, ‘c’], 1);// (console) b

#

Saturday, October 5, 13

Page 40: The Shape of Functional Programming

Purity

Saturday, October 5, 13

Page 41: The Shape of Functional Programming

Ostrich Purity

Saturday, October 5, 13

Page 42: The Shape of Functional Programming

Ostrich Purity

Saturday, October 5, 13

Page 43: The Shape of Functional Programming

#

Function

Kinda importantSaturday, October 5, 13

Page 44: The Shape of Functional Programming

function nth(array, index) { return array[index];}

nth([‘a’, ‘b’, ‘c’], 1);//=> ‘b’

#

Saturday, October 5, 13

Page 45: The Shape of Functional Programming

Saturday, October 5, 13

Page 46: The Shape of Functional Programming

Saturday, October 5, 13

Page 47: The Shape of Functional Programming

Saturday, October 5, 13

Page 48: The Shape of Functional Programming

id name age

555-55-5555 Moe 45

777-77-7777 Curly 47

0

1

Saturday, October 5, 13

Page 49: The Shape of Functional Programming

var table = [

{id: ‘555-55-5555’, name: ‘Moe’, age: 45},

{id: ‘777-77-7777’, name: ‘Curly’, age: 47}

];

Saturday, October 5, 13

Page 50: The Shape of Functional Programming

id name age

555-55-5555 Moe 45

777-77-7777 Curly 47

0

1

Saturday, October 5, 13

Page 51: The Shape of Functional Programming

#“

Saturday, October 5, 13

Page 52: The Shape of Functional Programming

function cell(table, col, row){ return table[row][col];}

cell(table, ‘name’, 0);//=> ‘Moe’

cell(table, ‘age’, 0);//=> 45

#“

Saturday, October 5, 13

Page 53: The Shape of Functional Programming

Larger abstractions built via composition

Saturday, October 5, 13

Page 54: The Shape of Functional Programming

LClojure, Haskell, ML, Joy, _

Saturday, October 5, 13

Page 55: The Shape of Functional Programming

Currying, partial application, fixation, oh

my!

Saturday, October 5, 13

Page 56: The Shape of Functional Programming

#“

Saturday, October 5, 13

Page 57: The Shape of Functional Programming

#“

L.fix

Saturday, October 5, 13

Page 58: The Shape of Functional Programming

#

L.fix(cell, L._, ‘name’, L._);

‘name’

Saturday, October 5, 13

Page 59: The Shape of Functional Programming

#

L.fix(cell, L._, ‘name’, L._);

var getName = L.fix(cell, L._, ‘name’, L._);

getName(table, 0);//=> ‘Moe’

getName(table, 1);//=> ‘Curly’

‘name’

Saturday, October 5, 13

Page 60: The Shape of Functional Programming

#“

L.rot

Saturday, October 5, 13

Page 61: The Shape of Functional Programming

#

L.rot(cell);

Saturday, October 5, 13

Page 62: The Shape of Functional Programming

#“

L.partial

Saturday, October 5, 13

Page 63: The Shape of Functional Programming

L.partial(L.rot(cell), ‘name’);

#‘name’

Saturday, October 5, 13

Page 64: The Shape of Functional Programming

L.partial(L.rot(cell), ‘name’);

var getName = L.partial(L.rot(cell), ‘name’);

getName(0, table);//=> ‘Moe’

getName(1, table);//=> ‘Curly’

#‘name’

Saturday, October 5, 13

Page 65: The Shape of Functional Programming

L.rcurry

Codd.col(table, ‘name’);//=> [‘Moe’, ‘Curly’]

Saturday, October 5, 13

Page 66: The Shape of Functional Programming

L.rcurry2(Codd.col);

Saturday, October 5, 13

Page 67: The Shape of Functional Programming

L.rcurry2(Codd.col);

var valuesFor = L.rcurry2(Codd.col);var allNames = valuesFor(‘name’);

allNames(table);//=> [‘Moe’, ‘Curly’]

Saturday, October 5, 13

Page 68: The Shape of Functional Programming

Compositions and Pipelines

Saturday, October 5, 13

Page 69: The Shape of Functional Programming

How to countall of the values

in a column?

Saturday, October 5, 13

Page 70: The Shape of Functional Programming

“function countValues(table, tag) { return L.len(Codd.col(table, tag));}

countValues(table, ‘name’);//=> 2

#

Saturday, October 5, 13

Page 71: The Shape of Functional Programming

L.comp

return L.len(Codd.col(table, tag));

First thisThen thisSaturday, October 5, 13

Page 72: The Shape of Functional Programming

“Codd.col

#L.len

Saturday, October 5, 13

Page 73: The Shape of Functional Programming

“Codd.col

#L.len

Saturday, October 5, 13

Page 74: The Shape of Functional Programming

var countValues = L.comp(L.len, Codd.col);

countValues(table, ‘name’);//=> 2

#

Saturday, October 5, 13

Page 75: The Shape of Functional Programming

L.pipeline

L.pipeline(table, Codd.col(‘name’), L.len);//=> 2

First this Then thisSaturday, October 5, 13

Page 76: The Shape of Functional Programming

codd.js (demo)Relational Algebra

Saturday, October 5, 13

Page 77: The Shape of Functional Programming

Thanks

• The NationJS team

• O’Reilly

• Alan Dipert

• Paul Khuong

• The Underscore community

• The Clojure community

Saturday, October 5, 13