2006-08-24

yield

So i found this keyword that i did not know about in c#. Intriguing. So what it does is creates an enumeration on the fly, without having to code a class to do it. Sounds good, but what could i use it in. A few examples suggest that it be used in tree traveral:

        public IEnumerator<Node> GetDepthFirstTraversalEnumerator() {
            yield return this.value;
            foreach(Node child in this.children) {
                IEnumerator<Node> enum_ = kidchildGetDepthFirstTraversalEnumerator();
                while(enum_.MoveNext()) {
                    yield return enum_.Current;
                }
            }
        }
 
and also found the map function from other laungauges:

        public delegate object MapFunction(object o);
        public static IEnumerable Map(MapFunction mapFunction, IEnumerable enumerable) {
            foreach(object o in enumerable) {
                yield return mapFunction(o);
            }
        }
        public static IEnumerable PositiveIntegers() {
            int a = 1;
            while(true) {
                yield return a;
                a = a + 1;
            }
        }
        public static IEnumerable SubList(int start, int end, IEnumerable enumerable) {
            int count = 0;
            foreach(object o in enumerable) {
                count++;
                if(count < start) {
                    continue;
                } else if(count <= end) {
                    yield return o;
                } else {
                    yield break;
                }
            }
        }
        IEnumerable set = I.SubList(5, 15, I.PositiveNumbers());
        foreach(int value in Map(delegate(object o) { return ((int)o) * 2; }, )) {
            //do something
        }
 
heres the link to a Fibonacci generator 
http://www.cerkit.com/cerkitBlog/2004/06/30/Using+The+Yield++Statement+In+Whidbey+C.aspx
 
a lazy paged collection:
http://haacked.com/archive/2006/08/14/FunIteratingPagedCollectionsWithGenericsAndIterators.aspx
 
and a textreader:
http://blogs.msdn.com/jmstall/archive/2005/08/08/textreader_yield.aspx

2 comments:

oakleyses said...

instyler, hollister, oakley, nfl jerseys, jimmy choo outlet, nike air max, north face outlet, soccer jerseys, louboutin, hollister, celine handbags, ghd hair, baseball bats, soccer shoes, gucci, ray ban, beats by dre, longchamp uk, lululemon, nike air max, ralph lauren, ferragamo shoes, bottega veneta, converse outlet, p90x workout, mcm handbags, nike trainers uk, converse, vans outlet, north face outlet, giuseppe zanotti outlet, hermes belt, reebok outlet, mont blanc pens, hollister clothing, chi flat iron, babyliss, herve leger, mac cosmetics, nike roshe run, valentino shoes, insanity workout, vans, nike huaraches, wedding dresses, iphone cases, timberland boots, new balance shoes, abercrombie and fitch, asics running shoes

oakleyses said...

canada goose, louis vuitton, replica watches, juicy couture outlet, pandora jewelry, canada goose, ugg,uggs,uggs canada, doudoune moncler, canada goose outlet, canada goose uk, canada goose jackets, louis vuitton, moncler outlet, barbour, pandora charms, ugg, moncler, louis vuitton, ugg uk, lancel, barbour uk, marc jacobs, moncler, links of london, karen millen uk, toms shoes, swarovski, wedding dresses, canada goose, louis vuitton, ugg,ugg australia,ugg italia, pandora jewelry, moncler, moncler outlet, supra shoes, hollister, swarovski crystal, thomas sabo, juicy couture outlet, canada goose outlet, moncler uk, pandora uk, coach outlet, ugg pas cher, louis vuitton, montre pas cher, canada goose outlet, moncler