Skip to content

Instantly share code, notes, and snippets.

@liorean
Created November 18, 2017 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liorean/fa6b1f147df2dd3bd0c99e3254dd21f0 to your computer and use it in GitHub Desktop.
Save liorean/fa6b1f147df2dd3bd0c99e3254dd21f0 to your computer and use it in GitHub Desktop.
Improving on the bad map
// See https://gist.github.com/liorean/58667acd9b8b4554a9c7b4740065e93c
const map=(f,array)=>{
const iterator=array[Symbol.iterator]()
let {done,value}=iterator.next()
if(done){
if('return' in iterator)
iterator.return()
return []}
return [f(value),...map(f,iterator)]}
console.log(map(e=>2**e,[0,1,2,3,4,5,6,7,8,9]))
//> ch bettermap.es
// 1,2,4,8,16,32,64,128,256,512
//> js bettermap.es
// 1,2,4,8,16,32,64,128,256,512
//> node bettermap.es
// [ 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment