www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11084] New: std.algorithm.scan

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11084

           Summary: std.algorithm.scan
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



I suggest to add to Phobos a function that returns a range, with usage very
similar to std.algorithm.reduce, that returns all the intermediate values.

An example from Haskell:

Prelude> [1 .. 10]
[1,2,3,4,5,6,7,8,9,10]
Prelude> scanl (+) 0 [1 .. 10]
[0,1,3,6,10,15,21,28,36,45,55]
Prelude> scanr (+) 0 [1 .. 10]
[55,54,52,49,45,40,34,27,19,10,0]


That is also related to the FoldList of Mathematica:
http://reference.wolfram.com/mathematica/ref/FoldList.html


In D it could work like this:

iota(1, 11).scan!q{a + b}(0).writeln
==> 
[0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55]

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 21 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11084


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com



05:52:21 PDT ---
Intermediate? Can you be more specific? What exact steps does that scan!() call
make?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 21 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11084





 Intermediate? Can you be more specific? What exact steps does that scan!() call
 make?
The Haskell scanl is a very simple function, it acts very much like reduce, but instead of returning just the last result, it returns them all: scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...] Its whole Haskell implementation in the Haskell Prelude: scanl :: (a -> b -> a) -> a -> [b] -> [a] scanl f q ls = q : (case ls of [] -> [] x:xs -> scanl f (f q x) xs) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 21 2013