Last active
December 25, 2023 10:52
-
-
Save nauzilus/d2e3862162c756ed06b4 to your computer and use it in GitHub Desktop.
(Highland) stream vs array transformation
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
(function() { | |
var highland = require("highland") | |
var array = [1,2,3], | |
spy = (message, fn) => { | |
return x => { | |
console.log(`${message} ${x}`) | |
return fn(x) | |
} | |
}, | |
increment = spy("incrementing", x => x + 1), | |
double = spy("doubling", x => x * 2), | |
evenOnly = spy("filter", x => x % 2 === 0), | |
output = spy("done", x => x) | |
highland(array) | |
.map(increment) | |
.filter(evenOnly) | |
.map(double) | |
.each(output) | |
console.log("---") | |
array | |
.map(increment) | |
.filter(evenOnly) | |
.map(double) | |
.forEach(output) | |
})() | |
/* | |
incrementing 1 | |
filter 2 | |
doubling 2 | |
done 4 | |
incrementing 2 | |
filter 3 | |
incrementing 3 | |
filter 4 | |
doubling 4 | |
done 8 | |
--- | |
incrementing 1 | |
incrementing 2 | |
incrementing 3 | |
filter 2 | |
filter 3 | |
filter 4 | |
doubling 2 | |
doubling 4 | |
done 4 | |
done 8 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment