lambda-dev Digest, Vol 15, Issue 20 [reduce method result type]

Seamus Sullivan ssullivan at aptima.com
Thu Mar 10 06:48:34 PST 2011


The use of fold in functional programming makes perfect sense, as they tend
to support tail-call recursion - which has some rather nice performance
benefits. 

To get back on point a bit, the key element of fold in ML (I'm sure it's
just as true in other fp languages) is that of the accumulation function
performed within the fold - which allows a type transformation to take place
- what do you gain by limiting the return type of the binary function?  By
limiting the return type of the accumulator, the expressiveness of fold is
limited, so there is a very definite loss that happens.

I have used the accumulate template function in C++'s standard library to
perform processing on collections of relatively complex objects that
returned relatively simple results: computing the average quality assessment
of a collection containing complex data comes to mind - my point is that the
transformation from a class of some sort to a primitive type was relatively
common inside the fold.  Yes, this could have been done in two steps -
performing a map and then a simply-typed fold/reduce, but why split this
into two iterative operations when it seems natural to think of it as one
collective action performed within some sort of loop?  I would find it odd
to have to iterate twice over every collection I wanted to run a fold
operation on.

Seamus

-----Original Message-----
From: lambda-dev-bounces at openjdk.java.net
[mailto:lambda-dev-bounces at openjdk.java.net] On Behalf Of Lawrence Kesteloot
Sent: Tuesday, March 08, 2011 3:38 AM
To: lambda-dev at openjdk.java.net
Subject: Re: lambda-dev Digest, Vol 15, Issue 20 [reduce method result type]

On Mon, Mar 7, 2011 at 6:30 PM, Neal Gafter <neal at gafter.com> wrote:
> It would be a shame to ignore [the functional programming community's]
experience
> and repeat mistakes of the distant past.

Their mistake of the distant past was to use fold-left in the first place.

> for some simple examples, <Matt Malone's blog post "Lots And Lots Of
> foldLeft
Examples"<http://oldfashionedsoftware.com/2009/07/30/lots-and-lots-of-foldle
ft-examples/>

I assert that every single one of those examples would be more clearly
(if more verbosely) expressed as a for-loop. (I tried this myself and
it's striking how much clearer the for-loop is, and often more
efficient.) I can genuinely see why computer science theorists love
fold-left, but it doesn't belong in any code that will later be read
by a human. Java should follow the example of Python 3000 and remove
fold-left from the API.

> Being composed of experts, I expect that the Project Lambda JSR expert
group
> has members who are well versed in these techniques.

I'm hoping for exactly the same thing.

Lawrence



The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.



More information about the lambda-dev mailing list