View mode: basic / threaded / horizontal-split · Log in · Help
April 27, 2008
Re: Automatic Foreach
janderson wrote:
> I know Walter has his head stuck in Const/Invarient/Pure multithreading 
> land at the moment.  I thought I'd fire off this interesting proposal 
> anyway.
> 
> This is a follow up proposal/suggestion to one I sent a while back.  I 
> hope to flesh it out a bit more.
> 
> The idea is to automatically generate for loops for arrays that are used 
> as parameters.  It will reduces type safety in one area but makes the 
> language more expressive, particularly for templates.
> 
> Basically:
> 
> void foo(char c)
> {
>  ...
> }
> 
> ...
> 
> char[] array;
> 
> ...
> 
> This part:
> 
> foreach(auto value; array)
> {
>   foo(array);
> }

foreach(auto value; array)
{
    foo(value);
}

> 
> becomes:
> 
> foo(array);
April 27, 2008
Re: Automatic Foreach
Janice Caron a écrit :
> I think I'd be happy with a template in std.algorithm.
> 
>     // a[] = b[] + c[]
>     auto a = parallel!("a = b + c")(a,b,c);
> 
>     // foo(array1[], array2[], array3[], array4[])
>     parallel!("foo(a,b,c,d)")(array1,array2,array3,array4);
> 
> Or some such. I'm sure Andrei could write that. He's a clever guy.

Basically, that would be an equivalent of the "map" function present in 
functional programming languages.
April 27, 2008
Re: Automatic Foreach
Janice Caron wrote:
> On 27/04/2008, janderson <askme@me.com> wrote:
>> Janice Caron wrote:
>>
>>> Actually, I should have said that, in general, Joel's proposal is
>>> undefined for all non-unary operations.
>>>
>>>   a[] = b[] + c[]
>>>
>>>   f(a[], b[], c[], d[])
>>>
>>  My proposal was to generate nested loops and base it of the order the
>> arrays where specified. However it could just as easily be parallel for all
>> values and require that all arrays are the same size.
> 
> I think I'd be happy with a template in std.algorithm.
> 
>     // a[] = b[] + c[]
>     auto a = parallel!("a = b + c")(a,b,c);
> 
>     // foo(array1[], array2[], array3[], array4[])
>     parallel!("foo(a,b,c,d)")(array1,array2,array3,array4);
> 
> Or some such. I'm sure Andrei could write that. He's a clever guy.

I don't know.  Might as well use a foreach if its going to start looking 
like that.

-Joel
April 27, 2008
Re: Automatic Foreach
Reply to janderson,
> 
> float[] array;
> array ~= sqrt(array2);


how about


char[] to;
char[][] from;

to ~= from;

goes to

uint add=0;
foreach(a;from) add+=a.length;

to.length += add;

foreach(a;from)
{
to[$-add,$-add+a.length = a[]
add-= a.length;
}

??
April 28, 2008
Re: Automatic Foreach
I think people have missed the point on my suggestion.  I wanted 
something that I could use within a template (see example at bottom). 
Something that I could override later or use the overridden case (if 
someone had already written a function to handle arrays of that type).


I often write functions like:

int Covert(int a)
{
...
}


Then endup writing an array version as well.

int[] Convert(int[] a)
{
  int result[];
  result.length = a.length;
  int i=0;
  foreach( ; )
  {
    result [i++];

  }
}

80% of the time the array version is pretty much the same thing.  So a 
while back I wrote a template that works like:

void foo(int i)
{
  printf("B");
}

alias AutoForeach!(foo, int) foo;

Then I could go:

for(6); //Prints "B"
int[] array;
array = 2;
foo(array); //Prints "BB"

But then I couldn't specialize it later without removing the alias, 
because I'd get a name clash.

The other problem is this won't do cases like:

result ~= foo(array);

or

foo(foo1(array));  //Runs foo and foo1 array.length times.


It started cutting my code size down by about 30%.  So I thought it 
might be a good idea for the language.  The language would be able to 
provide provide the candy that you would never be able to get with 
templates.


//////////////////////////////////////////////////
//Use example with templates
//////////////////////////////////////////////////

void print(A ...)(A a)
{
  write(a);
}

print(array1[], array2[], value);

//What happens here is the array is passed into the template, because an 
array is a valid input into a template.  The value is only evaluated 
when inside the template.

So it's equivalent to:

foreach (auto val; array1)
{
  write(val);
}

foreach (auto val; array2)
{
  write(val);
}

write(value);
April 28, 2008
Re: Automatic Foreach
> What do you think?
I think Walter really needs to get macros implemented in D.

Then we could all make our own little specific loops, which is far more 
reasonable. Your idea is well demonstrated, but I don't like the syntax. 
It doesn't really tell you what is going on just by looking at it.


PS: You also might want to look at 'map' in phobos2's std.algorithm.
April 28, 2008
Re: Automatic Foreach
Jarrod wrote:
>> What do you think?
> I think Walter really needs to get macros implemented in D.
> 
> Then we could all make our own little specific loops, which is far more 
> reasonable. Your idea is well demonstrated, but I don't like the syntax. 
> It doesn't really tell you what is going on just by looking at it.
> 
> 
> PS: You also might want to look at 'map' in phobos2's std.algorithm. 

Python has map() too, but people prefer to use list comprehensions for 
some reason.

--bb
April 29, 2008
Re: Automatic Foreach
janderson wrote:
> while back I wrote a template that works like:
> 
> void foo(int i)
> {
>   printf("B");
> }
> 
> alias AutoForeach!(foo, int) foo;
> 
> Then I could go:
> 
> for(6); //Prints "B"
> int[] array;
> array = 2;
> foo(array); //Prints "BB"
> 
> But then I couldn't specialize it later without removing the alias, 
> because I'd get a name clash.
> 
> The other problem is this won't do cases like:
> 
> result ~= foo(array);
> 

Why not? Didn't you define AutoForeach!(foo, int) to return an int[] ?

> or
> 
> foo(foo1(array));  //Runs foo and foo1 array.length times.
> 
> 

Same as above.

-- 
Bruno Medeiros - Software Developer, MSc. in CS/E graduate
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
April 29, 2008
Re: Automatic Foreach
Bill Baxter wrote:
> Jarrod wrote:
>>> What do you think?
>> I think Walter really needs to get macros implemented in D.
>>
>> Then we could all make our own little specific loops, which is far 
>> more reasonable. Your idea is well demonstrated, but I don't like the 
>> syntax. It doesn't really tell you what is going on just by looking at 
>> it.
>>
>>
>> PS: You also might want to look at 'map' in phobos2's std.algorithm. 
> 
> Python has map() too, but people prefer to use list comprehensions for 
> some reason.
> 
> --bb

Comprehensions might be syntactically nicer than map, but as for the 
original proposal (automatic foreach), the map construct covers it quite 
nicely and sufficiently.

-- 
Bruno Medeiros - Software Developer, MSc. in CS/E graduate
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
April 29, 2008
SwapStrategy issues again...
I'm getting the following error messages now. Does SwapStrategy have to be specified at compile time?

C:\Documents and Settings\dag033\D\d_dedup>dmd -g d_dedup.d ..\d_libcsv\d_libcsv.d ..\d_libcsv\csv.d ..\d_libcsv\bin\deb
ug\d_libcsv.lib
c:\dmd\bin\..\src\phobos\std\algorithm.d(2632): Error: expression ss == cast(SwapStrategy)0 is not constant or does not
evaluate to a bool
c:\dmd\bin\..\src\phobos\std\algorithm.d(2655): static assert  (ss != cast(SwapStrategy)1) is not evaluatable at compile
time

Here's the call to schwartzSort!:

protected:
   SwapStrategy ss;
.
.
.
schwartzSort!(kx.get_key, "a < b", ss)(lines);

Thanx

Brian
1 2 3 4
Top | Discussion index | About this forum | D home