3v4l.org

run code in 300+ PHP versions simultaneously
<?php // thanks to metaultralurker on reddit for inspiration function map(callable $fn, \Traversable $data) { foreach ($data as $v) { yield $fn($v); } } function filter(callable $fn, \Traversable $data) { foreach ($data as $v) { if ($fn($v)) { yield $v; } } } function infinite_range() { $i = 0; while (true) { yield $i++; } } function take($n, \Traversable $data) { $i = 0; foreach ($data as $v) { if ($i++ === $n) { break; } yield $v; } } function reduce(callable $fn, \Traversable $data, $runningTotal = null) { $first = true; foreach ($data as $v) { if (null === $runningTotal && $first) { $runningTotal = $v; continue; } $runningTotal = $fn($runningTotal, $v); $first = false; } return $runningTotal; } $isEven = function ($x) { return $x % 2 == 0; }; $data = take(10, map('floatval', filter($isEven, infinite_range()))); /* var_dump($data->current()); $data->next(); var_dump($data->current()); $data->next(); */ // var_dump(iterator_to_array($data)); $concat = function ($sep, $a, $b) { return $a.$sep.$b; }; $concatComma = function ($a, $b) use ($concat) { return $concat(',', $a, $b); }; var_dump(reduce($concatComma, $data));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vbnPs
function name:  (null)
number of ops:  30
compiled vars:  !0 = $isEven, !1 = $data, !2 = $concat, !3 = $concatComma
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FvbnPs%3A51%240'
          1        ASSIGN                                                   !0, ~4
   55     2        INIT_FCALL                                               'take'
          3        SEND_VAL                                                 10
          4        INIT_FCALL                                               'map'
          5        SEND_VAL                                                 'floatval'
          6        INIT_FCALL                                               'filter'
          7        SEND_VAR                                                 !0
          8        INIT_FCALL                                               'infinite_range'
          9        DO_FCALL                                      0  $6      
         10        SEND_VAR                                                 $6
         11        DO_FCALL                                      0  $7      
         12        SEND_VAR                                                 $7
         13        DO_FCALL                                      0  $8      
         14        SEND_VAR                                                 $8
         15        DO_FCALL                                      0  $9      
         16        ASSIGN                                                   !1, $9
   66    17        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FvbnPs%3A66%241'
         18        ASSIGN                                                   !2, ~11
   70    19        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FvbnPs%3A70%242'
         20        BIND_LEXICAL                                             ~13, !2
         21        ASSIGN                                                   !3, ~13
   74    22        INIT_FCALL                                               'var_dump'
         23        INIT_FCALL                                               'reduce'
         24        SEND_VAR                                                 !3
         25        SEND_VAR                                                 !1
         26        DO_FCALL                                      0  $15     
         27        SEND_VAR                                                 $15
         28        DO_ICALL                                                 
         29      > RETURN                                                   1

Function map:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 10
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 10
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 10
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 10
filename:       /in/vbnPs
function name:  map
number of ops:  12
compiled vars:  !0 = $fn, !1 = $data, !2 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        GENERATOR_CREATE                                         
    6     3      > FE_RESET_R                                       $3      !1, ->10
          4    > > FE_FETCH_R                                               $3, !2, ->10
    7     5    >   INIT_DYNAMIC_CALL                                        !0
          6        SEND_VAR_EX                                              !2
          7        DO_FCALL                                      0  $4      
          8        YIELD                                                    $4
    6     9      > JMP                                                      ->4
         10    >   FE_FREE                                                  $3
    9    11      > GENERATOR_RETURN                                         

End of function map

Function filter:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 11
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 10
Branch analysis from position: 11
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 11
filename:       /in/vbnPs
function name:  filter
number of ops:  13
compiled vars:  !0 = $fn, !1 = $data, !2 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        GENERATOR_CREATE                                         
   12     3      > FE_RESET_R                                       $3      !1, ->11
          4    > > FE_FETCH_R                                               $3, !2, ->11
   13     5    >   INIT_DYNAMIC_CALL                                        !0
          6        SEND_VAR_EX                                              !2
          7        DO_FCALL                                      0  $4      
          8      > JMPZ                                                     $4, ->10
   14     9    >   YIELD                                                    !2
   12    10    > > JMP                                                      ->4
         11    >   FE_FREE                                                  $3
   17    12      > GENERATOR_RETURN                                         

End of function filter

Function infinite_range:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 6, Position 2 = 3
Branch analysis from position: 6
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 6, Position 2 = 3
Branch analysis from position: 6
Branch analysis from position: 3
filename:       /in/vbnPs
function name:  infinite_range
number of ops:  7
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   GENERATOR_CREATE                                         
   20     1        ASSIGN                                                   !0, 0
   21     2      > JMP                                                      ->5
   22     3    >   POST_INC                                         ~2      !0
          4        YIELD                                                    ~2
   21     5    > > JMPNZ                                                    <true>, ->3
   24     6    > > GENERATOR_RETURN                                         

End of function infinite_range

Function take:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 12
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 12
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 12
Branch analysis from position: 12
filename:       /in/vbnPs
function name:  take
number of ops:  14
compiled vars:  !0 = $n, !1 = $data, !2 = $i, !3 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        GENERATOR_CREATE                                         
   27     3        ASSIGN                                                   !2, 0
   28     4      > FE_RESET_R                                       $5      !1, ->12
          5    > > FE_FETCH_R                                               $5, !3, ->12
   29     6    >   POST_INC                                         ~6      !2
          7        IS_IDENTICAL                                             !0, ~6
          8      > JMPZ                                                     ~7, ->10
   30     9    > > JMP                                                      ->12
   33    10    >   YIELD                                                    !3
   28    11      > JMP                                                      ->5
         12    >   FE_FREE                                                  $5
   35    13      > GENERATOR_RETURN                                         

End of function take

Function reduce:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 19
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 19
Branch analysis from position: 6
2 jumps found. (Code = 46) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 9
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/vbnPs
function name:  reduce
number of ops:  22
compiled vars:  !0 = $fn, !1 = $data, !2 = $runningTotal, !3 = $first, !4 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
   38     3        ASSIGN                                                   !3, <true>
   39     4      > FE_RESET_R                                       $6      !1, ->19
          5    > > FE_FETCH_R                                               $6, !4, ->19
   40     6    >   TYPE_CHECK                                    2  ~7      !2
          7      > JMPZ_EX                                          ~7      ~7, ->9
          8    >   BOOL                                             ~7      !3
          9    > > JMPZ                                                     ~7, ->12
   41    10    >   ASSIGN                                                   !2, !4
   42    11      > JMP                                                      ->5
   44    12    >   INIT_DYNAMIC_CALL                                        !0
         13        SEND_VAR_EX                                              !2
         14        SEND_VAR_EX                                              !4
         15        DO_FCALL                                      0  $9      
         16        ASSIGN                                                   !2, $9
   45    17        ASSIGN                                                   !3, <false>
   39    18      > JMP                                                      ->5
         19    >   FE_FREE                                                  $6
   48    20      > RETURN                                                   !2
   49    21*     > RETURN                                                   null

End of function reduce

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

End of function %00%7Bclosure%7D%2Fin%2FvbnPs%3A51%240

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

End of function %00%7Bclosure%7D%2Fin%2FvbnPs%3A66%241

Function %00%7Bclosure%7D%2Fin%2FvbnPs%3A70%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vbnPs
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $a, !1 = $b, !2 = $concat
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
   71     3        INIT_DYNAMIC_CALL                                        !2
          4        SEND_VAL_EX                                              '%2C'
          5        SEND_VAR_EX                                              !0
          6        SEND_VAR_EX                                              !1
          7        DO_FCALL                                      0  $3      
          8      > RETURN                                                   $3
   72     9*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FvbnPs%3A70%242

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
163.76 ms | 1411 KiB | 20 Q