Skip to content

Instantly share code, notes, and snippets.

@kunzimariano
Created November 6, 2015 20:02
Show Gist options
  • Save kunzimariano/13c88ed9ed1e7de65373 to your computer and use it in GitHub Desktop.
Save kunzimariano/13c88ed9ed1e7de65373 to your computer and use it in GitHub Desktop.
Functional generators
'use strict';
let R = require('ramda');
let map = R.curry(function*(fun, list) {
for (let i of list) {
console.log('map');
yield fun(i);
}
});
let find = R.curry(function*(predicate, list) {
for (let i of list) {
console.log('find');
if (predicate(i)) {
yield i;
return;
}
}
});
let take = R.curry(function*(n, list) {
let count = 0;
for (let i of list) {
console.log('take');
if (count < n) {
count++;
yield i;
} else {
return;
}
}
});
let nth = R.curry(function*(nth, list) {
console.log('nth');
yield list[nth];
return;
});
let mapTakeNth = R.pipe(
map(x => x + 1),
take(3),
nth(1)
//find(x => x === 4)
);
let a = [1, 2, 3, 4, 5, 6];
for (let i of mapTakeNth(a)) {
console.log(i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment