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/Pt82S
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
-------------------------------------------------------------------------------------
   50     0  E >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FPt82S%3A50%240'
          1        ASSIGN                                                   !0, ~4
   54     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
   65    17        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FPt82S%3A65%241'
         18        ASSIGN                                                   !2, ~11
   69    19        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FPt82S%3A69%242'
         20        BIND_LEXICAL                                             ~13, !2
         21        ASSIGN                                                   !3, ~13
   73    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/Pt82S
function name:  map
number of ops:  12
compiled vars:  !0 = $fn, !1 = $data, !2 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        GENERATOR_CREATE                                         
    5     3      > FE_RESET_R                                       $3      !1, ->10
          4    > > FE_FETCH_R                                               $3, !2, ->10
    6     5    >   INIT_DYNAMIC_CALL                                        !0
          6        SEND_VAR_EX                                              !2
          7        DO_FCALL                                      0  $4      
          8        YIELD                                                    $4
    5     9      > JMP                                                      ->4
         10    >   FE_FREE                                                  $3
    8    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/Pt82S
function name:  filter
number of ops:  13
compiled vars:  !0 = $fn, !1 = $data, !2 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        GENERATOR_CREATE                                         
   11     3      > FE_RESET_R                                       $3      !1, ->11
          4    > > FE_FETCH_R                                               $3, !2, ->11
   12     5    >   INIT_DYNAMIC_CALL                                        !0
          6        SEND_VAR_EX                                              !2
          7        DO_FCALL                                      0  $4      
          8      > JMPZ                                                     $4, ->10
   13     9    >   YIELD                                                    !2
   11    10    > > JMP                                                      ->4
         11    >   FE_FREE                                                  $3
   16    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/Pt82S
function name:  infinite_range
number of ops:  7
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   GENERATOR_CREATE                                         
   19     1        ASSIGN                                                   !0, 0
   20     2      > JMP                                                      ->5
   21     3    >   POST_INC                                         ~2      !0
          4        YIELD                                                    ~2
   20     5    > > JMPNZ                                                    <true>, ->3
   23     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/Pt82S
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
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        GENERATOR_CREATE                                         
   26     3        ASSIGN                                                   !2, 0
   27     4      > FE_RESET_R                                       $5      !1, ->12
          5    > > FE_FETCH_R                                               $5, !3, ->12
   28     6    >   POST_INC                                         ~6      !2
          7        IS_IDENTICAL                                             !0, ~6
          8      > JMPZ                                                     ~7, ->10
   29     9    > > JMP                                                      ->12
   32    10    >   YIELD                                                    !3
   27    11      > JMP                                                      ->5
         12    >   FE_FREE                                                  $5
   34    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/Pt82S
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
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
   37     3        ASSIGN                                                   !3, <true>
   38     4      > FE_RESET_R                                       $6      !1, ->19
          5    > > FE_FETCH_R                                               $6, !4, ->19
   39     6    >   TYPE_CHECK                                    2  ~7      !2
          7      > JMPZ_EX                                          ~7      ~7, ->9
          8    >   BOOL                                             ~7      !3
          9    > > JMPZ                                                     ~7, ->12
   40    10    >   ASSIGN                                                   !2, !4
   41    11      > JMP                                                      ->5
   43    12    >   INIT_DYNAMIC_CALL                                        !0
         13        SEND_VAR_EX                                              !2
         14        SEND_VAR_EX                                              !4
         15        DO_FCALL                                      0  $9      
         16        ASSIGN                                                   !2, $9
   44    17        ASSIGN                                                   !3, <false>
   38    18      > JMP                                                      ->5
         19    >   FE_FREE                                                  $6
   47    20      > RETURN                                                   !2
   48    21*     > RETURN                                                   null

End of function reduce

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

End of function %00%7Bclosure%7D%2Fin%2FPt82S%3A50%240

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

End of function %00%7Bclosure%7D%2Fin%2FPt82S%3A65%241

Function %00%7Bclosure%7D%2Fin%2FPt82S%3A69%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Pt82S
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $a, !1 = $b, !2 = $concat
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   69     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
   70     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
   71     9*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FPt82S%3A69%242

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.79 ms | 1411 KiB | 20 Q