1 module des.util.stdext.algorithm; 2 3 public 4 { 5 import std.array; 6 import std.algorithm; 7 import std.range; 8 import std.traits; 9 } 10 11 /// 12 template amap(fun...) if ( fun.length >= 1 ) 13 { 14 auto amap(Range)(Range r) 15 if (isInputRange!(Unqual!Range)) 16 { return array( map!(fun)(r) ); } 17 } 18 19 /// 20 unittest 21 { 22 int[] res = [ 1, 2, 3 ]; 23 void func( int[] arr ) { res ~= arr; } 24 func( amap!(a=>a^^2)(res) ); 25 assert( res == [ 1, 2, 3, 1, 4, 9 ] ); 26 } 27 28 /// 29 bool oneOf(E,T)( T val ) 30 if( is( E == enum ) ) 31 { 32 foreach( pv; [EnumMembers!E] ) 33 if( pv == val ) return true; 34 return false; 35 } 36 37 private version(unittest) 38 { 39 enum TestEnum 40 { 41 ONE = 1, 42 TWO = 2, 43 FOUR = 4 44 } 45 } 46 47 unittest 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 } 56 57 /// 58 bool oneOf(E,T)( E[] arr, T val ) 59 if( is( typeof( arr[0] == val ) ) ) 60 { 61 foreach( pv; arr ) if( pv == val ) return true; 62 return false; 63 } 64 65 unittest 66 { 67 assert( !oneOf( [TestEnum.ONE, TestEnum.TWO], 0) ); 68 assert( oneOf( [TestEnum.ONE, TestEnum.TWO], 2) ); 69 }