Run the following code against [Ramda][ramda].
// Simple predicate to test whether a number is even
var even = function(n) {return (n % 2) === 0;};
// Generates an infinite list of Fibonacci numbers
var fibsgen = generator(
(function() { | |
var split = {}, intervalData, | |
timeComponent = { | |
seconds: function (split) {return split.seconds * 6;}, | |
minutes: function (split) {return (split.minutes + (split.seconds / 60)) * 6;}, | |
hours: function (split) {return (split.hours % 12 * 30 ) + split.minutes / 2;} | |
}; | |
var SVG = (function() { | |
var SVG = function(config) { |
var app = (function() { | |
var app = (function () { | |
var cache = {}; | |
var getTopic = function (topic) { | |
return cache[topic] || (cache[topic] = []); | |
}; | |
var emitData = function(data, callback) { | |
callback.call(null, data); |
(function(global) { | |
// TODO: Make a tree structure from type objects' optional `parentKey` properties. Do a depth-first search of | |
// this tree instead of the simple linear search of `Object.keys`. | |
var types = {}; | |
global.Functor = function(config) { | |
types[config.key] = config; | |
}; | |
global.fmap = curry(function(f, obj) { |
var fibonacci = (function() { | |
var fib = function(curr, next, n) { | |
return (n === 0) ? curr : fib(next, curr + next, n - 1); | |
}; | |
return function(n) { | |
return fib(0, 1, n); | |
} | |
}()); |
inspired to understand this from:
compare with https://github.com/Gozala/js-tail-call/blob/master/core.js for speed (after refactoring to work like his does)
const R = require('ramda'); | |
const mapIndexed = R.addIndex(R.map); | |
// transpose :: [[a]] -> [[a]] -- (row, col) becomes (col, row) | |
const transpose = function(matrix) { | |
return mapIndexed(function (_, col) { | |
return R.reject(R.isNil, R.map(R.prop(col), matrix)); | |
}, R.head(matrix)); | |
}; |
This came out of the recent JS Promises debate. @ForbesLindesay was confused by liftA2, so I thought I’d try to explain it.
Let me explain liftA2. I’ll tweak @pufuwozu’s examples so that they return values.
// Promises (called when both succeed)
liftA2(readFIle('hello.txt'), readFile('world.txt'), function(hello, world) {
return hello + ' ' + world;
});
// Optional values (only inserted into database when both exist)
liftA2(optionalUsername, optionalPassword, function(username, password) {
var transform = function(n) {return 3 * n + 2;} | |
var log = function(n, idx, arr) { | |
return 'n: ' + n + ', idx: ' + idx + ', arr: ' + arr; | |
} | |
console.log('----------------------------------------'); | |
var Fk = require('fkit'); | |
console.log('fkit'); | |
console.log(Fk.compose(Fk.map(log), Fk.map(transform))([0, 1, 2]).join('\n')); |
var transform = function(n) {return 3 * n + 2;}; | |
var log = function(n, idx, obj) { | |
return 'n: ' + n + ', idx: ' + idx + ', obj: ' + obj; | |
}; | |
var Lazy = require('lazy.js'); | |
console.log("lazy.js") | |
console.log(Lazy.range(3).map(transform).map(log).toArray().join('\n')); | |
console.log("----------------------------------------"); |