www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8755] New: Change the order of reduce arguments

reply d-bugmail puremagic.com writes:
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



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: -------
Oct 04 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8755


bearophile_hugs eml.cc changed:

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





 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: -------
Oct 04 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8755




 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: -------
Oct 04 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8755






 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: -------
Oct 04 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Mar 22 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8755


monarchdodra gmail.com changed:

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




 *** 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: -------
Mar 22 2013
prev sibling parent d-bugmail puremagic.com writes:
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



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: -------
Sep 27 2013