| 1 | # | ||||||||||
| 2 | # Copyright (c) 2019 Christian Jaeger, copying@christianjaeger.ch | ||||||||||
| 3 | # | ||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||
| 7 | # bundled with this file. | ||||||||||
| 8 | # | ||||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | FP::Combinators2 - more function combinators | ||||||||||
| 13 | |||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | use FP::Combinators2 ":all"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use FP::Array qw(array); use FP::Equal ":all"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | my $ra = right_associate_(\&array, 0); | ||||||||||
| 20 | is_equal $ra->(qw(a b c d)), | ||||||||||
| 21 | ['a', ['b', ['c', 'd']]]; | ||||||||||
| 22 | is_equal $ra->(qw(a b)), ['a', 'b']; | ||||||||||
| 23 | is_equal $ra->(qw(a)), 'a'; | ||||||||||
| 24 | is_equal $ra->(), 0; | ||||||||||
| 25 | |||||||||||
| 26 | my $la = left_associate_(\&array, 0); | ||||||||||
| 27 | is_equal $la->(qw(a b c d)), | ||||||||||
| 28 | [[['a', 'b'], 'c'], 'd']; | ||||||||||
| 29 | is_equal $la->(qw(a b)), ['a', 'b']; | ||||||||||
| 30 | is_equal $la->(qw(a)), 'a'; | ||||||||||
| 31 | is_equal $la->(), 0; | ||||||||||
| 32 | |||||||||||
| 33 | |||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||
| 35 | |||||||||||
| 36 | This is an extension of L<FP::Combinators> for functions that need | ||||||||||
| 37 | more dependencies and can't be put into the former because of circular | ||||||||||
| 38 | dependencies. | ||||||||||
| 39 | |||||||||||
| 40 | =head1 SEE ALSO | ||||||||||
| 41 | |||||||||||
| 42 | L<FP::Combinators> | ||||||||||
| 43 | |||||||||||
| 44 | =head1 NOTE | ||||||||||
| 45 | |||||||||||
| 46 | This is alpha software! Read the status section in the package README | ||||||||||
| 47 | or on the L<website|http://functional-perl.org/>. | ||||||||||
| 48 | |||||||||||
| 49 | =cut | ||||||||||
| 50 | |||||||||||
| 51 | package FP::Combinators2; | ||||||||||
| 52 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 53 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 54 | 2 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 55 | 2 | ▶ | use Exporter "import"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 56 | |||||||||||
| 57 | 2 | our @EXPORT = qw(); | |||||||||
| 58 | 2 | our @EXPORT_OK = qw( | |||||||||
| 59 | right_associate_ | ||||||||||
| 60 | left_associate_); | ||||||||||
| 61 | 2 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |||||||||
| 62 | |||||||||||
| 63 | #use Chj::TEST; | ||||||||||
| 64 | 2 | ▶ | use FP::PureArray; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 65 | 2 | ▶ | use FP::Combinators qw(flip); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 66 | 2 | ▶ | use FP::Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 67 | |||||||||||
| 68 | 5 | ▶ | sub right_associate_ { | ||||||||
|
Subroutine
right_associate_: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 69 | 5 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 5 | my ($op, $noop) = @_; | |||||||||
| 71 | 9 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 72 | @_ | ||||||||||
| 73 | 9 | ▶ | ? do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 7 | my $init = pop; | |||||||||
| 75 | 7 | purearray(@_)->fold_right($op, $init) | |||||||||
| 76 | } | ||||||||||
| 77 | : $noop | ||||||||||
| 78 | } | ||||||||||
| 79 | 5 | } | |||||||||
| 80 | |||||||||||
| 81 | 1 | ▶ | sub left_associate_ { | ||||||||
|
Subroutine
left_associate_: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 82 | 1 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 1 | my ($op, $noop) = @_; | |||||||||
| 84 | 1 | my $op2 = flip $op; | |||||||||
| 85 | 4 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 86 | @_ | ||||||||||
| 87 | 4 | ▶ | ? do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 3 | my $init = shift; | |||||||||
| 89 | 3 | purearray(@_)->fold($op2, $init) | |||||||||
| 90 | } | ||||||||||
| 91 | : $noop | ||||||||||
| 92 | } | ||||||||||
| 93 | 1 | } | |||||||||
| 94 | |||||||||||
| 95 | 2 | 1 | |||||||||