fiber iterators

About Monkey 2 Forums Monkey 2 Development fiber iterators

This topic contains 1 reply, has 2 voices, and was last updated by  Mark Sibly 3 years, 2 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #263

    dmaz
    Participant

    question about how fiber generators will work… the example on the front page used a while loop instead of eachin, is/will there be support for eachin with those?

    in c# I’ve done this in a vector class I use for grids.

    [code]    public IEnumerable<IntVector2> Neighbours {
    yield return this + left;
    yield return this + right;
    yield return this + up;
    yield return this + down;
    }[/code] Which allows me to do

    for(IntVector2 v2 in location.Neighbours()) {}

    in monkey2, will I need to extend something like Generator(as mentioned on the blog) or implement HasNext(), GetNext() etc…  ?   is there some way to hide some of  this?

    #277

    Mark Sibly
    Keymaster

    To do this right now, the obvious way would be to subclass something like the Generator<T> class with a NeighborGenerator<Vector2> class. That would work, but it’s not as elegant as an enumerator method.

    An interesting potential alternative would be to ‘push’ the neighbors to a generator (consumer?) eg:

    Then, a global function like ‘Generate’ could be used to do the generation (not sure if it’s this simple…!) eg:

    …which could be used by client code like this…

    This is actually kind of interesting – instead of the Neighbors method returning a ‘producer’ ala c#, we do the opposite and pass it a ‘consumer’.

    If course, it’d be nice to support both approaches, and the compiler can indeed help (although it’s *really* nice if it doesn’t have to, or a minor tweak ‘enables’ something cool like this!), but I’m still getting my head around a lot of this stuff and don’t want to commit to anything yet.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.