www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Array-wise expressions

reply "Lobachevsky" <ibeam2000 gmail.com> writes:
Is there any plan to extend array-wise expressions to include 
calls to arbitrary "scalar" functions, i.e.

auto a = [1.2, 2.3, 3.4];
auto b = [7.1, 8.2, 9.3];
auto c = new double[3];
c[] = sin( 4 * a[] + b[] );

or

c[] = foo(a[], b[], 42);
Jan 06 2013
next sibling parent "David Nadlinger" <see klickverbot.at> writes:
On Sunday, 6 January 2013 at 15:39:03 UTC, Lobachevsky wrote:
 Is there any plan to extend array-wise expressions to include 
 calls to arbitrary "scalar" functions

Currently, there are no plans for adding a feature like this – at least I'm not aware of any. However, you can just use std.algorithm.map and write map!sin(…), possibly followed by .array() (import std.array) for eager evaluation. David
Jan 06 2013
prev sibling parent "Xinok" <xinok live.com> writes:
On Sunday, 6 January 2013 at 15:39:03 UTC, Lobachevsky wrote:
 Is there any plan to extend array-wise expressions to include 
 calls to arbitrary "scalar" functions, i.e.

 auto a = [1.2, 2.3, 3.4];
 auto b = [7.1, 8.2, 9.3];
 auto c = new double[3];
 c[] = sin( 4 * a[] + b[] );

 or

 c[] = foo(a[], b[], 42);

Performing this action would require several calls to foo. By convention, array operations aren't loops; it only calculates the R-value once, then sets all elements to the same value. I wrote this as a quick example: http://dpaste.dzfl.pl/4ba03ef6 It does get a random value, but it only calls rand() once so all of the elements in the array are set to the same random value.
Jan 06 2013