c# - Can't figure out why this IEnumerable is giving this answer -



c# - Can't figure out why this IEnumerable is giving this answer -

the reply next code 5

can explain why is? if replace int d1 = x.current d1 = x.current , declare d1 above while loop, reply 2 , understand why don't know why it's 5 otherwise.

ienumerable<int> num = new []{10,11,12,13,14,15,16}; ienumerable<int> div = new [] {2,3,5}; var lazy = enumerable.empty<int>(); var x = div.getenumerator(); while(x.movenext()){ int d1 = x.current; lazy = lazy.concat(num.where(s=>s % d1 == 0)); } int count = lazy.distinct().count(); console.writeline("{0}",count);

edit: here snippet gives reply of 2.

ienumerable<int> num = new []{10,11,12,13,14,15,16}; ienumerable<int> div = new [] {2,3,5}; var lazy = enumerable.empty<int>(); var x = div.getenumerator(); int d1; while(x.movenext()){ d1 = x.current; lazy = lazy.concat(num.where(s=>s % d1 == 0)); } int count = lazy.distinct().count(); console.writeline("{0}",count);

(peter beat me punch, half done when reply appeared, i'll post anyhow.)

you can more insight difference instrumenting code. instrumentation within lambda expression thing provides crucial insight:

class programme { static void main(string[] args) { console.writeline("baseline."); test1(); console.writeline("modified."); test2(); } static void test1() { ienumerable<int> num = new[] { 10, 11, 12, 13, 14, 15, 16 }; ienumerable<int> div = new[] { 2, 3, 5 }; var lazy = enumerable.empty<int>(); var x = div.getenumerator(); while (x.movenext()) { int d1 = x.current; console.writeline("d1 = " + d1); lazy = lazy.concat(num.where(s => {bool result = s % d1 == 0; console.writeline("s = " + s + ", d1 = " + d1); homecoming result;})); console.writeline("lazy has " + lazy.count()); } console.writeline("evaluating lazy.distinct().count()"); int count = lazy.distinct().count(); console.writeline("{0}", count); } static void test2() { ienumerable<int> num = new[] { 10, 11, 12, 13, 14, 15, 16 }; ienumerable<int> div = new[] { 2, 3, 5 }; var lazy = enumerable.empty<int>(); var x = div.getenumerator(); int d1; while (x.movenext()) { d1 = x.current; console.writeline("d1 = " + d1); lazy = lazy.concat(num.where(s => {bool result = s % d1 == 0; console.writeline("s = " + s + ", d1 = " + d1); homecoming result;})); console.writeline("lazy has " + lazy.count()); } console.writeline("evaluating lazy.distinct().count()"); int count = lazy.distinct().count(); console.writeline("{0}", count); } }

after "evaluating lazy.distinct().count()" printed, you'll notice 2 things might surprise you.

first, evaluation requires re-running lambda look declared within loop. easy, wrong, think of "lazy" collection of integers. in fact function creating collection of integers, counting distinct elements requires re-running function.

second, you'll notice values of d1 different between 2 evaluations. in first case, d1 2, , in sec case d1 5. reason is, peter duniho pointed out, declaring d1 outside loop allows retain value had when loop finished (so have 5, lastly value in div sequence), wherease declaring within loop requires re-computing (so have 2, first element in div sequence).

c# ienumerable

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -