Expected distribution of lambda sizes (Re: Syntax poll, take 2)
Ben Evans
benjamin.john.evans at gmail.com
Wed Jun 15 08:19:14 PDT 2011
Hi Maurizio,
On Wed, Jun 15, 2011 at 3:13 PM, Maurizio Cimadamore <
maurizio.cimadamore at oracle.com> wrote:
> On 15/06/11 14:22, Ben Evans wrote:
>
> On Tue, Jun 14, 2011 at 8:26 PM, Jack Moxley <jack at moxley.co.uk> <jack at moxley.co.uk> wrote:
>
>
> Whatever syntax is chosen remember that it WILL be abused, and some poor
> sod like me is going to have to find the issue and then teach the
> perpetrator as to why it was abuse.
>
> To sum up, expect the unexpected.
>
>
>
> This is precisely why we need a syntax containing a "Yoo-hoo, I'm a lambda"
> character.
>
> The syntax should favour the poor maint programmer - who is sooner or later
> going to be us when woken at 0300 and hungover.
>
> As we now seem to be debating alternatives (despite Brian's instructions),
> here's the blog post I wrote about this:
> http://www.java7developer.com/blog/?p=326
>
> Hi Ben,
> I went through the examples in your blog entry - I think that the syntax in
> your post looks way more verbose than it is in practice; the first problem
> is that your example seems not to be correct, as it uses type-variable
> declaration syntax in a use-site position; i.e. stuff like:
>
> .sort(#(Pair<T, V extends Comparable<T>> l)(l.get(1)))
>
>
> Is wrong, as 'V extends' cannot appear here. I think that what you want to
> achieve here is to push up constraints on V at the method declaration level,
> as follows:
>
> <T, V extends Comparable<T>> List<V> schwarz(List<T> x, Function<T,
> Pair<T,V>> f) {
> return map(#{T w -> f.apply(w) }, x)
> .sort(#{Pair<T, V> l -> l.fst()})
> .map(#{Pair<T, V> l -> l.snd()});
> }
>
> This is already way shorter than your version.
>
> Secondly, note that you can take advantage of type-inference for lambda
> parameters, which further reduces your examples to:
>
> <T, V extends Comparable<T>> List<V> schwarz(List<T> x, Function<T,
> Pair<T,V>> f) {
> return map(#{ w -> f.apply(w) }, x)
> .sort(#{ l -> l.fst()})
> .map(#{l -> l.snd()});
> }
>
> Or, with method references:
>
> <T, V extends Comparable<T>> List<V> schwarz(List<T> x, Function<T,
> Pair<T,V>> f) {
> return map(#{ w -> f.apply(w) }, x)
> .sort( Pair<T,V>#fst() )
> .map( Pair<T,V>#snd() );
> }
>
>
Thanks for the patch - I'll post an update - the type inference stuff is
really useful. I think that when I originally write the post I thought I
could see cases where type inference could be defeated, but on further
reflection I think I was deluded. :)
Ben
More information about the lambda-dev
mailing list