Permalink
Browse files

[samples added 2 samples

  • Loading branch information...
1 parent 010efd4 commit ab62f9d53b278e191a01f5b29c34e8d89967d2ca @dscape committed Feb 18, 2012
Showing with 36 additions and 0 deletions.
  1. +4 −0 samples/declaring_in_a_library.js
  2. +25 −0 samples/hof.js
  3. +7 −0 samples/map_async.js
@@ -0,0 +1,4 @@
+var map = require('./hof').map;
+
+map(function duplicate(x) { return x*2; }, [1,2,3], [],
+ function (x) { console.log(x); });
View
@@ -0,0 +1,25 @@
+var map = require('../pattern')
+ , mapa = require('../pattern')
+ , _, f, ac
+ ;
+
+// high order functions
+map(f, [], ac, _,
+ function map_done(f, l, ac, cb) { return cb(ac); });
+map(f, _, ac, _,
+ function map_catch_all(f, l, ac, cb) {
+ ac.push(f(l.shift())); // head
+ map(f, l, ac, cb); // l is now tail
+ });
+
+mapa(f, [], ac, _,
+ function map_done(f, l, ac, cb) { return cb(ac); });
+mapa(f, _, ac, _,
+ 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
+ });
+ });
+
+module.exports = { map: map, map_async: mapa };
View
@@ -0,0 +1,7 @@
+var map_async = require('./hof').map_async;
+
+function duplicate(x, cb) {
+ setTimeout(function() { cb(x*2); }, Math.ceil(Math.random() * 100));
+}
+
+map_async(duplicate, [1,2,3], [], function(ac) { console.log(ac); });

0 comments on commit ab62f9d

Please sign in to comment.