Skip to content

Instantly share code, notes, and snippets.

@bwestergard
Last active December 4, 2016 21:39
Show Gist options
  • Save bwestergard/d8b24846816d7670af901dc1ba7094ec to your computer and use it in GitHub Desktop.
Save bwestergard/d8b24846816d7670af901dc1ba7094ec to your computer and use it in GitHub Desktop.
Lazy Streams in Node
"use strict";
// http://stackoverflow.com/questions/23260390/node-js-tail-call-optimization-possible-or-not
// Tested and working with node v7.2.0. Both of the following work:
// node --harmony_tailcalls tco.js
// node --harmony tco.js
const natsFrom = (n) => [n, () => natsFrom(n+1)]
const kth = ([hd, tl], k) => k === 0 ? hd : kth(tl(), k - 1)
console.log(
kth(natsFrom(1337), 8000000)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment