Gleb Bahmutov PRO
JavaScript ninja, image processing expert, software quality fanatic
Dr. Gleb Bahmutov PhD
Kensho Boston / NYC
source: Improved Minesweeper
Changing how JS developers program will have a huge impact on the software quality
in the world
Kensho - instant financial analytics
Kensho - instant financial analytics
Harvard Sq, Cambridge MA
1 WTC New York
number of engineers
lifetime of the company
2 major parts
Everyone has their own path to JS
! junior vs senior
In theory
anyone
functional reactive programmer
In practice
anyone
functional reactive programmer
...
...
...
...
...
...
...
Problem: given an array of numbers, multiply each number by a constant and print the result.
var numbers = [3, 1, 7];
var constant = 2;
// 6 2 14
var numbers = [3, 1, 7];
var constant = 2;
var k = 0;
for(k = 0; k < numbers.length; k += 1) {
console.log(numbers[k] * constant);
}
// 6 2 14
Start splitting code into procedures
var numbers = [3, 1, 7];
var constant = 2;
function mul(a, b) {
return a * b;
}
function processNumber(n, constant) {
console.log(mul(n, constant));
}
for(k = 0; k < numbers.length; k += 1) {
processNumber(numbers[k], constant);
}
// 6 2 14
Boring code does not surprise you
// for loops WILL surprise you
for(k = 0; k < numbers.length; k += 1) {
processNumber(numbers[k], constant);
}
Boring code does not surprise you
// using "this" WILL surprise you
numbers.forEach(function process(n) {
return this.mul(n, 2);
});
Boring code does not surprise you
// using non-local variables WILL surprise you
constant = 2;
...
numbers.forEach(function process(n) {
return mul(n, constant);
});
function mul(a, b) {
return a * b;
}
function print(n) {
console.log(n);
}
var mulBy2 = mul.bind(null, 2);
var mul2by3 = mul.bind(null, 2, 3);
var print = console.log.bind(console, 'RESULT');
function mul(a, b) {
return a * b;
}
print(mulBy2(10));
// RESULT 20
print(mul2by3(), mul2by3());
// RESULT 6 6
var numbers = [3, 1, 7];
var constant = 2;
function processNumber(n, constant) { ... }
// use built-in Array.prototype.forEach
numbers.forEach(function (n) {
processNumber(n, constant);
});
// 6 2 14
no more for-loop !
Object-Oriented EcmaScript5
Object-oriented + functional
var numbers = [3, 1, 7];
var constant = 2;
function mul(a, b) {
return a * b;
}
function print(n) {
console.log(n);
}
function mulBy = mul.bind(null, constant);
numbers.map(mulBy).forEach(print);
// 6 2 14
// OO (built-in)
Array.prototype.map(cb)
// functional (user space)
Library.map(array, cb)
// plus
Library.chunk(array, cb);
Library.drop(array, cb);
...
var _ = require('lodash');
var byConstant = _.partial(mul, constant);
_(numbers)
.map(byConstant)
.forEach(print);
// 6 2 14
Work with arrays
_({ John: 3, Jim: 10 })
.map(byConstant)
.forEach(print);
// 6 20
Treat objects same as collections
var mulBy = ...
// OO (built-in)
Array.prototype.map(mulBy)
// lodash / underscore
_.map(array, mulBy)
// Ramda
R.map(mulBy, array)
var R = require('ramda');
var byConstant = R.partial(R.mul, constant);
var mapByConstant = R.partial(R.map, byConstant);
var printEach = R.forEach(print);
var algorithm = R.compose(printEach, mapByConstant);
algorithm(numbers);
// 6 2 14
1. compose algorithm
2. apply to data
3. profit!!!
// R.mul = function (a, b) { ... }
var byConstant = R.partial(R.mul, constant);
// same as
var byConstant = R.mul(constant);
Partial application => curry
var R = require('ramda');
var mapByConstant = R.map(R.mul(constant));
var algorithm = R.compose(
R.forEach(print),
mapByConstant
);
algorithm(numbers);
// 6 2 14
No custom code necessary
"Smart" objects
Pass data around
"Dumb" objects
"Smart" pipeline
Pass code (functions) around
new NumberMultiplier()
.add(3, 1, 7)
.multiply(2)
.print();
_([3, 1, 7])
.map(_.partial(mul, 2))
.forEach(print);
Object-oriented VS functional
It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures."
- Alan J. Perlis
new NumberMultiplier()
.add(3, 1, 7)
.multiply(2)
.print();
_([3, 1, 7])
.map(_.partial(mul, 2))
.forEach(print);
Mutable data
var numbers = [...];
numbers.forEach(processNumber);
// is the list "numbers" unchanged?
function processNumber(x, k, array) {
array[1] = 10000;
print(x);
}
Mutable data makes code hard to understand and modify
Immutable data structures
'use strict';
var immutable = require('seamless-immutable');
var byConstant = _.partial(mul, constant);
immutable(numbers)
.map(byConstant)
.forEach(function (x, k, array) {
array[1] = 10000; // throws an Error
print(x);
});
Efficient immutable JS libraries: seamless-immutable, mori
Tell return values Bye-bye
// imperative code
var sum = add(2, 3);
// can use sum right away!
var multiplied = [1, 2, 3].map(mul);
// can use multiplied array right away!
var _ = require('lodash');
var byConstant = _.partial(mul, constant);
_(numbers)
.map(byConstant)
.forEach(print);
// we have not returned any values!
Tell middle functions Bye-bye
var byConstant = _.partial(mul, constant);
_(numbers)
.map(function (x) {
return byConstant(x);
})
.forEach(function (x) {
print(x);
});
_(numbers)
.map(byConstant)
.forEach(print);
// no middle functions
Tell deep equality checks Bye-bye
var result = process(numbers);
if (deepEqual(result, numbers)) {
...
}
var result = process(immutableNumbers);
if (result === immutableNumbers) {
...
}
// Does not work
numbers.map(function (x, done) {
mul(x, function cb(result) {
done(result);
})
}).forEach(function (value) {
print(value, function cb() {
done():
})
});
numbers.map(asyncFn)
// async step
.forEach(asyncFn);
var Q = require('q');
Q(...)
.then(function onSuccess(result) { ... },
function onFailure(error) { ... })
.then(...);
A single async action with a single result or error
var Q = require('q');
function foo() {
var defer = Q.defer();
...
defer.resolve(returnValue);
...
return defer.promise;
}
foo()
.then(bar)
.then(baz);
// functions foo, bar, baz return a promise
Each step in the promise chain can be asynchronous and return a promise!
var sleepSecond = _.partial(Q.delay, 1000);
var pauseMulAndPrint = function (n) {
return function () {
return sleepSecond()
.then(_.partial(byConstant, n))
.then(print);
};
};
numbers.map(pauseMulAndPrint)
.reduce(Q.when, Q())
.done();
// ... 6 ... 2 ... 14
Promises handle single event very well
Extra complexity trying to handle sequence of events
var sleepSecond = _.partial(Q.delay, 1000);
var pauseMulAndPrint = function (n) {
return function () {
return sleepSecond()
.then(_.partial(byConstant, n))
.then(print);
};
};
numbers.map(pauseMulAndPrint)
.reduce(Q.when, Q())
.done();
// ... 6 ... 2 ... 14
Promises handle single event very well
Extra complexity trying to handle sequence of events
var sleepSecond = _.partial(Q.delay, 1000);
var pauseMulAndPrint = function (n) {
return function () {
return sleepSecond()
.then(_.partial(byConstant, n))
.then(print);
};
};
numbers.map(pauseMulAndPrint)
// returned list is list of promises!
.reduce(Q.when, Q())
.done();
// ... 6 ... 2 ... 14
Constructing a promise for each number!
var sleepSecond = _.partial(Q.delay, 1000);
var pauseMulAndPrint = function (n) {
return function () {
return sleepSecond()
.then(_.partial(byConstantAsync, n))
.then(print);
};
};
Widely used in browser code
$(el).on('click', action)
window.onmessage = action
socket.on('message', action)
We can extend the same approach to any event generator (like number sequence)
// action
var events = require('events');
var numberEmitter = new events.EventEmitter();
numberEmitter.on('number',
_.compose(print, byConstant)
);
Single code pipeline processes all numbers
// async event source
lazy(numbers)
.async(1000)
.each(
_.bind(numberEmitter.emit, numberEmitter, 'number')
);
// prints 6, 2 14 with 1 second intervals
Process number with 1 second pauses
function source(generate) {
var eventEmitter = new events.EventEmitter();
generate(
_.bind(eventEmitter.emit, eventEmitter, 'step')
);
return {
on: function (cb) {
eventEmitter.on('step', cb);
}
};
}
Decouple emit / on code
var generator = lazy(list).async(1000);
source(_.bind(generator.each, generator))
.on(_.compose(print, byConstant));
// prints 6, 2 14 with 1 second intervals
var generator = lazy(list).async(1000);
var generatorFn = _.bind(generator.each, generator);
var actionFn = _.compose(print, byConstant);
source(generatorFn)
.on(actionFn);
Just the user code
Are we overcomplicating things?
// sync event generation
// sync action
action(eventGenerator);
// any event generation
// async action
source(eventGenerator)
.on(action);
source(eventGenerator)
.on(action);
[3, 1, 7]
.map(mulBy2)
.forEach(print);
source of events (numbers)
.map(an async callback)
.buffer(n, async callback)
.forEach(another async callback);
Chained emitters
var stepEmitter = {
map: function (cb) {
var emitter = new events.EventEmitter();
return _.extend(emitter, stepEmitter);
},
forEach: function (cb) {
var emitter = new events.EventEmitter();
return _.extend(emitter, stepEmitter);
}
};
Chained emitters
var stepEmitter = {
map: function (cb) {
var emitter = new events.EventEmitter();
this.on('step', function (value) {
var mappedValue = cb(value);
emitter.emit('step', mappedValue);
});
return _.extend(emitter, stepEmitter);
},
forEach: function (cb) {
var emitter = new events.EventEmitter();
this.on('step', function (value) {
cb(value);
emitter.emit('step', value);
});
return _.extend(emitter, stepEmitter);
}
};
Chained emitters
var stepEmitter = {
map: function (cb) {
var emitter = new events.EventEmitter();
this.on('step', function (value) {
var mappedValue = cb(value);
emitter.emit('step', mappedValue);
});
return _.extend(emitter, stepEmitter);
},
forEach: function (cb) {
var emitter = new events.EventEmitter();
this.on('step', function (value) {
cb(value);
emitter.emit('step', value);
});
return _.extend(emitter, stepEmitter);
}
};
source of events
.map(syncFn)
// next step async
.forEach(syncFn);
// next step async
Chained emitters
Chained emitters + promises
var Q = require('q');
var stepEmitter = {
map: function (cb) {
var emitter = new events.EventEmitter();
this.on('step', function (value) {
Q.when(cb(value)).then(function (mappedValue) {
emitter.emit('step', mappedValue);
});
});
return _.extend(emitter, stepEmitter);
},
forEach: function (cb) {
var emitter = new events.EventEmitter();
this.on('step', function (value) {
Q.when(cb(value)).then(function () {
emitter.emit('step', value);
});
});
return _.extend(emitter, stepEmitter);
}
};
source of events (numbers)
.map(asyncFn)
// next step async
.forEach(asyncFn);
// next step async
Chained emitters + promises
var stepEmitter = {
// same map and forEach methods as above
// returns a new step emitter that accumulates N items
// emits the entire array with N items as single argument
buffer: function (n) {
var received = [];
var emitter = new events.EventEmitter();
this.on('step', function (value) {
received.push(value);
if (received.length === n) {
emitter.emit('step', received);
received = [];
}
});
return _.extend(emitter, stepEmitter);
}
};
source(numbers)
.map(byConstant)
.buffer(3)
.forEach(print);
// sleeps 3 seconds then prints [6, 2, 14]
source(numbers)
.map(byConstant) // data transform
.buffer(3) // sequence control
.forEach(print); // data transform
We implemented 2 types
of operations
on a sequence of events
.map, .filter
// vs
.buffer
Use a reactive FP library
var Rx = require('rx');
var timeEvents = Rx.Observable
.interval(1000)
.timeInterval(); // stream 1
var numberEvents = Rx.Observable
.fromArray(numbers); // stream 2
Rx.Observable.zip(timeEvents, numberEvents,
function pickValue(t, n) { return n; })
.map(byConstant)
.subscribe(print); // stream 3
// prints 6 2 14 with 1 second intervals
User click events
var Rx = require('rx');
var clickEvents = Rx.Observable
.fromEvent(document.querySelector('#btn'), 'click')
var numberEvents = Rx.Observable
.fromArray(numbers);
Rx.Observable.zip(clickEvents, numberEvents,
function pickValue(whatever, n) {
return n;
})
.map(byConstant)
.subscribe(print);
// prints a number on each button click
function searchWikipedia (term) {
return $.ajax({
url: 'http://en.wikipedia.org/w/api.php',
dataType: 'jsonp',
data: {
action: 'opensearch',
format: 'json',
search: term
}
}).promise();
}
var keyups = Rx.Observable.fromEvent($('#input'), 'keyup')
.map(function (e) { return e.target.value; })
.filter(function (text) { return text.length > 2; });
keyups.throttle(500)
.distinctUntilChanged()
.flatMapLatest(searchWikipedia)
.subscribe(function (data) {
// display results
});
Autocomplete
object-oriented ** 2 /10
functional ********* 9 /10
immutable data ** 2 /10
lazy evaluation * 1 /10
promises ********* 9 /10
event emitters ******* 7 /10
reactive streams ***** 5 /10
All examples shown use today's ES5 standard
Dr. Gleb Bahmutov PhD
By Gleb Bahmutov
How do we convince / train developers used to imperative programming style to switch to functional or even reactive programming? I show the same example implemented in multiple styles to highlight the advantages one would get by going from imperative to object-oriented, functional, lazy evaluation, promise-based, event emitters, and finally reactive approach. The short examples are in JavaScript - the most widely used functional language today.
JavaScript ninja, image processing expert, software quality fanatic