3v4l.org

run code in 300+ PHP versions simultaneously
<?php function chain($iterable, array $functions) { foreach ($functions as $function) { $iterable = $function(as_iterator($iterable)); } return $iterable; } function as_iterator($iterable) { if ($iterable instanceof \Iterator) { return $iterable; } if (is_array($iterable)) { return new \ArrayIterator($iterable); } if (is_string($iterable)) { return new \ArrayIterator(str_split($iterable)); } throw new \Exception(); } function do_map(callable $function) { return function(Iterator $iterator) use ($function) { foreach ($iterator as $key => $value) { yield $key => $function($value, $key); } }; } function do_filter(callable $function) { return function(Iterator $iterator) use($function) { foreach ($iterator as $key => $value) { if ($function($value, $key)) { yield $key => $value; } } }; } function do_concat($iterable) { return function(Iterator $iterator) use ($iterable) { foreach ($iterator as $value) { yield $value; } foreach (as_iterator($iterable) as $value) { yield $value; } }; } function do_as_array() { return function(Iterator $iterator) { return iterator_to_array($iterator); }; } $array = chain([1, 2, 3, 4], [ do_concat([5, 6, 7, 8]), do_filter(function ($v) { return $v % 2 == 0; }), do_map(function ($v) { return $v * 3; }), do_as_array(), ]); var_dump($array);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fmS6q
function name:  (null)
number of ops:  26
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   INIT_FCALL                                               'chain'
          1        SEND_VAL                                                 <array>
   65     2        INIT_FCALL                                               'do_concat'
          3        SEND_VAL                                                 <array>
          4        DO_FCALL                                      0  $1      
          5        INIT_ARRAY                                       ~2      $1
   66     6        INIT_FCALL                                               'do_filter'
          7        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FfmS6q%3A66%244'
          8        SEND_VAL                                                 ~3
          9        DO_FCALL                                      0  $4      
         10        ADD_ARRAY_ELEMENT                                ~2      $4
   67    11        INIT_FCALL                                               'do_map'
         12        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FfmS6q%3A67%245'
         13        SEND_VAL                                                 ~5
         14        DO_FCALL                                      0  $6      
         15        ADD_ARRAY_ELEMENT                                ~2      $6
   68    16        INIT_FCALL                                               'do_as_array'
         17        DO_FCALL                                      0  $7      
         18        ADD_ARRAY_ELEMENT                                ~2      $7
         19        SEND_VAL                                                 ~2
         20        DO_FCALL                                      0  $8      
   64    21        ASSIGN                                                   !0, $8
   70    22        INIT_FCALL                                               'var_dump'
         23        SEND_VAR                                                 !0
         24        DO_ICALL                                                 
         25      > RETURN                                                   1

Function chain:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 12
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 12
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/fmS6q
function name:  chain
number of ops:  15
compiled vars:  !0 = $iterable, !1 = $functions, !2 = $function
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2      > FE_RESET_R                                       $3      !1, ->12
          3    > > FE_FETCH_R                                               $3, !2, ->12
    6     4    >   INIT_DYNAMIC_CALL                                        !2
          5        INIT_FCALL_BY_NAME                                       'as_iterator'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0  $4      
          8        SEND_VAR_NO_REF_EX                                       $4
          9        DO_FCALL                                      0  $5      
         10        ASSIGN                                                   !0, $5
    5    11      > JMP                                                      ->3
         12    >   FE_FREE                                                  $3
    8    13      > RETURN                                                   !0
    9    14*     > RETURN                                                   null

End of function chain

Function as_iterator:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 10
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 19
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/fmS6q
function name:  as_iterator
number of ops:  23
compiled vars:  !0 = $iterable
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
   13     1        INSTANCEOF                                               !0, 'Iterator'
          2      > JMPZ                                                     ~1, ->4
   14     3    > > RETURN                                                   !0
   16     4    >   TYPE_CHECK                                  128          !0
          5      > JMPZ                                                     ~2, ->10
   17     6    >   NEW                                              $3      'ArrayIterator'
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0          
          9      > RETURN                                                   $3
   19    10    >   TYPE_CHECK                                   64          !0
         11      > JMPZ                                                     ~5, ->19
   20    12    >   NEW                                              $6      'ArrayIterator'
         13        INIT_FCALL                                               'str_split'
         14        SEND_VAR                                                 !0
         15        DO_ICALL                                         $7      
         16        SEND_VAR_NO_REF_EX                                       $7
         17        DO_FCALL                                      0          
         18      > RETURN                                                   $6
   22    19    >   NEW                                              $9      'Exception'
         20        DO_FCALL                                      0          
         21      > THROW                                         0          $9
   23    22*     > RETURN                                                   null

End of function as_iterator

Function do_map:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fmS6q
function name:  do_map
number of ops:  5
compiled vars:  !0 = $function
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
   27     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FfmS6q%3A27%240'
          2        BIND_LEXICAL                                             ~1, !0
   31     3      > RETURN                                                   ~1
   32     4*     > RETURN                                                   null

End of function do_map

Function %00%7Bclosure%7D%2Fin%2FfmS6q%3A27%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 12
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 12
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 12
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 12
filename:       /in/fmS6q
function name:  {closure}
number of ops:  14
compiled vars:  !0 = $iterator, !1 = $function, !2 = $value, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
          1        GENERATOR_CREATE                                         
          2        BIND_STATIC                                              !1
   28     3      > FE_RESET_R                                       $4      !0, ->12
          4    > > FE_FETCH_R                                       ~5      $4, !2, ->12
          5    >   ASSIGN                                                   !3, ~5
   29     6        INIT_DYNAMIC_CALL                                        !1
          7        SEND_VAR_EX                                              !2
          8        SEND_VAR_EX                                              !3
          9        DO_FCALL                                      0  $7      
         10        YIELD                                                    $7, !3
   28    11      > JMP                                                      ->4
         12    >   FE_FREE                                                  $4
   31    13      > GENERATOR_RETURN                                         

End of function %00%7Bclosure%7D%2Fin%2FfmS6q%3A27%240

Function do_filter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fmS6q
function name:  do_filter
number of ops:  5
compiled vars:  !0 = $function
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
   36     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FfmS6q%3A36%241'
          2        BIND_LEXICAL                                             ~1, !0
   42     3      > RETURN                                                   ~1
   43     4*     > RETURN                                                   null

End of function do_filter

Function %00%7Bclosure%7D%2Fin%2FfmS6q%3A36%241:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 13
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 13
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 12
Branch analysis from position: 13
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 13
filename:       /in/fmS6q
function name:  {closure}
number of ops:  15
compiled vars:  !0 = $iterator, !1 = $function, !2 = $value, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
          1        GENERATOR_CREATE                                         
          2        BIND_STATIC                                              !1
   37     3      > FE_RESET_R                                       $4      !0, ->13
          4    > > FE_FETCH_R                                       ~5      $4, !2, ->13
          5    >   ASSIGN                                                   !3, ~5
   38     6        INIT_DYNAMIC_CALL                                        !1
          7        SEND_VAR_EX                                              !2
          8        SEND_VAR_EX                                              !3
          9        DO_FCALL                                      0  $7      
         10      > JMPZ                                                     $7, ->12
   39    11    >   YIELD                                                    !2, !3
   37    12    > > JMP                                                      ->4
         13    >   FE_FREE                                                  $4
   42    14      > GENERATOR_RETURN                                         

End of function %00%7Bclosure%7D%2Fin%2FfmS6q%3A36%241

Function do_concat:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fmS6q
function name:  do_concat
number of ops:  5
compiled vars:  !0 = $iterable
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
   47     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FfmS6q%3A47%242'
          2        BIND_LEXICAL                                             ~1, !0
   54     3      > RETURN                                                   ~1
   55     4*     > RETURN                                                   null

End of function do_concat

Function %00%7Bclosure%7D%2Fin%2FfmS6q%3A47%242:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 7
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 7
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 15
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 15
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 15
Branch analysis from position: 7
filename:       /in/fmS6q
function name:  {closure}
number of ops:  17
compiled vars:  !0 = $iterator, !1 = $iterable, !2 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
          1        GENERATOR_CREATE                                         
          2        BIND_STATIC                                              !1
   48     3      > FE_RESET_R                                       $3      !0, ->7
          4    > > FE_FETCH_R                                               $3, !2, ->7
   49     5    >   YIELD                                                    !2
   48     6      > JMP                                                      ->4
          7    >   FE_FREE                                                  $3
   51     8        INIT_FCALL                                               'as_iterator'
          9        SEND_VAR                                                 !1
         10        DO_FCALL                                      0  $5      
         11      > FE_RESET_R                                       $6      $5, ->15
         12    > > FE_FETCH_R                                               $6, !2, ->15
   52    13    >   YIELD                                                    !2
   51    14      > JMP                                                      ->12
         15    >   FE_FREE                                                  $6
   54    16      > GENERATOR_RETURN                                         

End of function %00%7Bclosure%7D%2Fin%2FfmS6q%3A47%242

Function do_as_array:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fmS6q
function name:  do_as_array
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FfmS6q%3A59%243'
   61     1      > RETURN                                                   ~0
   62     2*     > RETURN                                                   null

End of function do_as_array

Function %00%7Bclosure%7D%2Fin%2FfmS6q%3A59%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fmS6q
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $iterator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
   60     1        INIT_FCALL                                               'iterator_to_array'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $1      
          4      > RETURN                                                   $1
   61     5*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FfmS6q%3A59%243

Function %00%7Bclosure%7D%2Fin%2FfmS6q%3A66%244:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fmS6q
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     0  E >   RECV                                             !0      
          1        MOD                                              ~1      !0, 2
          2        IS_EQUAL                                         ~2      ~1, 0
          3      > RETURN                                                   ~2
          4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FfmS6q%3A66%244

Function %00%7Bclosure%7D%2Fin%2FfmS6q%3A67%245:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fmS6q
function name:  {closure}
number of ops:  4
compiled vars:  !0 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   RECV                                             !0      
          1        MUL                                              ~1      !0, 3
          2      > RETURN                                                   ~1
          3*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FfmS6q%3A67%245

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.55 ms | 1415 KiB | 25 Q