score:1

the paragraph you showed from the book doesn't say it will print a single number. it says it will never print a single number.

[...] this program would never make it as far as printing out a single number [...]

indeed, deferred execution would allow you to run through an infinite sequence, while exeucting the full sequence without deferred exeuction would never end.

score:3

how can i define an extension method of where like the one in code 3 for the fibonacci example

you can use yield return again if you want deferred execution.

public static ienumerable<cultureinfo> mywhere(this ienumerable<cultureinfo> source, predicate<cultureinfo> filter) {
    foreach (var thing in source) {
        if (filter(thing)) yield return thing;
    }
}

note that it has to return and accept ienumerable<cultureinfo>s because arrays can't be "infinite", ienumerable<t>s can, because they can use deferred execution.

why would it print out a single number when the where method would never return?

if you try to call your customlinqprovider.where method with an infinite ienumerable<cultureinfo>, you'd first have to convert that to a cultureinfo[], which you will do by using toarray. since the ienumerable<t> is infinite, toarray will go on forever. it's not that where will not return, it's that toarray will not.

also, the book says

this program would never make it as far as printing out a single number

"would never make it as far as" means "wouldn't even".

score:4

i defined an extension method as the following and it didn't print out anything. the point is that when it's going to be treated like an array then the where method would never return, hence the evaluation of evenfib would never end and we'll have nothing printed. i also made sure in my code when both linq to object provider and my own extension method are available, my extension method would be used. i didn't change code 1 and code 2 by the way.

public static t[] where<t>(this ienumerable<t> src,
        predicate<t> filter)
        {
            var array = src.toarray();           
            return array.findall(array,filter);
        }

Related Query

More Query from same tag