Thread overview
[Issue 8755] New: Change the order of reduce arguments
Mar 22, 2013
Denis Shelomovskij
Sep 27, 2013
Peter Alexander
October 04, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8755

           Summary: Change the order of reduce arguments
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: adamsibson@hotmail.com


--- Comment #0 from adamsibson@hotmail.com 2012-10-04 09:03:45 PDT ---
Reduce cannot have a seed applied elegantly when used with UFCS due to the argument order being range, seed. A newly named version with the argument order reversed would allow the use of the full range of reduce's capabilities with UFCS arguments.

Suggestion:

auto fold(range, seed) {}

to allow:

[1,2,3,4,5].fold!((a, b) => a + b * b)(0).writeln;

With the possibility of a longer chain in place of the array argument, a normal use with UFCS. At present only a few uses such as sum, max and min are usable with the unseeded form, limiting the usefulness of reduce.

Giving it a default of "a + b" would also improve its use as summing is probably the most common use case.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 04, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8755


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #1 from bearophile_hugs@eml.cc 2012-10-04 09:39:15 PDT ---
(In reply to comment #0)

> auto fold(range, seed) {}

Good.


> Giving it a default of "a + b" would also improve its use as summing is probably the most common use case.

This is not a good idea. Invisible defaults are magic, and magic is bad.

It's better to introduce an optimized sum() function, as in Haskell:

Prelude> sum [1,2,3]
6

And Python:

>>> sum([1,2,3])
6

A sum() function needs to work with fixed sized arrays too (like reduce/fold),
and it needs a specialization for short fixed-sized arrays, so this code:

int[4] a;
auto b = sum(a);

gets compiled about as:

int[4] a;
auto b = a[0] + a[1] + a[2] + a[3];

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 04, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8755



--- Comment #2 from adamsibson@hotmail.com 2012-10-04 10:03:04 PDT ---
> This is not a good idea. Invisible defaults are magic, and magic is bad.

Why is it any different to sort being "a < b" by default? Should we require that sort is always sort!"a < b"?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 04, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8755



--- Comment #3 from bearophile_hugs@eml.cc 2012-10-04 12:26:41 PDT ---
(In reply to comment #2)

> Why is it any different to sort being "a < b" by default? Should we require that sort is always sort!"a < b"?

It's different because it's widely accepted that "just sorting" a sequence returns it ordered from the min value. In Python, Haskell, Ruby and several other languages "just sort" has such definite meaning.

But I don't know of any language where reduce/fold has a default function that sums.

When people not expert of D look at code like this, they understand its meaning:

auto data = [3, 2, 1];
data.sort();
writeln(data.sum());


But when they see this, they can't know/see what this reduce is doing:

auto data = [3, 2, 1];
writeln(data.reduce());


Summing items of an iterable is a very common operation, and having a
specialized function (as in Python, Haskell, and other languages, even in
Fortran) with a short clear name as sum() is good.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 22, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=8755


Denis Shelomovskij <verylonglogin.reg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |verylonglogin.reg@gmail.com


--- Comment #4 from Denis Shelomovskij <verylonglogin.reg@gmail.com> 2013-03-22 10:00:19 MSK ---
*** Issue 9687 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 22, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=8755


monarchdodra@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra@gmail.com


--- Comment #5 from monarchdodra@gmail.com 2013-03-22 01:26:34 PDT ---
(In reply to comment #4)
> *** Issue 9687 has been marked as a duplicate of this issue. ***

For reference: http://forum.dlang.org/thread/jorsc4$kvo$1@digitalmars.com

As for the fix, which was submitted 5 months ago: https://github.com/D-Programming-Language/phobos/pull/861

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 27, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=8755


Peter Alexander <peter.alexander.au@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |daniel350@bigpond.com


--- Comment #6 from Peter Alexander <peter.alexander.au@gmail.com> 2013-09-27 14:43:37 PDT ---
*** Issue 11128 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------