3v4l.org

run code in 300+ PHP versions simultaneously
<?php use \ArrayIterator; use \Traversable; class Enumerable { /** * * @var Traversable */ private $traversable; /** * * @param Traversable|array $traversable * @return Enumerable */ public static function enumerate($traversable) { return new Enumerable($traversable); } /** * * @param Traversable|array $traversable */ public function __construct($traversable) { if ($traversable instanceof Traversable) { $this->traversable = $traversable; } else { $this->traversable = new ArrayIterator((array) $traversable); } } /** * * @param callable $callable * @return Enumerable */ public function select(callable $callable) { $generator = function () use ($callable) { foreach ($this->traversable as $key => $value) { yield $key => $callable($value, $key); } }; return new Enumerable($generator()); } /** * * @param callable $callable * @return Enumerable */ public function filter(callable $callable) { $generator = function () use ($callable) { foreach ($this->traversable as $key => $value) { if ($callable($value, $key)) { yield $key => $value; } } }; return new Enumerable($generator()); } /** * * @return array */ public function toArray() { return iterator_to_array($this->traversable); } } $array = Enumerable::enumerate([ 1, 6, 3, 8, 6, 1, 9, 4, 2, 4, 6, 4, 2, ])->select(function ($i) { echo sprintf('Selecting %d * 2', $i) . PHP_EOL; return $i * 2; })->filter(function ($i) { echo sprintf(' Filtering %d > 10', $i) . PHP_EOL; return $i > 10; })->select(function ($i) { echo sprintf(' Selecting %d * 3', $i) . PHP_EOL; return $i * 3; })->filter(function ($i) { echo sprintf(' Filtering %d > 40', $i) . PHP_EOL; return $i > 40; })->select(function ($i) { echo sprintf(' Selecting %d * 4', $i) . PHP_EOL; return $i * 3; })->toArray(); var_dump($array);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/04Udl
function name:  (null)
number of ops:  30
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   82     0  E >   INIT_STATIC_METHOD_CALL                                  'Enumerable', 'enumerate'
   83     1        SEND_VAL                                                 <array>
          2        DO_FCALL                                      0  $1      
   84     3        INIT_METHOD_CALL                                         $1, 'select'
          4        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F04Udl%3A84%242'
   87     5        SEND_VAL_EX                                              ~2
          6        DO_FCALL                                      0  $3      
          7        INIT_METHOD_CALL                                         $3, 'filter'
          8        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F04Udl%3A87%243'
   90     9        SEND_VAL_EX                                              ~4
         10        DO_FCALL                                      0  $5      
         11        INIT_METHOD_CALL                                         $5, 'select'
         12        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F04Udl%3A90%244'
   93    13        SEND_VAL_EX                                              ~6
         14        DO_FCALL                                      0  $7      
         15        INIT_METHOD_CALL                                         $7, 'filter'
         16        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F04Udl%3A93%245'
   96    17        SEND_VAL_EX                                              ~8
         18        DO_FCALL                                      0  $9      
         19        INIT_METHOD_CALL                                         $9, 'select'
         20        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F04Udl%3A96%246'
   99    21        SEND_VAL_EX                                              ~10
         22        DO_FCALL                                      0  $11     
         23        INIT_METHOD_CALL                                         $11, 'toArray'
         24        DO_FCALL                                      0  $12     
   82    25        ASSIGN                                                   !0, $12
  101    26        INIT_FCALL                                               'var_dump'
         27        SEND_VAR                                                 !0
         28        DO_ICALL                                                 
         29      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2F04Udl%3A46%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 13
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 13
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 13
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 13
filename:       /in/04Udl
function name:  {closure}
number of ops:  15
compiled vars:  !0 = $callable, !1 = $value, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   GENERATOR_CREATE                                         
          1        BIND_STATIC                                              !0
   47     2        FETCH_THIS                                       $3      
          3        FETCH_OBJ_R                                      ~4      $3, 'traversable'
          4      > FE_RESET_R                                       $5      ~4, ->13
          5    > > FE_FETCH_R                                       ~6      $5, !1, ->13
          6    >   ASSIGN                                                   !2, ~6
   48     7        INIT_DYNAMIC_CALL                                        !0
          8        SEND_VAR_EX                                              !1
          9        SEND_VAR_EX                                              !2
         10        DO_FCALL                                      0  $8      
         11        YIELD                                                    $8, !2
   47    12      > JMP                                                      ->5
         13    >   FE_FREE                                                  $5
   50    14      > GENERATOR_RETURN                                         

End of function %00%7Bclosure%7D%2Fin%2F04Udl%3A46%240

Function %00%7Bclosure%7D%2Fin%2F04Udl%3A61%241:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 14
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 14
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 13
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 13
Branch analysis from position: 14
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 14
filename:       /in/04Udl
function name:  {closure}
number of ops:  16
compiled vars:  !0 = $callable, !1 = $value, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   GENERATOR_CREATE                                         
          1        BIND_STATIC                                              !0
   62     2        FETCH_THIS                                       $3      
          3        FETCH_OBJ_R                                      ~4      $3, 'traversable'
          4      > FE_RESET_R                                       $5      ~4, ->14
          5    > > FE_FETCH_R                                       ~6      $5, !1, ->14
          6    >   ASSIGN                                                   !2, ~6
   63     7        INIT_DYNAMIC_CALL                                        !0
          8        SEND_VAR_EX                                              !1
          9        SEND_VAR_EX                                              !2
         10        DO_FCALL                                      0  $8      
         11      > JMPZ                                                     $8, ->13
   64    12    >   YIELD                                                    !1, !2
   62    13    > > JMP                                                      ->5
         14    >   FE_FREE                                                  $5
   67    15      > GENERATOR_RETURN                                         

End of function %00%7Bclosure%7D%2Fin%2F04Udl%3A61%241

Function %00%7Bclosure%7D%2Fin%2F04Udl%3A84%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/04Udl
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   84     0  E >   RECV                                             !0      
   85     1        INIT_FCALL                                               'sprintf'
          2        SEND_VAL                                                 'Selecting+%25d+%2A++2'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $1      
          5        CONCAT                                           ~2      $1, '%0A'
          6        ECHO                                                     ~2
   86     7        MUL                                              ~3      !0, 2
          8      > RETURN                                                   ~3
   87     9*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F04Udl%3A84%242

Function %00%7Bclosure%7D%2Fin%2F04Udl%3A87%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/04Udl
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
          0  E >   RECV                                             !0      
   88     1        INIT_FCALL                                               'sprintf'
          2        SEND_VAL                                                 '++Filtering+%25d+%3E+10'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $1      
          5        CONCAT                                           ~2      $1, '%0A'
          6        ECHO                                                     ~2
   89     7        IS_SMALLER                                       ~3      10, !0
          8      > RETURN                                                   ~3
   90     9*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F04Udl%3A87%243

Function %00%7Bclosure%7D%2Fin%2F04Udl%3A90%244:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/04Udl
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
          0  E >   RECV                                             !0      
   91     1        INIT_FCALL                                               'sprintf'
          2        SEND_VAL                                                 '++++Selecting+%25d+%2A+3'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $1      
          5        CONCAT                                           ~2      $1, '%0A'
          6        ECHO                                                     ~2
   92     7        MUL                                              ~3      !0, 3
          8      > RETURN                                                   ~3
   93     9*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F04Udl%3A90%244

Function %00%7Bclosure%7D%2Fin%2F04Udl%3A93%245:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/04Udl
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
          0  E >   RECV                                             !0      
   94     1        INIT_FCALL                                               'sprintf'
          2        SEND_VAL                                                 '++++++Filtering+%25d+%3E+40'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $1      
          5        CONCAT                                           ~2      $1, '%0A'
          6        ECHO                                                     ~2
   95     7        IS_SMALLER                                       ~3      40, !0
          8      > RETURN                                                   ~3
   96     9*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F04Udl%3A93%245

Function %00%7Bclosure%7D%2Fin%2F04Udl%3A96%246:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/04Udl
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
          0  E >   RECV                                             !0      
   97     1        INIT_FCALL                                               'sprintf'
          2        SEND_VAL                                                 '++++++++Selecting+%25d+%2A+4'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $1      
          5        CONCAT                                           ~2      $1, '%0A'
          6        ECHO                                                     ~2
   98     7        MUL                                              ~3      !0, 3
          8      > RETURN                                                   ~3
   99     9*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F04Udl%3A96%246

Class Enumerable:
Function enumerate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/04Udl
function name:  enumerate
number of ops:  6
compiled vars:  !0 = $traversable
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
   22     1        NEW                                              $1      'Enumerable'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4      > RETURN                                                   $1
   23     5*     > RETURN                                                   null

End of function enumerate

Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/04Udl
function name:  __construct
number of ops:  13
compiled vars:  !0 = $traversable
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
   31     1        INSTANCEOF                                               !0, 'Traversable'
          2      > JMPZ                                                     ~1, ->6
   32     3    >   ASSIGN_OBJ                                               'traversable'
          4        OP_DATA                                                  !0
          5      > JMP                                                      ->12
   35     6    >   NEW                                              $4      'ArrayIterator'
          7        CAST                                          7  ~5      !0
          8        SEND_VAL_EX                                              ~5
          9        DO_FCALL                                      0          
         10        ASSIGN_OBJ                                               'traversable'
         11        OP_DATA                                                  $4
   37    12    > > RETURN                                                   null

End of function __construct

Function select:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/04Udl
function name:  select
number of ops:  11
compiled vars:  !0 = $callable, !1 = $generator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
   46     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F04Udl%3A46%240'
          2        BIND_LEXICAL                                             ~2, !0
          3        ASSIGN                                                   !1, ~2
   51     4        NEW                                              $4      'Enumerable'
          5        INIT_DYNAMIC_CALL                                        !1
          6        DO_FCALL                                      0  $5      
          7        SEND_VAR_NO_REF_EX                                       $5
          8        DO_FCALL                                      0          
          9      > RETURN                                                   $4
   52    10*     > RETURN                                                   null

End of function select

Function filter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/04Udl
function name:  filter
number of ops:  11
compiled vars:  !0 = $callable, !1 = $generator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
   61     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F04Udl%3A61%241'
          2        BIND_LEXICAL                                             ~2, !0
          3        ASSIGN                                                   !1, ~2
   68     4        NEW                                              $4      'Enumerable'
          5        INIT_DYNAMIC_CALL                                        !1
          6        DO_FCALL                                      0  $5      
          7        SEND_VAR_NO_REF_EX                                       $5
          8        DO_FCALL                                      0          
          9      > RETURN                                                   $4
   69    10*     > RETURN                                                   null

End of function filter

Function toarray:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/04Udl
function name:  toArray
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   77     0  E >   INIT_FCALL                                               'iterator_to_array'
          1        FETCH_OBJ_R                                      ~0      'traversable'
          2        SEND_VAL                                                 ~0
          3        DO_ICALL                                         $1      
          4      > RETURN                                                   $1
   78     5*     > RETURN                                                   null

End of function toarray

End of class Enumerable.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.38 ms | 1412 KiB | 19 Q