Subject: | Add a map() method that applies a block or sub to all elements |
As it stands, it's dangerous to pass junctions to subroutines because
most subroutines don't expect junctions and can fail in mysterious ways
if the subroutine only uses equality operators (which appear to work).
An example on p5 had an is_prime test with a nested junction:
Show quoted text
> sub is_prime {
> my ($n) = @_;
>
> return $n == 2 || $n % all(2..$n-1) != 0;
> }
This fails for is_prime(any(1,2,3)) because the "$n == 2" equality tests
short-circuits against any(1,2,3).
I propose adding a map() method for generating a new junction with a
transformation to every value of a junction. Effectively, turn this:
any( map { is_prime($_) } any(1,2,3)->values )
Into this:
any(1,2,3)->map( \&is_prime )
It must return a new junction, it should *not* modify the original.
Also note that this is a generic form of what would happen by
overloading arithmetic operators:
any(1,2,3)->map( sub{ $_[0] * 8 } )
Conceptually they are the same, but this provides a more general tool.
If you want to be fancy about it, localize $_ to the argument, so this
works, too, which people might expect if you call it "map":
any(1,2,3)->map( sub{ $_ * 8 } )
Thank you,
David