-
-
Save kunzimariano/13c88ed9ed1e7de65373 to your computer and use it in GitHub Desktop.
Functional generators
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
'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