I needed a scan function for some data processing I am doing. Unfortunately Sub::Curried does not work, so I cannot use it's scan function.

Language::Functional has a broken test suite. And I can't figure out how to fix its issues.

fp does not have a scanl function. I considered contributing this code to it, but am turned off by the semantics of the reduce function... I think that would be better named behead. It also potentially conflicts with List::Util in that respect.

So here we have the scanl function, a staple of functional programming, available in standalone fashion. As you can see, it behaves the same as scanl for haskell

use strict; use warnings; use Data::Dumper; =for comment scanl f q ls = q : case ls of [] -> [] x:xs -> scanl f (f q x) xs =cut sub scanl { my ($f, $seed, @list)=@_; my @tail = @list[ 1 .. $#list ] ; my @rest = scalar @list ? scanl($f, $f->($seed, $list[0]), @tail) +: (); ($seed, @rest); } my @lis = (4,2,4); sub add { $_[0] + $_[1] } sub divide { $_[0] / $_[1] } my @result = scanl \&divide, 64, @lis; warn Dumper(\@result); @result = scanl \&divide, 3, (); warn Dumper(\@result); use List::Util 'max'; @result = scanl \&List::Util::max, 5, (1 .. 7) ; warn Dumper(\@result);

In reply to functional programming: scan function by metaperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.