1 module des.stdx.algorithm;
2 
3 public
4 {
5     import std.array;
6     import std.algorithm;
7     import std.range;
8     import std.traits;
9 }
10 
11 import des.ts;
12 
13 /// map and get result as array
14 template amap(fun...) if ( fun.length >= 1 )
15 {
16     auto amap(Range)(Range r)
17         if (isInputRange!(Unqual!Range))
18     { return array( map!(fun)(r) ); }
19 }
20 
21 ///
22 unittest
23 {
24     int[] res = [ 1, 2, 3 ];
25     void func( int[] arr ) { res ~= arr; }
26     func( amap!(a=>a^^2)(res) );
27     assertEq( res, [ 1, 2, 3, 1, 4, 9 ] );
28 }
29 
30 ///
31 bool oneOf(E,T)( T val )
32     if( is( E == enum ) )
33 {
34     foreach( pv; [EnumMembers!E] )
35         if( pv == val ) return true;
36     return false;
37 }
38 
39 ///
40 unittest
41 {
42     enum TestEnum
43     {
44         ONE = 1,
45         TWO = 2,
46         FOUR = 4
47     }
48 
49     assert( !oneOf!TestEnum(0) );
50     assert(  oneOf!TestEnum(1) );
51     assert(  oneOf!TestEnum(2) );
52     assert( !oneOf!TestEnum(3) );
53     assert(  oneOf!TestEnum(4) );
54     assert( !oneOf!TestEnum(5) );
55 }