Top Banner
Functional influences Bran van der Meer, 18-08-2015
32

Functional Influences

Feb 13, 2017

Download

Technology

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: Functional Influences

FunctionalinfluencesBran van der Meer, 18-08-2015

Page 2: Functional Influences

Agenda1. Abstraction

2. Currying3. Composition

Page 3: Functional Influences

Abstraction1

Page 4: Functional Influences

Pure function• No other dependencies than it’s

signature• Referentially Transparent• Stateless• Separated from it’s environment• Memoizable (cachable results)• More testable, portable and

parallelizable

Page 5: Functional Influences

Pure functionvar sum = function(left, right) { return left + right;};

sum(1, 2); //=> 3sum(1, 2); //=> 3sum(10, 2); //=> 12

Page 6: Functional Influences

Impure functionvar sumish = function(left, right) { return (left + right) * Math.random();};

sumish(1, 2); //=> ?sumish(3, 5); //=> ?

Page 7: Functional Influences

Impure functionvar Counter = function() { this._c = 0;};Counter.prototype.increment = function() { return ++this._c;};

var c = new Counter;c.increment(); //=> 1c.increment(); //=> 2

Page 8: Functional Influences

Impure functionfunction daysThisMonth() { var date = new Date(); var y = date.getFullYear(); var m = date.getMonth(); var start = new Date(y, m, 1); var end = new Date(y, m + 1, 1); return (end - start) / (1000 * 60 * 60 * 24);}

Page 9: Functional Influences

Pure functionfunction daysInMonth(y, m) { var start = new Date(y, m, 1); var end = new Date(y, m + 1, 1); return (end - start) / (1000 * 60 * 60 * 24);}

Page 10: Functional Influences

Higher-order function• A function that takes another function

as an argument• A function that returns a function• Famous examples: map, filter, reduce

Page 11: Functional Influences

Higher-order functionvar map = function(fn, arr) { var newArr = []; for (var i = 0; i < arr.length; i++) { newArr.push(fn(arr[i])); } return newArr;}

Page 12: Functional Influences

Higher-order functionvar makeAdd = function(left) { return function(right) { return left + right; };};

var add5 = makeAdd(5);add5(10); //=> 15

Page 13: Functional Influences

Currying2

Page 14: Functional Influences

Currying• Postponing giving arguments to a

function• A curried function can take less

arguments than it needs, it’ll wait for the others

• A curried function keeps returning a function when called, until it has all arguments

• Partial application is calling a function with less arguments than it needs

• In the extreme: giving one argument at a time

Page 15: Functional Influences

Manual curryingvar sum = function(left) { return function(right) { return left + right; };};

var sum5 = sum(5);//=> function(right) { return 5 + right }

sum5(7); //=> 12

Page 16: Functional Influences

One argument at a timevar sum = function(left, right) { return left + right;};

sum(5, 8); //=> 13

sum = curry(sum);

var sum5 = sum(5); //=> function() {...}sum5(7); //=> 12

Page 17: Functional Influences

One argument at a timesum = curry(sum);

sum(1, 2, 3) //=> 6

sum(1)(2)(3) //=> 6

Page 18: Functional Influences

Partial applicationsum = curry(sum);

sum(1, 2, 3) //=> 6sum(1)(2)(3) //=> 6sum(1)(2, 3) //=> 6sum(1, 2)(3) //=> 6sum(1, 2, 3) //=> 6

Page 19: Functional Influences

Useful examplevar getProp = function(prop, obj) { return obj[prop];};getProp = curry(getProp);

var people = [ {name: ...}, ... ];

var names = map(getProp('name'), people);

Page 20: Functional Influences

Underscore & lodashvar getProp = _.curry(function(obj, prop) { return obj[prop];});

var names = map(getProp('name'), people); // ?!

Page 21: Functional Influences

A functional libraryvar split = curry(function(pattern, str) { return str.split(pattern);});var filter = curry(function(fn, arr) { return arr.filter(fn);});var test = curry(function(re, str) { return re.test(str);});

Page 22: Functional Influences

Truly functional: Ramda

Page 23: Functional Influences

Another examplevar words = function(str) { return split(' ', str);};

var words = split(' ');

Page 24: Functional Influences

Yet another examplevar filterQs = function(xs) { return filter(function(x) { return test(/q/i, x); }, xs);};

var filterQs = filter(test(/q/i));

Page 25: Functional Influences

Composition3

Page 26: Functional Influences

Composition• Chaining functions to create a new

function• Passing return values from one to the

other• Composition enables pipelining of data

Page 27: Functional Influences

Naive implementationvar compose = function(f, g) { return function(x) { return f(g(x)); };};

Page 28: Functional Influences

Examplevar shout = function(x) { return exclaim(toUpper(x));};

var shout = compose(exclaim, toUpper);

shout('send in the clowns');//=> "SEND IN THE CLOWNS!"

Page 29: Functional Influences

Not referencing data argumentsmap(function(num) { return Math.sqrt(num);}, numbers);

map(Math.sqrt, numbers); // No arguments

Page 30: Functional Influences

Pointfreevar invert = function(x) { return -1 * x; };var negate = compose(invert, Math.abs);var negateAll = map(negate);

negateAll([-1, -2, 0, 2, -3, 4]);//=> [-1, -2, -0, -2, -3, -4]

Page 31: Functional Influences

Another examplevar cars = [ {price: '48000'}, ...];

var averagePrice = compose( average, map(parseInt), map(getProp('price')));

averagePrice(cars); //=> 42000

Page 32: Functional Influences

More? Training!4