Created
January 20, 2019 13:49
-
-
Save rehia/e034c55a4a28f4093aa3490f3cf47d7b to your computer and use it in GitHub Desktop.
linq.js
This file contains hidden or 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
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