How to apply a function to a container/array ?
Domingo via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Oct 15 18:32:27 PDT 2014
Even better would be if phobos provided it out of the box:
---------------
import std.stdio;
import std.algorithm;
import std.array;
import std.string;
import std.conv;
void stripStringArrayInPlace(T)(T[] ar){for(long i=0,
len=ar.length; i < len; ++i) ar[i] = ar[i].strip;}
T[] stripStringArray(T)(T[] ar){return ar.map!strip.array;}
T[] splitString(T)(T str, T sep=","){return
str.split(sep).map!strip.array;}
void main() {
auto str = " abor , France , Spain ";
auto arstr = str.splitString;
arstr.writeln;
auto ar = [" dad ", " blue "];
ar.writeln;
auto arStriped = ar.map!strip.array;
arStriped.writeln;
auto ar2 = ar.stripStringArray;
ar2.writeln;
ar.stripStringArrayInPlace;
ar.writeln;
alias stringize = map!text;
auto sz = stringize([ 1, 2, 3, 4 ]);
assert(sz.equal(["1", "2", "3", "4"]));
}
---------------
More information about the Digitalmars-d-learn
mailing list