1 module libd.algorithm.mutate; 2 3 auto map(alias Mapper, RangeT)(RangeT range) 4 { 5 import libd.algorithm; 6 7 alias ElementT = typeof(Mapper(range.front)); 8 static struct Range 9 { 10 private RangeT _range; 11 private ElementT _front; 12 private bool _empty; 13 14 @nogc nothrow: 15 16 this(RangeT range) 17 { 18 this._range = range; 19 this.popFront(); 20 } 21 22 void popFront() 23 { 24 if(this._range.empty) 25 { 26 this._empty = true; 27 return; 28 } 29 30 this._front = Mapper(this._range.front); 31 this._range.popFront(); 32 } 33 34 bool empty() 35 { 36 return this._empty; 37 } 38 39 ElementT front() 40 { 41 return this._front; 42 } 43 } 44 45 return Range(range); 46 } 47 /// 48 @("map") 49 unittest 50 { 51 import libd.data.conv, libd.algorithm; 52 53 Array!int nums; 54 nums.put(1, 2, 3); 55 56 String[3] expected = [String("1"), String("2"), String("3")]; 57 auto range = nums.range.map!(n => n.to!String); 58 assert(range.equals!((a,b) => a == b)(expected[0..$])); 59 }