Thread overview
[Issue 11084] New: std.algorithm.scan
Sep 21, 2013
Andrej Mitrovic
September 21, 2013
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


--- Comment #0 from bearophile_hugs@eml.cc 2013-09-21 05:47:16 PDT ---
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: -------
September 21, 2013
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


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-09-21 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: -------
September 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11084



--- Comment #2 from bearophile_hugs@eml.cc 2013-09-21 06:04:43 PDT ---
(In reply to comment #1)
> 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: -------