www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Auto Interation

reply janderson <askme me.com> writes:
Here's a random idea that I think may be a little bit much:

Auto Array iteration.

If you have a function like:

void foo(int x);

Then the language could allow you to use it on arrays like:

auto array = new int[];
foo(array);  //Iterate though every array
foo(5);  //One element like normal

foo(int[] x);  //Override array version of the operation to do something 
more optimal.

It may make logic errors more difficult to see, so scratch that idea.

Perhaps it could be done with some template syntax in a slightly more 
verbose way.
Sep 01 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
janderson wrote:
 Here's a random idea that I think may be a little bit much:
 
 Auto Array iteration.
 
 If you have a function like:
 
 void foo(int x);
 
 Then the language could allow you to use it on arrays like:
 
 auto array = new int[];
 foo(array);  //Iterate though every array
 foo(5);  //One element like normal
 
 foo(int[] x);  //Override array version of the operation to do something 
 more optimal.
 
 It may make logic errors more difficult to see, so scratch that idea.
 
 Perhaps it could be done with some template syntax in a slightly more 
 verbose way.
That's what the function called 'apply' typically does in the functional paradigm. Something like apply(foo, array). With the extended member syntax proposed you could even make it be foo.apply(array). --bb
Sep 01 2007
parent janderson <askme me.com> writes:
Bill Baxter wrote:
 janderson wrote:
 Here's a random idea that I think may be a little bit much:

 Auto Array iteration.

 If you have a function like:

 void foo(int x);

 Then the language could allow you to use it on arrays like:

 auto array = new int[];
 foo(array);  //Iterate though every array
 foo(5);  //One element like normal

 foo(int[] x);  //Override array version of the operation to do 
 something more optimal.

 It may make logic errors more difficult to see, so scratch that idea.

 Perhaps it could be done with some template syntax in a slightly more 
 verbose way.
That's what the function called 'apply' typically does in the functional paradigm. Something like apply(foo, array). With the extended member syntax proposed you could even make it be foo.apply(array). --bb
Some other languages I've use have a foreach function in the standard lib. However the point is those can't be overridden. -Joel
Sep 01 2007