Olli Mahlamäki / Reaktor
“In computer science, functional programming is a programming paradigm, a style of building the structure and elements of computer programs, that treats computation as the evaluation of mathematical functions and avoids state and mutable data.”http://en.wikipedia.org/wiki/Functional_programming
function sum(x, y) {
return x + y;
}
display(sum(4,8));
var n = 7;
function addN(x) {
return x + n;
}
display(addN(4));
var i=0;
function count() {
return ++i;
}
display(count());
display(count());
function fibonacci(i) {
if(i < 3) return 1;
return fibonacci(i-1) + fibonacci(i-2);
}
display(fibonacci(7));
function warn(message) {
window.alert("Warning: " + message);
}
warn("Audience participation needed");
The point in functional programming style is to separate them from rest of the code
var calculator = new Calculator();
calculator.addInput(5);
calculator.addInput(10);
calculator.sumInputs();
console.log(calculator.getValue());
console.log(Calculator.sum(5,10));
var center = new Point(15, 40)
var radius = 15
var circle = new Circle(center, radius)
circle.draw(canvas)
center.setX(30)
circle.draw(canvas)
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.prototype.moveBy = function(deltaX, deltaY) {
this.x += deltaX
this.y += deltaY
return this
}
function distanceUntilNegative(point) {
var d = 0;
while(point.x >= 0 && point.y >= 0) {
point.moveBy(-1, -1)
d++
}
return d;
}
var pos = new Point(3,5)
display(distanceUntilNegative(pos))
display(pos)
function point(x, y) {
return { x: x, y: y }
}
function moveBy(p, deltaX, deltaY) {
return point(p.x + deltaX, p.y + deltaY)
}
function distanceUntilNegative(point) {
var d = 0;
while(point.x >= 0 && point.y >= 0) {
point = moveBy(point, -1, -1)
d++
}
return d;
}
var pos = point(3,5)
display(distanceUntilNegative(pos))
display(pos)
“Functional reactive programming (FRP) is a programming paradigm for reactive programming using the building blocks of functional programming. FRP has been used for programming GUIs, robotics, and music, aiming to simplify these problems by explicitly modeling time.”
var mappedStream = stream.map(f)
var filteredStream = stream.filter(f)
Merges events from both streams a and b into the result stream.
Result stream ends when both streams have ended.
myStream.onValue(function(value) {
// Called every time myStream gets a value
$("#result").text(value)
})
Combines the latest values of property a and stream or property b using a given function. In this example, a self-explanatory plus function is used.
First output event is generated when both a and b have produced a value. The combined stream ends when both the inputs have ended or one has ended without having produced a value.
Creates a new stream for each value in the source stream, using the given function f. Merges the events from all created streams into the result stream.
Result stream ends when both the source stream and all the so-far created streams have ended.
Like flatMap, flatMapLatest creates new streams for each source event. Instead of merging all created streams, it "switches" (hence the former name "switch") between them so that when a new stream is created, the earlierly created stream is no longer listened to.
Result stream ends when source stream has ended and the latest created stream has ended.