December 15, 2010
"lurker" <lurk@lurk.net> wrote in message news:ie8rc3$27l1$1@digitalmars.com...
> Nick Sabalausky Wrote:
>
>> "piotrek" <starpit@tlen.pl> wrote in message news:ie8fu9$ejl$1@digitalmars.com...
>> > On Mon, 13 Dec 2010 09:49:50 -0600, Andrei Alexandrescu wrote:
>> >> By the way, I couldn't stop cringing at the distasteful, male-centric sexual jokes that the talk is peppered with. Wonder if there was any woman in the audience, and how she might have felt. And this is not a ghetto rant - it's the keynote of a major Ruby conference! (And I'm definitely not a prude.) Am I alone in thinking that this is not what our metier should evolve into?
>> >>
>> >>
>> >> Andrei
>> >
>> > You're definitely not. No matter how strong pressure is, I stand and fight against common acceptance for female humiliation. It's so sad to me how many think they're real men while treating women as a things they can freely abuse.
>> >
>>
>> Didja hear the one about the blonde who couldn't find her pencil?
>
> I have a bad sense of humor, but what is this if I'm trolling?

Heh. It's me being an ass :)

But seriously, I'm not flaming anyone here, and really taking a round-about way to point out that there's too much "inventing things to be offended about" going on in the world. I still haven't looked at the video, so maybe some of the things really were worse than the example Andrei mentioned or the joke I referenced above, but *if* these things are the sorts of things that piotrek and Andrei find "offensive to women", then I'd have to call a big "bullshit, this is just inventing blatant assumptions and then getting offended by them".


December 15, 2010
On 14.12.2010 19:49, Simen kjaeraas wrote:
> Stephan Soller <stephan.soller@helionweb.de> wrote:
>
>> [1, 2, 3, 4, 5].select((int e){ return e > 3; })
>> .collect((int e){ return e*e; });
>>
>> It's already fairly compact. The main overhead is the parameter type
>> and the return statement. I don't believe this can be reduced any more
>> without breaking consistency of the language.
>
> The delegates can be passed by alias parameter, and then be written
> without the parameter type:
>
> @property void bar(alias t)() {
> t(3);
> }
>
> void main() {
> bar!((e){writeln(e);});
> }
>

Hm, nice trick, thanks Simen. Looks interesting and more general to use. Will try that for my next D project.

Happy programming
Stephan Soller
December 15, 2010
On 14.12.2010 20:03, Jacob Carlborg wrote:
> On 2010-12-14 19:33, Stephan Soller wrote:
>>>>
>>>> I think it's a matter of consistency. In Ruby blocks are used all the
>>>> time for pretty much everything. In D this isn't the case because
>>>> usually templates are used for stuff where blocks are used in Ruby
>>>> (e.g.
>>>> map, group and find in std.algorithm).
>>>
>>> I think that the templates that take a string as a predicate is just an
>>> ugly hack because D has a too verbose delegate syntax.
>>>
>>
>> I absolutely agree with that. However I don't have a better idea how to
>> write it. Delegate syntax is already fairly compact in D.
>>
>> For example code as this isn't very uncommon in Ruby:
>>
>> [1, 2, 3, 4, 5].select{|e| e > 3}.collect{|e| e*e}
>>
>> Of course this is a simplified example. Usually the collection would
>> contain some objects and the blocks would filter for a method call (like
>> "e.even?"). However I built a small D1 struct that implements a select
>> and collect function. With the unified function call synatax for array
>> with code should actually work:
>>
>> [1, 2, 3, 4, 5].select((int e){ return e > 3; })
>> .collect((int e){ return e*e; });
>>
>> It's already fairly compact. The main overhead is the parameter type and
>> the return statement. I don't believe this can be reduced any more
>> without breaking consistency of the language.
>
> Probably not, but, for example, Scala allows very compact delegate
> literals:
>
> Array(1, 2, 3, 4, 5).select(_ > 3).collect(_ * _)
>
> Or more verbose:
>
> Array(1, 2, 3, 4, 5).select((x) => x > 3).collect((x, y) => x * y)
>
> I'm not 100% sure I that the syntax is correct.
>
>> Delegates are way more verbose in other languages like PHP and
>> JavaScript (more or less the same syntax in both languages). Despite
>> that it's no problem to use it and in case of jQuery it's used very
>> often. I think the main reason why delegates are not used like that in D
>> is performance. I'm really not sure about it but I suspect that
>> delegates are less effective than code directly generated by a template
>> like map!(). I don't know how efficient templates can integrate
>> delegates so I just suspect that this is a performance problem.
>>
>> Happy programming
>> Stephan Soller
>
> PHP has a very verbose delegate syntax with explicit closures, one of
> the many reasons I don't use PHP. JavaScript has quite similar syntax as
> D but the "function" keyword is required, I try to use CoffeeScript
> (compiles to javascript), which has a lot nicer delegate syntax, as
> often I can.
>
> D(dmd) needs to be able to inline delegates.
>

I'm not so much concerned about verbose delegate syntax. Personally I don't mind using the current delegate syntax with templates like map!. However I'm concerned about performance and parameter order. Does someone know how delegates within templates are handled? I looked at the source of std.algorithm and std.functional but I'm still not sure what actually happens if you call something like

	auto ary = [1, 2, 3];
	map!((e){ return e*e; })(ary);

I can't test this code right now since I don't have a D2 compiler installed at this computer but the std.algorithm source contains unit tests with delegates.

If that now works with uniform function call syntax it could be written like that:

	auto ary = [1, 2, 3];
	ary.map!((e){ return e*e; });

I don't know if chaining of such calls is possible but the above syntax is already pretty good. Sure, it's not perfect and the "return" still is some overhead, but I don't think it needs work. What's more important (at least for me) is the chaining ability and how performant such delegates actually are. From what I understood from std.functional it wraps string expressions in delegates anyway so using

	ary.map!("a*a");

is not more efficient. Please correct me if I'm wrong! The std.functional code is definitely above my understanding.


So, _if_ we can figure out a way to do some nice chaining of such calls we can get:

	[1, 2, 3, 4, 5].filter!((e){ return e > 3; })
		.map!((e){ return e*e; });

Or if you don't like delegates and don't mind obvious compile time black magic:

	[1, 2, 3, 4, 5].filter!("a > 3").map!("a * a");

I think if chaining would work like that D would already be pretty close to the expressive power of Ruby. It's just that this kind of programming is very dependent on the ability of built in collections and libraries that allow such a programming style.

Happy programming
Stephan Soller
December 15, 2010
On 12/15/10, Stephan Soller <stephan.soller@helionweb.de> wrote:
>
> So, _if_ we can figure out a way to do some nice chaining of such calls we can get:
>
> 	[1, 2, 3, 4, 5].filter!((e){ return e > 3; })
> 		.map!((e){ return e*e; });
>
> Or if you don't like delegates and don't mind obvious compile time black magic:
>
> 	[1, 2, 3, 4, 5].filter!("a > 3").map!("a * a");
>

You might use pipe! as an alternative:

import std.algorithm;
import std.functional;
import std.stdio;

void main()
{
    int[] arr = [1, 2, 3, 4, 5];
    auto result = pipe!( filter!("a>3"), map!("a * a") )(arr);
    writeln(result);
}

But I can't seem to do the same with delegates:

import std.algorithm;
import std.functional;
import std.stdio;

void main()
{
    int[] arr = [1, 2, 3, 4, 5];
    pipe!( filter!( (e){ return e > 3; } ),
             map!( (e){ return e*e; } ) )(arr);
}

chain.d(9): Error: expression template filter(Range) is not a valid
template value argument
chain.d(9): Error: expression template map(Range) is not a valid template
value argument
December 15, 2010
On Wed, 15 Dec 2010 02:53:40 -0500, Nick Sabalausky wrote:

> "lurker" <lurk@lurk.net> wrote in message news:ie8rc3$27l1$1@digitalmars.com...
>> Nick Sabalausky Wrote:
>>
>>> "piotrek" <starpit@tlen.pl> wrote in message news:ie8fu9$ejl$1@digitalmars.com...
>>> > On Mon, 13 Dec 2010 09:49:50 -0600, Andrei Alexandrescu wrote:
>>> >> By the way, I couldn't stop cringing at the distasteful, male-centric sexual jokes that the talk is peppered with. Wonder if there was any woman in the audience, and how she might have felt. And this is not a ghetto rant - it's the keynote of a major Ruby conference! (And I'm definitely not a prude.) Am I alone in thinking that this is not what our metier should evolve into?
>>> >>
>>> >>
>>> >> Andrei
>>> >
>>> > You're definitely not. No matter how strong pressure is, I stand and fight against common acceptance for female humiliation. It's so sad to me how many think they're real men while treating women as a things they can freely abuse.
>>> >
>>> >
>>> Didja hear the one about the blonde who couldn't find her pencil?
>>
>> I have a bad sense of humor, but what is this if I'm trolling?
> 
> Heh. It's me being an ass :)
> 
> But seriously, I'm not flaming anyone here, and really taking a round-about way to point out that there's too much "inventing things to be offended about" going on in the world. I still haven't looked at the video, so maybe some of the things really were worse than the example Andrei mentioned or the joke I referenced above, but *if* these things are the sorts of things that piotrek and Andrei find "offensive to women", then I'd have to call a big "bullshit, this is just inventing blatant assumptions and then getting offended by them".

Firstly I apologize Andrei if I put him in bad light after hijacking his sub-thread
and then using it to add some personal opinion. Secondly I was referring to
bigger picture of society and not only this video. And still I don't agree to
"tolerate"* behavior which is based on so called "freedom". Because all you do
has its consequence and interacts with "freedom" of other people and vice
versa. Of course appropriate action should be used not exaggerated ones.
And I had a peaceful intercommunication on my mind when speaking about
fighting ;) I can say as a Christian that respect for dignity goes along with love.
But your choice. And in the world the weak ones are abused all the time.
I couldn't live pretending I don't see it. So one way is to give my opinion. Not
don't giving a shit.

Cheers
Piotrek
December 15, 2010
On 2010-12-14 22:04, Nick Sabalausky wrote:
> "Jacob Carlborg"<doob@me.com>  wrote in message
> news:ie8f5f$o6e$1@digitalmars.com...
>>
>> Probably not, but, for example, Scala allows very compact delegate
>> literals:
>>
>> Array(1, 2, 3, 4, 5).select(_>  3).collect(_ * _)
>>
>> Or more verbose:
>>
>> Array(1, 2, 3, 4, 5).select((x) =>  x>  3).collect((x, y) =>  x * y)
>>
>> I'm not 100% sure I that the syntax is correct.
>>
>
> I'd be surprised if the first one is correct, because that "collect(_ * _)"
> would seem highly limited (how would you use the same value twice, or use
> just the second value, or use them in reverse order?).

I guess for anything more complicated you would have to use the => syntax. BTW, I just verified that this works:

Array(1, 2, 3, 4, 5).sortWith(_ > _)

You can try it out for yourself at: http://www.simplyscala.com/

> The second one reminds me of C#'s lambdas and seems like an excellent
> solution to the question of what to do with all the noise of the extra curly
> braces, "return" keyword and trailing semicolon. And given that 1. The =>
> isn't currently a valid D token anyway, and 2. The (x) and (x,y) are already
> being used perfectly fine by the existing delegate literal syntax, I can't
> imagine there would be any ambiguity with it in D.
>
> I'd be absolutely in favor of adding that to D. In fact, I'd love to see it
> happen. Just require that the part after "=>" is an expression, accept that
> the more complex delegates that need statements use the existing syntax
> (which should be fine since it's only the single-expression-and-nothing-else
> delegates that are seen as verbose in D), and call it a day.

I would also love to have that syntax. I also love the syntax I previously suggested in this thread, there are many to choose of.

-- 
/Jacob Carlborg
December 15, 2010
"piotrek" <starpit@tlen.pl> wrote in message news:ieb3ik$1ri8$1@digitalmars.com...
> On Wed, 15 Dec 2010 02:53:40 -0500, Nick Sabalausky wrote:
>
>> "lurker" <lurk@lurk.net> wrote in message news:ie8rc3$27l1$1@digitalmars.com...
>>> Nick Sabalausky Wrote:
>>>
>>>> "piotrek" <starpit@tlen.pl> wrote in message news:ie8fu9$ejl$1@digitalmars.com...
>>>> > On Mon, 13 Dec 2010 09:49:50 -0600, Andrei Alexandrescu wrote:
>>>> >> By the way, I couldn't stop cringing at the distasteful, male-centric sexual jokes that the talk is peppered with. Wonder if there was any woman in the audience, and how she might have felt. And this is not a ghetto rant - it's the keynote of a major Ruby conference! (And I'm definitely not a prude.) Am I alone in thinking that this is not what our metier should evolve into?
>>>> >>
>>>> >>
>>>> >> Andrei
>>>> >
>>>> > You're definitely not. No matter how strong pressure is, I stand and fight against common acceptance for female humiliation. It's so sad to me how many think they're real men while treating women as a things they can freely abuse.
>>>> >
>>>> >
>>>> Didja hear the one about the blonde who couldn't find her pencil?
>>>
>>> I have a bad sense of humor, but what is this if I'm trolling?
>>
>> Heh. It's me being an ass :)
>>
>> But seriously, I'm not flaming anyone here, and really taking a round-about way to point out that there's too much "inventing things to be offended about" going on in the world. I still haven't looked at the video, so maybe some of the things really were worse than the example Andrei mentioned or the joke I referenced above, but *if* these things are the sorts of things that piotrek and Andrei find "offensive to women", then I'd have to call a big "bullshit, this is just inventing blatant assumptions and then getting offended by them".
>
> Firstly I apologize Andrei if I put him in bad light after hijacking his
> sub-thread
> and then using it to add some personal opinion. Secondly I was referring
> to
> bigger picture of society and not only this video. And still I don't agree
> to
> "tolerate"* behavior which is based on so called "freedom". Because all
> you do
> has its consequence and interacts with "freedom" of other people and vice
> versa. Of course appropriate action should be used not exaggerated ones.
> And I had a peaceful intercommunication on my mind when speaking about
> fighting ;) I can say as a Christian that respect for dignity goes along
> with love.
> But your choice. And in the world the weak ones are abused all the time.
> I couldn't live pretending I don't see it. So one way is to give my
> opinion. Not
> don't giving a shit.
>

Yea, it's entirely possible that we have completely different sorts of things in mind when you're talking about "offensive" and when I'm talking about "not offensive".

I'll admit I was being a bit of an ass by referencing that joke as an attempt to guage whether or not it was the sort of thing you would find offensive. Should have just come out and asked "Is this the sort of thing you mean?" Feel free not to answer that though, I don't think either of us are feeling particularly inclined to end up in a heated debate about it today.



December 15, 2010
Wed, 15 Dec 2010 22:23:35 +0100, Jacob Carlborg wrote:

> On 2010-12-14 22:04, Nick Sabalausky wrote:
>> "Jacob Carlborg"<doob@me.com>  wrote in message news:ie8f5f$o6e$1@digitalmars.com...
>>>
>>> Probably not, but, for example, Scala allows very compact delegate literals:
>>>
>>> Array(1, 2, 3, 4, 5).select(_>  3).collect(_ * _)
>>>
>>> Or more verbose:
>>>
>>> Array(1, 2, 3, 4, 5).select((x) =>  x>  3).collect((x, y) =>  x * y)
>>>
>>> I'm not 100% sure I that the syntax is correct.
>>>
>>>
>> I'd be surprised if the first one is correct, because that "collect(_ * _)" would seem highly limited (how would you use the same value twice, or use just the second value, or use them in reverse order?).
> 
> I guess for anything more complicated you would have to use the => syntax. BTW, I just verified that this works:
> 
> Array(1, 2, 3, 4, 5).sortWith(_ > _)

The first instance of _ (from left to right) is replaced with the first element of the parameter tuple, the second with second element, etc.

This is actually very useful since many lambdas only use 1-2 parameters. It has its limitations. For example referring to the same parameter requires a named parameter or some other hack. Combined with Haskell style partial application this allows stuff like:

Array(1, 2, 3, 4, 5).foreach { println }

Array(1, 2, 3, 4, 5).filter(2 <)
December 15, 2010
On 12/15/10 4:18 PM, retard wrote:
> Wed, 15 Dec 2010 22:23:35 +0100, Jacob Carlborg wrote:
>> Array(1, 2, 3, 4, 5).sortWith(_>  _)
>
> The first instance of _ (from left to right) is replaced with the first
> element of the parameter tuple, the second with second element, etc.
>
> This is actually very useful since many lambdas only use 1-2 parameters.
> It has its limitations. For example referring to the same parameter
> requires a named parameter or some other hack. Combined with Haskell
> style partial application this allows stuff like:
>
> Array(1, 2, 3, 4, 5).foreach { println }
>
> Array(1, 2, 3, 4, 5).filter(2<)

For short lambdas I prefer Phobos' convention of using "a" and "b", e.g. "2 < a" or "a < b". Since it's a string, "_ < _" would have been usable with Phobos too but I wouldn't like such a change.

Andrei
December 15, 2010
Wed, 15 Dec 2010 16:33:43 -0600, Andrei Alexandrescu wrote:

> On 12/15/10 4:18 PM, retard wrote:
>> Wed, 15 Dec 2010 22:23:35 +0100, Jacob Carlborg wrote:
>>> Array(1, 2, 3, 4, 5).sortWith(_>  _)
>>
>> The first instance of _ (from left to right) is replaced with the first element of the parameter tuple, the second with second element, etc.
>>
>> This is actually very useful since many lambdas only use 1-2 parameters. It has its limitations. For example referring to the same parameter requires a named parameter or some other hack. Combined with Haskell style partial application this allows stuff like:
>>
>> Array(1, 2, 3, 4, 5).foreach { println }
>>
>> Array(1, 2, 3, 4, 5).filter(2<)
> 
> For short lambdas I prefer Phobos' convention of using "a" and "b", e.g. "2 < a" or "a < b". Since it's a string, "_ < _" would have been usable with Phobos too but I wouldn't like such a change.

I haven't have time to test this (still using D1 + Tango), but these magical 'a' and 'b' make me wonder whether there are any namespace issues. Can you refer to symbols defined in the current module or in the Phobos module the collection function is declared? Do the 'a' and 'b' shadow some other instances of 'a' and 'b'?