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.
-
AuthorPosts
-
February 4, 2016 at 12:04 am #263
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 dofor(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?
February 10, 2016 at 11:19 pm #277To 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:
Monkey123456789Method Neighbors( g:Generator<Vector2> )g.Yield( this+up )g.Yield( this+left )...etc...EndThen, a global function like ‘Generate’ could be used to do the generation (not sure if it’s this simple…!) eg:
Monkey1234567Function Generate<T>:Generator<T>( func( g:Generator<T> ) )Local g:=new Generator<T>func( g )Return gEnd…which could be used by client code like this…
Monkey12345678910Function Main()For Local v2:=Eachin Generate( v1.Neighbors )...NextEndThis 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.
-
AuthorPosts
You must be logged in to reply to this topic.