I've found some more cases personally that have actually helped and are much smaller examples. My 3 favorites are:
for loops which need to be paused and resumed at a later date
infinitely looping over an array and having it reset to the beginning once it's done
creating iterables to use in for of loops from non-iterable objects using [Symbol.Iterator]
Generators also help to loop through structures where you'd normally need to keep track of multiple variables at a time. Instead of having multiple pieces of global state, you just need to call .next() on the generator.
I'd like to hear more about generators being a mistake though. I haven't yet heard that and I would like to know the reasoning behind it.
As to Generators being a mistake, I heard that in a talk a while back and vaguely remember agreeing with the arguments at the time. Since I can't remember the talk or the arguments, I'll change the language here.
I'm not going to bootstrap an entire project for an example, but I can give you a basic use case for them:
You'd want to play and pause loops if someone hit a pause button on an animation and the loop is a tween.
You'd want an infinitely looping array for things like carousels or lists of data which wrap around
You'd want to iterate over objects to check if any of its properties include a specific value. You get the array and then check with .includes()
You'd want a map if you have objects already and want to keep the object like structure, but also want to check if properties exist on an object that are falsy.
I hope you understand that I'm not going to code these entire use cases. It's be much too long for a comment and I'd sooner make my own post than a comment with the full code.
This is more than enough ... if you are OK with it, I'll do a follow-up post to this one with your examples included (attributed to you, of course) down the road.
Hey there! Just saw this now.
We can iterate through objects in many ways. The demonstration was showing that we can also iterate through objects in particular ways, as per our own use case.
We might want to create an iterator that only spits out object properties that are numbers or fall under some filter.
We could create an array of all properties and then filter, but this is creating 2 arrays whereas a generator allows us to create 0 arrays. We simply iterate over the returned iterable and deal with it as necessary
I've found some more cases personally that have actually helped and are much smaller examples. My 3 favorites are:
Generators also help to loop through structures where you'd normally need to keep track of multiple variables at a time. Instead of having multiple pieces of global state, you just need to call
.next()
on the generator.I'd like to hear more about generators being a mistake though. I haven't yet heard that and I would like to know the reasoning behind it.
Cheers!
Love the comments.
As to Generators being a mistake, I heard that in a talk a while back and vaguely remember agreeing with the arguments at the time. Since I can't remember the talk or the arguments, I'll change the language here.
Thanks again!
Bob
Can you provide "practical" code uses with your examples that I can include?
Here is the first example (looping arrays using generators to pause and resume):
Here is the infinitely looping array:
Here is how to make an iterable from an object:
and using a similar method, you can convert objects to Maps, which can become super useful in some instances:
OK, the code examples are good ... and I get where you are going with this.
By "practical," I am trying to find actual use-cases ... not just "Hello World" type of code.
I'm not going to bootstrap an entire project for an example, but I can give you a basic use case for them:
.includes()
I hope you understand that I'm not going to code these entire use cases. It's be much too long for a comment and I'd sooner make my own post than a comment with the full code.
This is more than enough ... if you are OK with it, I'll do a follow-up post to this one with your examples included (attributed to you, of course) down the road.
Sounds good.
If you want one or two more, Dr. Axel Rauschmayer provides some use cases with async code
Awesome ... thanks for the additional references!
@emnudge regarding your examples:
Could you explain / demonstrate 4. ?
I'm also not sure if I understand 3.
Why not use object.values().includes() ?
Hey there! Just saw this now.
We can iterate through objects in many ways. The demonstration was showing that we can also iterate through objects in particular ways, as per our own use case.
We might want to create an iterator that only spits out object properties that are numbers or fall under some filter.
We could create an array of all properties and then filter, but this is creating 2 arrays whereas a generator allows us to create 0 arrays. We simply iterate over the returned iterable and deal with it as necessary
I love the idea of using a generator for this. I might have to try an alternate!