3v4l.org

run code in 300+ PHP versions simultaneously
<?php class SinglePlaceholder { } class VariadicPlaceholder { } $PLACEHOLDER = new SinglePlaceholder(); $VARIADIC_PLACEHOLDER = new VariadicPlaceholder(); function partial(callable $in, ...$args) { $fmap = []; $partialIndex = 0; $variadic = false; foreach ($args as $k => $arg) { if ($arg instanceof SinglePlaceholder) { $fmap[$partialIndex++] = $k; } if ($arg instanceof VariadicPlaceholder) { $variadic = true; if ($k !== count($args) - 1) { throw new ArgumentCountError('Only the last parameter may be a variadic placeholder'); } $fmap[$partialIndex++] = $k; } } return static function (...$partialArgs) use ($in, $args, $fmap, $variadic) { $expectedCount = count($fmap); if ($variadic) { $expectedCount--; } if (count($partialArgs) < $expectedCount) { throw new ArgumentCountError('Expected ' . count($fmap) . ' only received ' . count($partialArgs)); } foreach ($partialArgs as $pIndex => $pValue) { $targetSlot = $fmap[$pIndex] ?? null; if ($targetSlot === null) { if ($variadic) { $args[] = $pValue; } else { throw new ArgumentCountError('Too many arguments without a variadic placeholder'); } } else { $args[$targetSlot] = $pValue; } } return $in(...$args); }; } function foo($a, $b, $c, $d, $e) { var_dump($a, $b, $c, $d, $e); } print_r(partial('foo', 1, $PLACEHOLDER, 3, $PLACEHOLDER, 5)(2, 4)); print_r(partial('foo', $VARIADIC_PLACEHOLDER)('a', 'b', 'c', 'd', 'e')); print_r(partial('foo', 1, 2, 3, 4, 5)()); $chain = partial('foo', $VARIADIC_PLACEHOLDER); $chain = partial($chain, 1, $VARIADIC_PLACEHOLDER); $chain = partial($chain, 2, $VARIADIC_PLACEHOLDER); $chain = partial($chain, 3, $VARIADIC_PLACEHOLDER); $chain = partial($chain, 4, $VARIADIC_PLACEHOLDER); $chain = partial($chain, 5, $VARIADIC_PLACEHOLDER); print_r($chain());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QiBk4
function name:  (null)
number of ops:  89
compiled vars:  !0 = $PLACEHOLDER, !1 = $VARIADIC_PLACEHOLDER, !2 = $chain
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   NEW                                              $3      'SinglePlaceholder'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $3
   12     3        NEW                                              $6      'VariadicPlaceholder'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !1, $6
   67     6        INIT_FCALL                                               'print_r'
          7        INIT_FCALL                                               'partial'
          8        SEND_VAL                                                 'foo'
          9        SEND_VAL                                                 1
         10        SEND_VAR                                                 !0
         11        SEND_VAL                                                 3
         12        SEND_VAR                                                 !0
         13        SEND_VAL                                                 5
         14        DO_FCALL                                      0  $9      
         15        INIT_DYNAMIC_CALL                                        $9
         16        SEND_VAL_EX                                              2
         17        SEND_VAL_EX                                              4
         18        DO_FCALL                                      0  $10     
         19        SEND_VAR                                                 $10
         20        DO_ICALL                                                 
   68    21        INIT_FCALL                                               'print_r'
         22        INIT_FCALL                                               'partial'
         23        SEND_VAL                                                 'foo'
         24        SEND_VAR                                                 !1
         25        DO_FCALL                                      0  $12     
         26        INIT_DYNAMIC_CALL                                        $12
         27        SEND_VAL_EX                                              'a'
         28        SEND_VAL_EX                                              'b'
         29        SEND_VAL_EX                                              'c'
         30        SEND_VAL_EX                                              'd'
         31        SEND_VAL_EX                                              'e'
         32        DO_FCALL                                      0  $13     
         33        SEND_VAR                                                 $13
         34        DO_ICALL                                                 
   69    35        INIT_FCALL                                               'print_r'
         36        INIT_FCALL                                               'partial'
         37        SEND_VAL                                                 'foo'
         38        SEND_VAL                                                 1
         39        SEND_VAL                                                 2
         40        SEND_VAL                                                 3
         41        SEND_VAL                                                 4
         42        SEND_VAL                                                 5
         43        DO_FCALL                                      0  $15     
         44        INIT_DYNAMIC_CALL                                        $15
         45        DO_FCALL                                      0  $16     
         46        SEND_VAR                                                 $16
         47        DO_ICALL                                                 
   71    48        INIT_FCALL                                               'partial'
         49        SEND_VAL                                                 'foo'
         50        SEND_VAR                                                 !1
         51        DO_FCALL                                      0  $18     
         52        ASSIGN                                                   !2, $18
   72    53        INIT_FCALL                                               'partial'
         54        SEND_VAR                                                 !2
         55        SEND_VAL                                                 1
         56        SEND_VAR                                                 !1
         57        DO_FCALL                                      0  $20     
         58        ASSIGN                                                   !2, $20
   73    59        INIT_FCALL                                               'partial'
         60        SEND_VAR                                                 !2
         61        SEND_VAL                                                 2
         62        SEND_VAR                                                 !1
         63        DO_FCALL                                      0  $22     
         64        ASSIGN                                                   !2, $22
   74    65        INIT_FCALL                                               'partial'
         66        SEND_VAR                                                 !2
         67        SEND_VAL                                                 3
         68        SEND_VAR                                                 !1
         69        DO_FCALL                                      0  $24     
         70        ASSIGN                                                   !2, $24
   75    71        INIT_FCALL                                               'partial'
         72        SEND_VAR                                                 !2
         73        SEND_VAL                                                 4
         74        SEND_VAR                                                 !1
         75        DO_FCALL                                      0  $26     
         76        ASSIGN                                                   !2, $26
   76    77        INIT_FCALL                                               'partial'
         78        SEND_VAR                                                 !2
         79        SEND_VAL                                                 5
         80        SEND_VAR                                                 !1
         81        DO_FCALL                                      0  $28     
         82        ASSIGN                                                   !2, $28
   77    83        INIT_FCALL                                               'print_r'
         84        INIT_DYNAMIC_CALL                                        !2
         85        DO_FCALL                                      0  $30     
         86        SEND_VAR                                                 $30
         87        DO_ICALL                                                 
         88      > RETURN                                                   1

Function partial:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 28
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 28
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 13
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 27
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 24
Branch analysis from position: 20
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 27
Branch analysis from position: 13
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
filename:       /in/QiBk4
function name:  partial
number of ops:  36
compiled vars:  !0 = $in, !1 = $args, !2 = $fmap, !3 = $partialIndex, !4 = $variadic, !5 = $arg, !6 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV_VARIADIC                                    !1      
   15     2        ASSIGN                                                   !2, <array>
   16     3        ASSIGN                                                   !3, 0
   17     4        ASSIGN                                                   !4, <false>
   19     5      > FE_RESET_R                                       $10     !1, ->28
          6    > > FE_FETCH_R                                       ~11     $10, !5, ->28
          7    >   ASSIGN                                                   !6, ~11
   20     8        INSTANCEOF                                               !5, 'SinglePlaceholder'
          9      > JMPZ                                                     ~13, ->13
   21    10    >   POST_INC                                         ~14     !3
         11        ASSIGN_DIM                                               !2, ~14
         12        OP_DATA                                                  !6
   24    13    >   INSTANCEOF                                               !5, 'VariadicPlaceholder'
         14      > JMPZ                                                     ~16, ->27
   25    15    >   ASSIGN                                                   !4, <true>
   26    16        COUNT                                            ~18     !1
         17        SUB                                              ~19     ~18, 1
         18        IS_NOT_IDENTICAL                                         !6, ~19
         19      > JMPZ                                                     ~20, ->24
   27    20    >   NEW                                              $21     'ArgumentCountError'
         21        SEND_VAL_EX                                              'Only+the+last+parameter+may+be+a+variadic+placeholder'
         22        DO_FCALL                                      0          
         23      > THROW                                         0          $21
   30    24    >   POST_INC                                         ~23     !3
         25        ASSIGN_DIM                                               !2, ~23
         26        OP_DATA                                                  !6
   19    27    > > JMP                                                      ->6
         28    >   FE_FREE                                                  $10
   34    29        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FQiBk4%3A34%240'
         30        BIND_LEXICAL                                             ~25, !0
         31        BIND_LEXICAL                                             ~25, !1
         32        BIND_LEXICAL                                             ~25, !2
         33        BIND_LEXICAL                                             ~25, !4
   60    34      > RETURN                                                   ~25
   61    35*     > RETURN                                                   null

End of function partial

Function %00%7Bclosure%7D%2Fin%2FQiBk4%3A34%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 21
Branch analysis from position: 12
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 77) Position 1 = 22, Position 2 = 42
Branch analysis from position: 22
2 jumps found. (Code = 78) Position 1 = 23, Position 2 = 42
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 39
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 34
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
Branch analysis from position: 34
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
Branch analysis from position: 9
filename:       /in/QiBk4
function name:  {closure}
number of ops:  49
compiled vars:  !0 = $partialArgs, !1 = $in, !2 = $args, !3 = $fmap, !4 = $variadic, !5 = $expectedCount, !6 = $pValue, !7 = $pIndex, !8 = $targetSlot
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV_VARIADIC                                    !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
          4        BIND_STATIC                                              !4
   35     5        COUNT                                            ~9      !3
          6        ASSIGN                                                   !5, ~9
   36     7      > JMPZ                                                     !4, ->9
   37     8    >   PRE_DEC                                                  !5
   40     9    >   COUNT                                            ~12     !0
         10        IS_SMALLER                                               ~12, !5
         11      > JMPZ                                                     ~13, ->21
   41    12    >   NEW                                              $14     'ArgumentCountError'
         13        COUNT                                            ~15     !3
         14        CONCAT                                           ~16     'Expected+', ~15
         15        CONCAT                                           ~17     ~16, '+only+received+'
         16        COUNT                                            ~18     !0
         17        CONCAT                                           ~19     ~17, ~18
         18        SEND_VAL_EX                                              ~19
         19        DO_FCALL                                      0          
         20      > THROW                                         0          $14
   44    21    > > FE_RESET_R                                       $21     !0, ->42
         22    > > FE_FETCH_R                                       ~22     $21, !6, ->42
         23    >   ASSIGN                                                   !7, ~22
   45    24        FETCH_DIM_IS                                     ~24     !3, !7
         25        COALESCE                                         ~25     ~24
         26        QM_ASSIGN                                        ~25     null
         27        ASSIGN                                                   !8, ~25
   46    28        TYPE_CHECK                                    2          !8
         29      > JMPZ                                                     ~27, ->39
   47    30    > > JMPZ                                                     !4, ->34
   48    31    >   ASSIGN_DIM                                               !2
         32        OP_DATA                                                  !6
         33      > JMP                                                      ->38
   51    34    >   NEW                                              $29     'ArgumentCountError'
         35        SEND_VAL_EX                                              'Too+many+arguments+without+a+variadic+placeholder'
         36        DO_FCALL                                      0          
         37      > THROW                                         0          $29
         38    > > JMP                                                      ->41
   55    39    >   ASSIGN_DIM                                               !2, !8
         40        OP_DATA                                                  !6
   44    41    > > JMP                                                      ->22
         42    >   FE_FREE                                                  $21
   59    43        INIT_DYNAMIC_CALL                                        !1
         44        SEND_UNPACK                                              !2
         45        CHECK_UNDEF_ARGS                                         
         46        DO_FCALL                                      1  $32     
         47      > RETURN                                                   $32
   60    48*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FQiBk4%3A34%240

Function foo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QiBk4
function name:  foo
number of ops:  13
compiled vars:  !0 = $a, !1 = $b, !2 = $c, !3 = $d, !4 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
          4        RECV                                             !4      
   64     5        INIT_FCALL                                               'var_dump'
          6        SEND_VAR                                                 !0
          7        SEND_VAR                                                 !1
          8        SEND_VAR                                                 !2
          9        SEND_VAR                                                 !3
         10        SEND_VAR                                                 !4
         11        DO_ICALL                                                 
   65    12      > RETURN                                                   null

End of function foo

Class SinglePlaceholder: [no user functions]
Class VariadicPlaceholder: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
131.17 ms | 1419 KiB | 26 Q