Skip to content

Instantly share code, notes, and snippets.

@rehia
Created January 20, 2019 13:49
Show Gist options
  • Save rehia/e034c55a4a28f4093aa3490f3cf47d7b to your computer and use it in GitHub Desktop.
Save rehia/e034c55a4a28f4093aa3490f3cf47d7b to your computer and use it in GitHub Desktop.
linq.js
const O = (a) => {
let compose = function* () {
for (let v of a) {
yield v;
}
};
const filter = function(p) {
let rest = compose;
compose = function* () {
for (let v of rest()) {
if (p(v)) {
yield v;
}
}
}
return m;
}
const map = function(t) {
let rest = compose;
compose = function* () {
for (let v of rest()) {
yield t(v);
}
}
return m;
}
const run = function(r) {
for (let v of compose()) {
r(v);
}
}
const m = {
filter,
map,
run
}
return m;
}
O([1,2,3,4,5,6])
.filter(v => v % 2 === 0)
.map(v => v + 1)
.run(v => console.log(v));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment