digitalmars.D.learn - How to apply a function to a container/array ?
- Domingo (32/32) Oct 15 2014 Ideally I want to use something like this:
- bearophile (25/56) Oct 15 2014 D algorithms like "map" don't return an array, but a lazy range.
- Domingo (18/18) Oct 15 2014 Thanks so much it's a bit more bloated than I was expecting but
- Domingo (31/31) Oct 15 2014 Even better would be if phobos provided it out of the box:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (24/25) Oct 15 2014 import std.stdio;
Ideally I want to use something like this: --------- import std.stdio; import std.string; import std.algorithm; import std.conv; void main() { string[] ar = [" dad ", " blue "]; writeln(typeid(ar)); //ar.each(writeln); //ar.map!writeln; //ar.apply!writeln; //string[] ar_striped = ar.map!strip; //string[] ar_striped = ar.apply!strip; //string[] ar_striped = ar.each!strip; //ar_striped.each!writeln; //ar_striped.apply!writeln; //ar_striped.map!writeln; alias stringize = map!(to!string); auto sz = stringize([ 1, 2, 3, 4 ]); writeln(typeid(sz)); assert(equal(sz, [ "1", "2", "3", "4" ])); } --------- But none of then work, any idea of how to that in D ? Cheers !
Oct 15 2014
Domingo:Ideally I want to use something like this: --------- import std.stdio; import std.string; import std.algorithm; import std.conv; void main() { string[] ar = [" dad ", " blue "]; writeln(typeid(ar)); //ar.each(writeln); //ar.map!writeln; //ar.apply!writeln; //string[] ar_striped = ar.map!strip; //string[] ar_striped = ar.apply!strip; //string[] ar_striped = ar.each!strip; //ar_striped.each!writeln; //ar_striped.apply!writeln; //ar_striped.map!writeln; alias stringize = map!(to!string); auto sz = stringize([ 1, 2, 3, 4 ]); writeln(typeid(sz)); assert(equal(sz, [ "1", "2", "3", "4" ])); } --------- But none of then work, any idea of how to that in D ?D algorithms like "map" don't return an array, but a lazy range. Use ".array" if you need an array: void main() { import std.stdio; import std.algorithm; import std.array; import std.string; import std.conv; immutable ar = [" dad ", " blue "]; pragma(msg, typeof(ar)); ar.writeln; auto arStriped = ar.map!strip; arStriped.writeln; pragma(msg, typeof(arStriped)); immutable arStripedArray = arStriped.array; pragma(msg, typeof(arStripedArray)); alias stringize = map!text; auto sz = stringize([ 1, 2, 3, 4 ]); assert(sz.equal(["1", "2", "3", "4"])); } Usually it's better to use pragma+typeof, typeid is used less often, for run time management/use of types. Bye, bearophile
Oct 15 2014
Thanks so much it's a bit more bloated than I was expecting but it works. ------- void main() { import std.stdio; import std.algorithm; import std.array; import std.string; import std.conv; auto ar = [" dad ", " blue "]; ar.writeln; auto arStriped = ar.map!strip.array; arStriped.writeln; alias stringize = map!text; auto sz = stringize([ 1, 2, 3, 4 ]); assert(sz.equal(["1", "2", "3", "4"])); } --------
Oct 15 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"])); } ---------------
Oct 15 2014
On 10/15/2014 04:26 PM, Domingo wrote:Ideally I want to use something like this:import std.stdio; import std.string; import std.algorithm; import std.conv; void main() { string[] ar = [" dad ", " blue "]; writeln(typeid(ar)); // a) foreach for purely side-effect expressions: foreach (s; ar) { writeln(s); } // b) Element format specifiers %( %) and %| writefln("%-(<%s>%|, %)", ar); // Again: auto ar_striped = ar.map!strip; writefln("%-(%s%)", ar_striped); alias stringize = map!(to!string); auto sz = stringize([ 1, 2, 3, 4 ]); writeln(typeid(sz)); assert(equal(sz, [ "1", "2", "3", "4" ])); } Ali
Oct 15 2014