specifying an auto array type

Jay Norwood via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Dec 26 21:45:22 PST 2015


This is getting kind of a long example, but I'm really only 
interested in the last 4 or 5 lines. This works as desired, 
creating the array of tuples, but I'm wondering if there is a way 
to have the Tuple array defined as auto instead of having to 
specify the types.  I tried using .array() at the end of the last 
samples.each!, but couldn't find an implementation that worked.

Yes, I know, some of these imports aren't required (yet).

import std.stdio;
import std.algorithm;
import std.conv;
import std.range;
import std.typecons;
import std.parallelism;
import std.array;

struct S { ulong a; ulong b; ulong c; ulong d; double e; ulong f;}
ulong f1(ref S s) { with(s){return a+b;}}
double f2(ref S s) { with(s){return (c+d)/e;}}
double f3(ref S s) { with(s){return (c+f)/e;}}

int main()
{

	S[10] samples;
	// initialize some values
	foreach ( int i, ref s; samples){
		int j=i+1;
		with (s){
			a=j; b=j*2; c=j*3; d=j*4; e=j*10; f=j*5;
		}
	}

	// apply several functions on each  sample
	samples.each!((int i, ref 
a)=>tuple!("sample","f1","f2","f3")(i,f1(a),f2(a),f3(a)).writeln());

	// output the function results to an array of tuples
	Tuple!(int, ulong, double, double)[] arr;

	samples.each!((int i, ref a)=> arr ~= 
tuple!("sample","f1","f2","f3")(i,f1(a),f2(a),f3(a)));
	writeln(arr);
	return 0;
}


More information about the Digitalmars-d-learn mailing list