Skip to content

Instantly share code, notes, and snippets.

@nauzilus
Last active December 25, 2023 10:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nauzilus/d2e3862162c756ed06b4 to your computer and use it in GitHub Desktop.
Save nauzilus/d2e3862162c756ed06b4 to your computer and use it in GitHub Desktop.
(Highland) stream vs array transformation
(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