| 1 | # | ||||||||||
| 2 | # Copyright (c) 2013-2020 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::Currying | ||||||||||
| 13 | |||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | use FP::Currying; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | # see usage below | ||||||||||
| 18 | |||||||||||
| 19 | =head1 FUNCTIONS | ||||||||||
| 20 | |||||||||||
| 21 | =over 4 | ||||||||||
| 22 | |||||||||||
| 23 | =item curry(f)->(arg1)->(arg2) | ||||||||||
| 24 | |||||||||||
| 25 | Takes a function (coderef) f that takes 2 arguments, and returns a | ||||||||||
| 26 | function that takes just 1 argument, which when called returns a | ||||||||||
| 27 | function that takes again 1 argument and when called calls f with the | ||||||||||
| 28 | two separately passed arguments. | ||||||||||
| 29 | |||||||||||
| 30 | 1 | ▶ | use FP::Array 'array'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | |||||||||||
| 32 | is_deeply curry(\&array)->(10)->(20), | ||||||||||
| 33 | [ 10, 20 ]; | ||||||||||
| 34 | |||||||||||
| 35 | =item curry_(f, args..)->(args1..)->(args2..) | ||||||||||
| 36 | |||||||||||
| 37 | Same as curry but accepts multiple arguments in each step. | ||||||||||
| 38 | |||||||||||
| 39 | is_deeply curry_(\&array, 1)->(10, 11)->(20, 21, 23), | ||||||||||
| 40 | [ 1, 10, 11, 20, 21, 23 ]; | ||||||||||
| 41 | |||||||||||
| 42 | =item partial(f, args...)->(args1..) | ||||||||||
| 43 | |||||||||||
| 44 | Takes a function f and fewer than the normal arguments to f, and | ||||||||||
| 45 | returns a fn that takes a variable number of additional args. When | ||||||||||
| 46 | called, the returned function calls f with args + additional args. | ||||||||||
| 47 | |||||||||||
| 48 | (Same as curry_ but with only one step.) | ||||||||||
| 49 | |||||||||||
| 50 | is_deeply partial(\&array, "hi", 1)->(3, 9), | ||||||||||
| 51 | [ "hi", 1, 3, 9 ]; | ||||||||||
| 52 | |||||||||||
| 53 | =item uncurry, uncurry_1_1, uncurry_2_1, uncurry_1_2, uncurry_2_2, uncurry_1_1_1 | ||||||||||
| 54 | |||||||||||
| 55 | Sometimes it's easier to write code in a curried fashion. Often users | ||||||||||
| 56 | still expect to receive an uncurried ("normal") version of the | ||||||||||
| 57 | function. `uncurry_1_1 $fn` returns a function that expects 2 | ||||||||||
| 58 | arguments, passes the first to $fn and then the second to the function | ||||||||||
| 59 | that $fn returns. Other variants behave similarly: the appendix tells | ||||||||||
| 60 | how many arguments each function level expects; the added numbers | ||||||||||
| 61 | determine how many arguments the resulting function expects. | ||||||||||
| 62 | |||||||||||
| 63 | my $mult = uncurry sub { my ($x) = @_; sub { my ($y) = @_; $x*$y }}; | ||||||||||
| 64 | is &$mult(2,3), 6; | ||||||||||
| 65 | # 'uncurry' is an alias to 'uncurry_1_1'. | ||||||||||
| 66 | |||||||||||
| 67 | my $mult3 = uncurry_2_1 sub { my ($x,$y) = @_; sub { my ($z) = @_; $x*$y*$z }}; | ||||||||||
| 68 | is &$mult3(2,3,4), 24; | ||||||||||
| 69 | |||||||||||
| 70 | =back | ||||||||||
| 71 | |||||||||||
| 72 | |||||||||||
| 73 | =head1 TODO | ||||||||||
| 74 | |||||||||||
| 75 | Add tail-call optimization to the last call in the chain. Waiting till | ||||||||||
| 76 | Sub::Call::Tail is fixed, or better, we've got a switchable variant. | ||||||||||
| 77 | |||||||||||
| 78 | =head1 SEE ALSO | ||||||||||
| 79 | |||||||||||
| 80 | There are also various modules for currying (curry, not uncurry) on | ||||||||||
| 81 | CPAN. | ||||||||||
| 82 | |||||||||||
| 83 | `the_method` and `cut_method` in L<FP::Ops>. | ||||||||||
| 84 | |||||||||||
| 85 | =head1 NOTE | ||||||||||
| 86 | |||||||||||
| 87 | This is alpha software! Read the status section in the package README | ||||||||||
| 88 | or on the L<website|http://functional-perl.org/>. | ||||||||||
| 89 | |||||||||||
| 90 | =cut | ||||||||||
| 91 | |||||||||||
| 92 | package FP::Currying; | ||||||||||
| 93 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 94 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 95 | 2 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 96 | 2 | ▶ | use Exporter "import"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 97 | |||||||||||
| 98 | 2 | our @EXPORT = qw( | |||||||||
| 99 | curry | ||||||||||
| 100 | curry_ | ||||||||||
| 101 | partial | ||||||||||
| 102 | uncurry | ||||||||||
| 103 | uncurry_1_1 | ||||||||||
| 104 | uncurry_2_1 | ||||||||||
| 105 | uncurry_1_2 | ||||||||||
| 106 | uncurry_2_2 | ||||||||||
| 107 | uncurry_1_1_1 | ||||||||||
| 108 | ); | ||||||||||
| 109 | 2 | our @EXPORT_OK = qw(); | |||||||||
| 110 | 2 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |||||||||
| 111 | |||||||||||
| 112 | 2 | ▶ | use FP::Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 113 | |||||||||||
| 114 | 1 | ▶ | sub curry { | ||||||||
|
Subroutine
curry: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||
| 115 | 1 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | 1 | my ($f) = @_; | |||||||||
| 117 | 1 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||
| 118 | 1 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 1 | my ($a) = @_; | |||||||||
| 120 | 1 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||
| 121 | 1 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 1 | @_ = ($a, @_); | |||||||||
| 123 | 1 | goto \&$f | |||||||||
| 124 | } | ||||||||||
| 125 | 1 | } | |||||||||
| 126 | 1 | } | |||||||||
| 127 | |||||||||||
| 128 | # relaxed version | ||||||||||
| 129 | 1 | ▶ | sub curry_ { | ||||||||
|
Subroutine
curry_: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 130 | 1 | my ($f, @p) = @_; | |||||||||
| 131 | 1 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 132 | 1 | my @a = @_; | |||||||||
| 133 | 1 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 134 | 1 | @_ = (@p, @a, @_); | |||||||||
| 135 | 1 | goto \&$f | |||||||||
| 136 | } | ||||||||||
| 137 | 1 | } | |||||||||
| 138 | 1 | } | |||||||||
| 139 | |||||||||||
| 140 | # https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj | ||||||||||
| 141 | # "Takes a function f and fewer than the normal arguments to f, and | ||||||||||
| 142 | # returns a fn that takes a variable number of additional args. When | ||||||||||
| 143 | # called, the returned function calls f with args + additional args." | ||||||||||
| 144 | 1 | ▶ | sub partial { | ||||||||
|
Subroutine
partial: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 145 | 1 | my ($f, @p) = @_; | |||||||||
| 146 | 1 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 147 | 1 | @_ = (@p, @_); | |||||||||
| 148 | 1 | goto \&$f | |||||||||
| 149 | } | ||||||||||
| 150 | 1 | } | |||||||||
| 151 | |||||||||||
| 152 | # Macros would be useful here. | ||||||||||
| 153 | |||||||||||
| 154 | 1 | ▶ | sub uncurry_1_1 { | ||||||||
|
Subroutine
uncurry_1_1: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 155 | 1 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 156 | 1 | my ($f) = @_; | |||||||||
| 157 | 1 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||
| 158 | 1 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 159 | 1 | my ($a, $b) = @_; | |||||||||
| 160 | 1 | $f->($a)->($b) | |||||||||
| 161 | } | ||||||||||
| 162 | 1 | } | |||||||||
| 163 | |||||||||||
| 164 | sub uncurry; | ||||||||||
| 165 | 2 | *uncurry = \&uncurry_1_1; | |||||||||
| 166 | |||||||||||
| 167 | 1 | ▶ | sub uncurry_2_1 { | ||||||||
|
Subroutine
uncurry_2_1: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 168 | 1 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 169 | 1 | my ($f) = @_; | |||||||||
| 170 | 1 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||
| 171 | 1 | ▶ | @_ == 3 or fp_croak_arity 3; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 172 | 1 | my ($a, $b, $c) = @_; | |||||||||
| 173 | 1 | $f->($a, $b)->($c) | |||||||||
| 174 | } | ||||||||||
| 175 | 1 | } | |||||||||
| 176 | |||||||||||
| 177 | 0 | ▶ | sub uncurry_1_2 { | ||||||||
|
Subroutine
uncurry_1_2: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 178 | 0 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 179 | 0 | my ($f) = @_; | |||||||||
| 180 | 0 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 181 | 0 | ▶ | @_ == 3 or fp_croak_arity 3; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 182 | 0 | my ($a, $b, $c) = @_; | |||||||||
| 183 | 0 | $f->($a)->($b, $c) | |||||||||
| 184 | } | ||||||||||
| 185 | 0 | } | |||||||||
| 186 | |||||||||||
| 187 | 0 | ▶ | sub uncurry_2_2 { | ||||||||
|
Subroutine
uncurry_2_2: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 188 | 0 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 189 | 0 | my ($f) = @_; | |||||||||
| 190 | 0 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 191 | 0 | ▶ | @_ == 4 or fp_croak_arity 4; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 192 | 0 | my ($a, $b, $c, $d) = @_; | |||||||||
| 193 | 0 | $f->($a, $b)->($c, $d) | |||||||||
| 194 | } | ||||||||||
| 195 | 0 | } | |||||||||
| 196 | |||||||||||
| 197 | 0 | ▶ | sub uncurry_1_1_1 { | ||||||||
|
Subroutine
uncurry_1_1_1: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 198 | 0 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 199 | 0 | my ($f) = @_; | |||||||||
| 200 | 0 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 201 | 0 | ▶ | @_ == 3 or fp_croak_arity 3; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 202 | 0 | my ($a, $b, $c) = @_; | |||||||||
| 203 | 0 | $f->($a)->($b)->($c) | |||||||||
| 204 | } | ||||||||||
| 205 | 0 | } | |||||||||
| 206 | |||||||||||
| 207 | # ... | ||||||||||
| 208 | |||||||||||
| 209 | 2 | 1 | |||||||||