my ideas for array operations

Daniel Keep daniel.keep.lists at gmail.com
Sat Oct 6 03:24:09 PDT 2007



dennis luehring wrote:
> i have some ideas for array operation
> hope they are "context free" and don't break any syntactic rules :-}

I don't think we need these.  The operators are very specific, and
aren't very obvious.  Incidentally, a lot of what you propose could be
done better, I think, with a decent functional library.

> OP ==> + - / ~ * and all the others
> 
> #1 operator between (operator concatenated) array elements:
> 
> syntax: array ~OP
> 
> result-type: value-type of array
> 
> examples:
> 
> [1,2,3] ~+ ==> 1+2+3 = 6
> 
> [1,3,4] ~* ==> 1*3*4 = 12
> 
> ["a","b","c"] ~~ ==> "abc"

reduce((int a, int b){return a+b;}, [1,2,3]) ==> 6
reduce((int a, int b){return a*b;}, [1,3,4]) ==> 12
reduce((string a, string b){return a~b;}, ["a","b","c"]) ==> "abc"

> #2 operator and "value" on each array element
> 
> syntax: array @OP value
> 
> value: array.value/applicable-type/function
> 
> result-type: typeof(array)
> 
> [1,2,3] @+ 10 ==> [1+10,2+10,3+10] = [11,12,13]
> 
> (maybe .+)

map((int a){return a+10;}, [1,2,3]) ==> [11,12,13]

> #3 operator between 2 arrays
> 
> syntax: array1 OP array2
> 
> result-type: typeof(array1)
> 
> precondition:
>   array1.value/applicable-type == array2.value/applicable-type
>   array1.dimension == array2.dimension
> 
> [a,b,c] + [d,e,f] ==> [a+d,b+e,c+f]

map((T a, T b){return a+b;}, zip([a,b,c], [d,e,f]));

These constructs are much more general, and more powerful.  And they can
be implemented as library code right now.

	-- Daniel



More information about the Digitalmars-d mailing list