www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there an elegant way of making a Result eager instead of lazy?

reply "ixid" <nuaccount gmail.com> writes:
I understand the point of lazy evaluation but I often want to use 
the lazy algorithm library functions in an eager way. Other than 
looping through them all which feels rather messy is there a good 
way of doing this?

Is there a reason not to allow the following to be automatically 
treated eagerly or is there some kind of cast or conv way of 
doing it?

	int[] test1 = [1,2,3,4];
	int[] test2 = map!("a + a")(test1); //Eager, not allowed

	auto test3 = map!("a + a")(test1); //Lazy
Mar 20 2012
parent reply simendsjo <simendsjo gmail.com> writes:
On Tue, 20 Mar 2012 18:36:46 +0100, ixid <nuaccount gmail.com> wrote:

 I understand the point of lazy evaluation but I often want to use the  
 lazy algorithm library functions in an eager way. Other than looping  
 through them all which feels rather messy is there a good way of doing  
 this?

 Is there a reason not to allow the following to be automatically treated  
 eagerly or is there some kind of cast or conv way of doing it?

 	int[] test1 = [1,2,3,4];
 	int[] test2 = map!("a + a")(test1); //Eager, not allowed

 	auto test3 = map!("a + a")(test1); //Lazy
std.array includes a method, array(), for doing exactly this: int[] test2 = array(map!"a+a"(test1)); //eager
Mar 20 2012
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
simendsjo:

 std.array includes a method, array(), for doing exactly this:
 int[] test2 = array(map!"a+a"(test1)); //eager
With 2.059 you can write that also in a more readable way, because there is less nesting: int[] test2 = test1.map!q{a + a}().array(); Bye, bearophile
Mar 20 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 03/20/2012 10:50 AM, bearophile wrote:
 simendsjo:

 std.array includes a method, array(), for doing exactly this:
 int[] test2 = array(map!"a+a"(test1)); //eager
With 2.059 you can write that also in a more readable way, because
there is less nesting:
 int[] test2 = test1.map!q{a + a}().array();
Going off-topic but there is also the new => lambda syntax: int[] test2 = test1.map!(a => a + a)(test1).array(); Although, it makes it longer in cases like the one above. :) By the way, is there a name for "the => syntax"?
 Bye,
 bearophile
Ali
Mar 20 2012
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, Mar 20, 2012 at 02:52:05PM -0700, Ali Çehreli wrote:
[...]
 By the way, is there a name for "the => syntax"?
[...] You just named it. :-) T -- "Real programmers can write assembly code in any language. :-)" -- Larry Wall
Mar 20 2012
prev sibling parent "ixid" <nuaccount gmail.com> writes:
Thanks, very handy!
Mar 20 2012