www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to apply a function to a container/array ?

reply "Domingo" <mingodad gmail.com> writes:
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
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent reply "Domingo" <mingodad gmail.com> writes:
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
parent "Domingo" <mingodad gmail.com> writes:
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
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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