3v4l.org

run code in 300+ PHP versions simultaneously
<?php function curry(\Closure $f) { $rf = new \ReflectionFunction($f); $arity = $rf->getNumberOfParameters(); function acc($f, $arity, $args=[]) { return function(...$acc) use($f, $arity, $args) { $acc = array_merge($args, $acc); return (count($acc) >= $arity) ? $f(...$acc) : acc($f, $arity, $acc); }; }; return acc($f, $arity); }; // ================================================== $sum_func = function($a,$b,$c) { return $a+$b+$c; }; $cur = curry($sum_func); var_dump($cur(1,2,3)); var_dump($cur(1,2)(3)); var_dump($cur(1)(2)(3)); var_dump($cur()(1)()(2)()(3)); var_dump($cur()()()()(1)()(2)()(3,4,5));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VvE0q
function name:  (null)
number of ops:  81
compiled vars:  !0 = $sum_func, !1 = $cur
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   DECLARE_LAMBDA_FUNCTION                          ~2      [0]
          1        ASSIGN                                                   !0, ~2
   22     2        INIT_FCALL                                               'curry'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $4      
          5        ASSIGN                                                   !1, $4
   24     6        INIT_FCALL                                               'var_dump'
          7        INIT_DYNAMIC_CALL                                        !1
          8        SEND_VAL_EX                                              1
          9        SEND_VAL_EX                                              2
         10        SEND_VAL_EX                                              3
         11        DO_FCALL                                      0  $6      
         12        SEND_VAR                                                 $6
         13        DO_ICALL                                                 
   25    14        INIT_FCALL                                               'var_dump'
         15        INIT_DYNAMIC_CALL                                        !1
         16        SEND_VAL_EX                                              1
         17        SEND_VAL_EX                                              2
         18        DO_FCALL                                      0  $8      
         19        INIT_DYNAMIC_CALL                                        $8
         20        SEND_VAL_EX                                              3
         21        DO_FCALL                                      0  $9      
         22        SEND_VAR                                                 $9
         23        DO_ICALL                                                 
   26    24        INIT_FCALL                                               'var_dump'
         25        INIT_DYNAMIC_CALL                                        !1
         26        SEND_VAL_EX                                              1
         27        DO_FCALL                                      0  $11     
         28        INIT_DYNAMIC_CALL                                        $11
         29        SEND_VAL_EX                                              2
         30        DO_FCALL                                      0  $12     
         31        INIT_DYNAMIC_CALL                                        $12
         32        SEND_VAL_EX                                              3
         33        DO_FCALL                                      0  $13     
         34        SEND_VAR                                                 $13
         35        DO_ICALL                                                 
   27    36        INIT_FCALL                                               'var_dump'
         37        INIT_DYNAMIC_CALL                                        !1
         38        DO_FCALL                                      0  $15     
         39        INIT_DYNAMIC_CALL                                        $15
         40        SEND_VAL_EX                                              1
         41        DO_FCALL                                      0  $16     
         42        INIT_DYNAMIC_CALL                                        $16
         43        DO_FCALL                                      0  $17     
         44        INIT_DYNAMIC_CALL                                        $17
         45        SEND_VAL_EX                                              2
         46        DO_FCALL                                      0  $18     
         47        INIT_DYNAMIC_CALL                                        $18
         48        DO_FCALL                                      0  $19     
         49        INIT_DYNAMIC_CALL                                        $19
         50        SEND_VAL_EX                                              3
         51        DO_FCALL                                      0  $20     
         52        SEND_VAR                                                 $20
         53        DO_ICALL                                                 
   28    54        INIT_FCALL                                               'var_dump'
         55        INIT_DYNAMIC_CALL                                        !1
         56        DO_FCALL                                      0  $22     
         57        INIT_DYNAMIC_CALL                                        $22
         58        DO_FCALL                                      0  $23     
         59        INIT_DYNAMIC_CALL                                        $23
         60        DO_FCALL                                      0  $24     
         61        INIT_DYNAMIC_CALL                                        $24
         62        DO_FCALL                                      0  $25     
         63        INIT_DYNAMIC_CALL                                        $25
         64        SEND_VAL_EX                                              1
         65        DO_FCALL                                      0  $26     
         66        INIT_DYNAMIC_CALL                                        $26
         67        DO_FCALL                                      0  $27     
         68        INIT_DYNAMIC_CALL                                        $27
         69        SEND_VAL_EX                                              2
         70        DO_FCALL                                      0  $28     
         71        INIT_DYNAMIC_CALL                                        $28
         72        DO_FCALL                                      0  $29     
         73        INIT_DYNAMIC_CALL                                        $29
         74        SEND_VAL_EX                                              3
         75        SEND_VAL_EX                                              4
         76        SEND_VAL_EX                                              5
         77        DO_FCALL                                      0  $30     
         78        SEND_VAR                                                 $30
         79        DO_ICALL                                                 
         80      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VvE0q
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $a, !1 = $b, !2 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   20     3        ADD                                              ~3      !0, !1
          4        ADD                                              ~4      ~3, !2
          5      > RETURN                                                   ~4
   21     6*     > RETURN                                                   null

End of Dynamic Function 0

Function curry:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VvE0q
function name:  curry
number of ops:  15
compiled vars:  !0 = $f, !1 = $rf, !2 = $arity
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        NEW                                              $3      'ReflectionFunction'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $3
    5     5        INIT_METHOD_CALL                                         !1, 'getNumberOfParameters'
          6        DO_FCALL                                      0  $6      
          7        ASSIGN                                                   !2, $6
    7     8        DECLARE_FUNCTION                                         'acc'
   14     9        INIT_FCALL_BY_NAME                                       'acc'
         10        SEND_VAR_EX                                              !0
         11        SEND_VAR_EX                                              !2
         12        DO_FCALL                                      0  $8      
         13      > RETURN                                                   $8
   15    14*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VvE0q
function name:  acc
number of ops:  9
compiled vars:  !0 = $f, !1 = $arity, !2 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <array>
    8     3        DECLARE_LAMBDA_FUNCTION                          ~3      [0]
          4        BIND_LEXICAL                                             ~3, !0
          5        BIND_LEXICAL                                             ~3, !1
          6        BIND_LEXICAL                                             ~3, !2
   11     7      > RETURN                                                   ~3
   12     8*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 18
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VvE0q
function name:  {closure}
number of ops:  26
compiled vars:  !0 = $acc, !1 = $f, !2 = $arity, !3 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV_VARIADIC                                    !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
    9     4        INIT_FCALL                                               'array_merge'
          5        SEND_VAR                                                 !3
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $4      
          8        ASSIGN                                                   !0, $4
   10     9        COUNT                                            ~6      !0
         10        IS_SMALLER_OR_EQUAL                                      !2, ~6
         11      > JMPZ                                                     ~7, ->18
         12    >   INIT_DYNAMIC_CALL                                        !1
         13        SEND_UNPACK                                              !0
         14        CHECK_UNDEF_ARGS                                         
         15        DO_FCALL                                      1  $8      
         16        QM_ASSIGN                                        ~9      $8
         17      > JMP                                                      ->24
         18    >   INIT_FCALL_BY_NAME                                       'acc'
         19        SEND_VAR_EX                                              !1
         20        SEND_VAR_EX                                              !2
         21        SEND_VAR_EX                                              !0
         22        DO_FCALL                                      0  $10     
         23        QM_ASSIGN                                        ~9      $10
         24    > > RETURN                                                   ~9
   11    25*     > RETURN                                                   null

End of Dynamic Function 0

End of Dynamic Function 0

End of function curry

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
217.48 ms | 1016 KiB | 16 Q