Skip Menu |

This queue is for tickets about the Scalar-List-Utils CPAN distribution.

Report information
The Basics
Id: 128237
Status: resolved
Priority: 0/
Queue: Scalar-List-Utils

People
Owner: Nobody in particular
Requestors: rvtol [...] isolution.nl
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: 1.54



Subject: feature request: slide
Date: Sun, 13 Jan 2019 16:50:13 +0100
To: bug-Scalar-List-Utils [...] rt.cpan.org
From: "Ruud H.G. van Tol" <rvtol [...] isolution.nl>
sub slide (&@) { # like reduce, but returns intermediate values my $f = shift; my $v0= shift; @_ or return $v0; my $pkg= caller; my $a= $v0; no strict 'refs'; local *{"$pkg\::a"} = \$a; my $glob_b= \*{"$pkg\::b"}; $v0, map { my $b= $_; local *$glob_b= \$b; local $_; $a= $f->(); } @_; } Example: my $e= 1+ sum slide { $a / $b } 1..19; # 2.71828182845905 -- Ruud
On Sun Jan 13 10:50:26 2019, rvtol@isolution.nl wrote: Show quoted text
> > sub slide (&@) { > # like reduce, but returns intermediate values
Yes; I've on occasion wanted a function to do exactly this. But I wonder about the name - where does "slide" come from? A more transparent name might be sub reduce_with_intermediates though that is ugly and long. I agree with the behaviour, just not so sure about the name - can we find a better one? -- Paul Evans
Subject: Re: [rt.cpan.org #128237] feature request: slide
Date: Mon, 14 Jan 2019 10:22:19 +0100
To: bug-Scalar-List-Utils [...] rt.cpan.org
From: "Ruud H.G. van Tol" <rvtol [...] isolution.nl>
On 2019-01-14 00:03, Paul Evans via RT wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=128237 > > On Sun Jan 13 10:50:26 2019, rvtol@isolution.nl wrote:
Show quoted text
>> sub slide (&@) { >> # like reduce, but returns intermediate values
> > Yes; I've on occasion wanted a function to do exactly this. But I wonder about the name - where does "slide" come from?
It was inspired on a dance move. (Hip Hop, the Slide Step). Show quoted text
> A more transparent name might be > > sub reduce_with_intermediates > > though that is ugly and long. I agree with the behaviour, just not so sure about the name - can we find a better one?
We should be able to find a better one. :) In Clojure it is called 'reductions'. http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/reductions -- Ruud
I am liking the name "reductions". One question on behaviour: what is the first value it returns? Does it return the seed value as well, or just the first result of the block? I.e. my @x = reductions { $a + $b } 1 .. 5; Which does this return? (1, 3, 6, 10, 15) (3, 6, 10, 15) I sortof feel the former feels neater - it returns a list of the same size as that it was given. -- Paul Evans
Show quoted text
> Which does this return? > > (1, 3, 6, 10, 15) > (3, 6, 10, 15) > > I sortof feel the former feels neater - it returns a list of the same > size as that it was given.
Both Clojure's "reductions" and Haskell's "scan" seem to agree with this idea, so lets go with that. -- Paul Evans
Subject: Re: [rt.cpan.org #128237] Feature request: reductions
Date: Wed, 29 Jan 2020 16:15:28 +0100
To: bug-Scalar-List-Utils [...] rt.cpan.org
From: "Ruud H.G. van Tol" <rvtol [...] isolution.nl>
On 2020-01-29 16:03, Paul Evans via RT wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=128237 > > > I am liking the name "reductions". One question on behaviour: what is the first value it returns? Does it return the seed value as well, or just the first result of the block? > > I.e. > > my @x = reductions { $a + $b } 1 .. 5; > > Which does this return? > > (1, 3, 6, 10, 15) > (3, 6, 10, 15) > > I sortof feel the former feels neater - it returns a list of the same size as that it was given.
It would be less useful if it didn't. $ perl -Mstrict -wE' use My::List qw(reductions); my @x= reductions { $a * $b } 1 .. 5; say "@x"; ' 1 2 6 24 120 my $e= 1+ sum map { reduce { $a / $b } 1..$_ } 1..19; #e=2.71828182845905 -- Ruud sub reductions (&@) { # like reduce, but returns intermediate values my $f = shift; my $v0= shift; @_ or return $v0; my $pkg= caller; my $a= $v0; no strict 'refs'; local *{"$pkg\::a"} = \$a; my $glob_b= \*{"$pkg\::b"}; $v0, map { my $b= $_; local $_; local *$glob_b= \$b; $a= $f->(); } @_; }
I now have a branch in progress https://github.com/Dual-Life/Scalar-List-Utils/tree/leonerd/reductions Still needs docs and some more tests, but so far it shows promise: $ perl -MList::Util=reductions -E 'say for reductions { $a * $b } 1 .. 5' 1 2 6 24 120 $ perl -MList::Util=sum,reductions -E 'say 1 + sum reductions { $a / $b } 1 .. 19' 2.71828182845905 -- Paul Evans
Subject: Re: [rt.cpan.org #128237] Feature request: reductions
Date: Sat, 1 Feb 2020 12:01:05 +0100
To: bug-Scalar-List-Utils [...] rt.cpan.org
From: "Ruud H.G. van Tol" <rvtol [...] isolution.nl>
On 2020-01-31 16:42, Paul Evans via RT wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=128237 > > > I now have a branch in progress > > https://github.com/Dual-Life/Scalar-List-Utils/tree/leonerd/reductions > > Still needs docs and some more tests, but so far it shows promise: > > $ perl -MList::Util=reductions -E 'say for reductions { $a * $b } 1 .. 5' > 1 > 2 > 6 > 24 > 120 > > $ perl -MList::Util=sum,reductions -E 'say 1 + sum reductions { $a / $b } 1 .. 19' > 2.71828182845905
To compare: perl -MList::Util=reduce,sum -E' my @reductions; reduce { push @reductions, $a; $a / $b } 1..19; say for @reductions, "-"x20, 1 + sum( @reductions ); ' 1 0.5 0.166666666666667 0.0416666666666667 0.00833333333333333 0.00138888888888889 0.000198412698412698 2.48015873015873e-05 2.75573192239859e-06 2.75573192239859e-07 2.50521083854417e-08 2.08767569878681e-09 1.60590438368216e-10 1.14707455977297e-11 7.64716373181982e-13 4.77947733238739e-14 2.81145725434552e-15 1.56192069685862e-16 -------------------- 2.71828182845905 perl -MList::Util=reduce -E' my $sum= 0; reduce { $sum+= $a; $a / $b } 1..19; say 1 + $sum; ' 2.71828182845905 -- Ruud
This is now on CPAN as part of 1.54 -- Paul Evans