www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - No static fold in phobos ?

reply userabcABC123 <userabcABC123 ab.ba> writes:
That would work on an AliasSeq ?
I'm surprised not to find one:

eg:

~~~~~~~
enum seq = AliasSeq!("aa", "bb");
enum val = staticFold!((a,b)=>a~b, seq);
static assert(val == "aabb");
~~~~~~~

it works with foreach or a eponymous template that consumes the 
sequence , but as said where is it in phobos ?
Nov 27 2015
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/27/2015 12:57 PM, userabcABC123 wrote:
 That would work on an AliasSeq ?
 I'm surprised not to find one:

 eg:

 ~~~~~~~
 enum seq = AliasSeq!("aa", "bb");
 enum val = staticFold!((a,b)=>a~b, seq);
 static assert(val == "aabb");
 ~~~~~~~

 it works with foreach or a eponymous template that consumes the sequence
 , but as said where is it in phobos ?
Thanks to CTFE, there is usually no need for static of anything in D. Your code works with a few changes: import std.meta; import std.algorithm; void main() { enum seq = AliasSeq!("aa", "bb"); enum val = [ seq ].reduce!((a, b) => a ~ b); static assert(val == "aabb"); } Ali
Nov 27 2015
parent userabcABC123 <userabcABC123 ab.ba> writes:
On Friday, 27 November 2015 at 23:46:32 UTC, Ali Çehreli wrote:
 On 11/27/2015 12:57 PM, userabcABC123 wrote:
 That would work on an AliasSeq ?
 I'm surprised not to find one:

 eg:

 ~~~~~~~
 enum seq = AliasSeq!("aa", "bb");
 enum val = staticFold!((a,b)=>a~b, seq);
 static assert(val == "aabb");
 ~~~~~~~

 it works with foreach or a eponymous template that consumes 
 the sequence
 , but as said where is it in phobos ?
Thanks to CTFE, there is usually no need for static of anything in D. Your code works with a few changes: import std.meta; import std.algorithm; void main() { enum seq = AliasSeq!("aa", "bb"); enum val = [ seq ].reduce!((a, b) => a ~ b); static assert(val == "aabb"); } Ali
Thx, so the trick was to pack seq in something that's a range...
Nov 27 2015
prev sibling parent Meta <jared771 gmail.com> writes:
On Friday, 27 November 2015 at 20:57:44 UTC, userabcABC123 wrote:
 That would work on an AliasSeq ?
 I'm surprised not to find one:

 eg:

 ~~~~~~~
 enum seq = AliasSeq!("aa", "bb");
 enum val = staticFold!((a,b)=>a~b, seq);
 static assert(val == "aabb");
 ~~~~~~~

 it works with foreach or a eponymous template that consumes the 
 sequence , but as said where is it in phobos ?
There's not much sense in a staticFold template that takes a function literal, as you can use regular functions in D at compile time. However, you need to use template "functions" for some things, so here's a simple way to do that: template staticFold(alias f, List...) if (List.length > 1) { static if (List.length == 2) { alias staticFold = f!(List[0], List[1]); } else { alias staticFold = staticFold!(f, f!(List[0], List[1]), List[2..$]); } } enum cat(alias l, alias r) = l ~ r; void main() { assert(staticFold!(cat, [0], [1]) == [0, 1]); assert(staticFold!(cat, [0], [1], [2]) == [0, 1, 2]); assert(staticFold!(cat, [0], [1], [2], [3]) == [0, 1, 2, 3]); assert(staticFold!(cat, [0], 1, 2, 3, 4) == [0, 1, 2, 3, 4]); }
Nov 27 2015