Last active
December 18, 2015 10:38
-
-
Save dscape/5769488 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var five = require('johnny-five'), | |
foldl = require('./hof').foldl | |
var leds = | |
[ 2 | |
, 3 | |
, 4 | |
, 5 | |
, 6 | |
, 7 | |
, 8 | |
, 9 | |
] | |
board = new five.Board() | |
board.on('ready', function() { | |
console.log('(-)(+)') | |
function runInteraction (interaction) { | |
return function runInteractionInLED(z, led, next) { | |
process.stdout.write(interaction === 'fadeIn' ? '+' : '—') | |
console.log(led) | |
led = new five.Led(led) | |
led[interaction]() | |
setTimeout(function () { | |
next() | |
}, 300) | |
} | |
} | |
function runAllInteractions(z, interaction, next) { | |
var l = JSON.parse(JSON.stringify(leds)) //clone | |
, f = runInteraction(interaction) | |
foldl(f, null, l, next) | |
} | |
foldl(runAllInteractions, null, ['fadeIn', 'fadeOut'], function done() { | |
console.log('(+)(-)') | |
process.exit() | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var p = require('p'), | |
map = p(), | |
mapa = p(), | |
zip_with = p(), | |
filtera = p(), | |
maybe = p(), | |
foldl = p(), | |
_, f, ac, l, l1, l2, cb, errcb, z; | |
// error handling for conditional execution of functions | |
maybe(Error, _, errcb, cb, function(err, data, errcb, cb) { | |
errcb(err); | |
}); | |
maybe(_, _, errcb, cb, function(err, data, errcb, cb) { | |
cb(data); | |
}); | |
// map | |
// map _ [] = [] | |
// map f (x:xs) = f x : map f xs | |
map(_, [], ac, cb, function map_done(f, l, ac, cb) { | |
return cb(ac); | |
}); | |
map(f, l, ac, cb, function map_catch_all(f, l, ac, cb) { | |
ac.push(f(l.shift())); // head | |
map(f, l, ac, cb); // l is now tail | |
}); | |
// map with async f, no error possible | |
mapa(f, [], ac, cb, function map_done(f, l, ac, cb) { | |
return cb(ac); | |
}); | |
mapa(f, l, ac, cb, function map_catch_all(f, l, ac, cb) { | |
f(l.shift(), function(x) { | |
ac.push(x); | |
mapa(f, l, ac, cb); // l is now tail | |
}); | |
}); | |
// filter with async f | |
// filter _ [] = [] | |
// filter p (x:xs) | |
// | p x = x : filter p xs | |
// | otherwise = filter p xs | |
filtera(_, [], ac, cb, function filter_done(f, l, ac, cb) { | |
return cb(null, ac); | |
}); | |
filtera(f, l, ac, cb, function filter_catch_all(f, l, ac, cb) { | |
var head = l.shift(); | |
f(head, function(err, ok) { | |
maybe(err, ok, cb, function(ok) { | |
if (ok) ac.push(head); | |
filtera(f, l, ac, cb); // l is now tail | |
}); | |
}); | |
}); | |
// foldl with async f, no errors possible | |
// foldl f z [] = z | |
// foldl f z (x:xs) = foldl f (f z x) xs | |
foldl(f, z, [], cb, function foldl_done(f, z, l, cb) { | |
cb(z); | |
}); | |
foldl(f, z, l, cb, function foldl_all(f, z, l, cb) { | |
f(z, l.shift(), function foldl_f_async(new_z) { | |
foldl(f, new_z, l, cb); | |
}); | |
}); | |
// zipWith _ [] _ = [] | |
// zipWith _ _ [] = [] | |
// zipWith f (x:xs) (y:ys) = f x y : zipWith' f xs ys | |
zip_with(_, [], _, ac, cb, function emptyl1(f, l1, l2, ac, cb) { | |
cb(ac); | |
}); | |
zip_with(_, _, [], ac, cb, function emptyl2(f, l1, l2, ac, cb) { | |
cb(ac); | |
}); | |
zip_with(f, l1, l2, ac, cb, function all(f, l1, l2, ac, cb) { | |
ac.push(f(l1.shift(), l2.shift())); | |
zip_with(f, l1, l2, ac, cb); | |
}); | |
module.exports = { | |
map: map, | |
map_async: mapa, | |
zip_with: zip_with, | |
filter_async: filtera, | |
foldl: foldl | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment