Skip to content

Instantly share code, notes, and snippets.

@therealklanni
Last active January 27, 2020 17:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save therealklanni/bd5b1b479819fc247651 to your computer and use it in GitHub Desktop.
Save therealklanni/bd5b1b479819fc247651 to your computer and use it in GitHub Desktop.
Infinitely repeating arrays in JavaScript ... kind of

repeat(a) -> [a]

Inspired by Haskell, I wanted to see if I could replicate, using ES6 features, the repeat function:

repeat :: a -> [a]

So as you can see in repeat.js, I have done exactly that. However there are some caveats.

First, I still need to make the result behave like a normal array (with all of Array's prototype methods), but it's a good start anyway.

You can access any index in the array and get the repeated value. You can even loop over it as much as you want and keep taking the repeated value each time. Infinitely, as far as I know (of course I didn't test how far it would go, because that would literally take forever, but even a number more than 10 digits long still produced the repeated value and that was good enough for me).

var batman = repeat('nana')

// don't try this at home, kids!
for (i=0;;i++) {
  console.log(batman[i])
}

Another caveat is that it also does this for literally any value given to the accessor.

var repeatBeep = repeat('boop')

repeatBeep['foobar'] // => "boop"

Obviously I can fix this easily by adding a type check to the get method passed into Proxy, but I just wanted to get the very basic functionality working (because it's 5am and I've been up all night).

The final caveat of course being the fact that there's almost no support for Proxy at this point. Literally only FireFox and Edge at this time. So feel free to drop that repeat function in the FF console and play around with infinitely-repeating arrays. Keep in mind, again, that the Array methods are still missing from this implementation, so don't expect to be able to slice() that array or anything fancy right now.

Of course, this proof-of-concept could have other (more useful?) applications as well. Don't like having to call .next().value on generators? Would you rather treat it like an array? I think we can accomodate.

That's it for now, but I'll probably update this when I get around to making the proxy more Array-like.

var repeat = (a) => new Proxy([], {
get: () => a
})
@ericelliott
Copy link

Interesting challenge. I think I've given up trying to keep compatability with arrays in object-list. Just too limiting.

It makes me wonder if we could make object-list comply with the ES6 iterable contract though, so you could use for of loops on it... what do you think?

@therealklanni
Copy link
Author

Yeah, it would definitely be doable, and probably a good idea, too.

@therealklanni
Copy link
Author

Totally forgot that since this is ES6, I can use arrow functions 🤘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment