3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* span :: (a -> Bool) -> [a] -> ([a],[a]) span _ xs@[] = (xs, xs) span p xs@(x:xs') | p x = let (ys,zs) = span p xs' in (x:ys,zs) | otherwise = ([],xs) */ function span($p, $xs) { if (empty($xs)) { return [[], []]; } $xs_ = $xs; $x = array_shift($xs_); if ($p($x)) { list($ys, $zs) = span($p, $xs_); return [array_merge([$x], $ys), $zs]; } return [[], $xs]; } /* groupBy :: (a -> a -> Bool) -> [a] -> [[a]] groupBy _ [] = [] groupBy eq (x:xs) = (x:ys) : groupBy eq zs where (ys,zs) = span (eq x) xs */ function array_group(callable $eq, array $xs) { if (empty($xs)) { return []; } $x = array_shift($xs); // curry of $eq $eqx = function ($y) use ($eq, $x) { return $eq($x, $y); }; list($ys, $zs) = span($eqx, $xs); return array_merge([array_merge([$x], $ys)], array_group($eq, $zs)); } function it($m,$p){echo ($p?'OK':'NO')." It $m\n"; if(!$p){$GLOBALS['f']=1;}} function done(){if(@$GLOBALS['f'])die(1);} it('spans elements which match', span(function ($x) { return $x < 3; }, [1, 2, 3]) === [[1, 2], [3]]); it('spans elements which match from the start', span(function ($x) { return $x == 3; }, [1, 6, 3, 3]) === [[], [1, 2, 3, 3]]); it('spans returns empty lists on empty list', span(function ($x) { return false; }, []) === [[], []]); it('groups elements recursively', array_group(function ($a, $b) { return $a <= ($b-1); }, [1, 1, 2, 1]) === [[1, 1], [2], [1]]); print_r(array_group(function ($a, $b) { return $a <= ($b-1); }, [1, 1, 2, 1]));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/JJP5V
function name:  (null)
number of ops:  49
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   INIT_FCALL                                               'it'
          1        SEND_VAL                                                 'spans+elements+which+match'
          2        INIT_FCALL                                               'span'
          3        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FJJP5V%3A50%241'
          4        SEND_VAL                                                 ~0
          5        SEND_VAL                                                 <array>
          6        DO_FCALL                                      0  $1      
          7        IS_IDENTICAL                                     ~2      $1, <array>
          8        SEND_VAL                                                 ~2
          9        DO_FCALL                                      0          
   51    10        INIT_FCALL                                               'it'
         11        SEND_VAL                                                 'spans+elements+which+match+from+the+start'
         12        INIT_FCALL                                               'span'
         13        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FJJP5V%3A51%242'
         14        SEND_VAL                                                 ~4
         15        SEND_VAL                                                 <array>
         16        DO_FCALL                                      0  $5      
         17        IS_IDENTICAL                                     ~6      $5, <array>
         18        SEND_VAL                                                 ~6
         19        DO_FCALL                                      0          
   52    20        INIT_FCALL                                               'it'
         21        SEND_VAL                                                 'spans+returns+empty+lists+on+empty+list'
         22        INIT_FCALL                                               'span'
         23        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FJJP5V%3A52%243'
         24        SEND_VAL                                                 ~8
         25        SEND_VAL                                                 <array>
         26        DO_FCALL                                      0  $9      
         27        IS_IDENTICAL                                     ~10     $9, <array>
         28        SEND_VAL                                                 ~10
         29        DO_FCALL                                      0          
   55    30        INIT_FCALL                                               'it'
         31        SEND_VAL                                                 'groups+elements+recursively'
         32        INIT_FCALL                                               'array_group'
         33        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FJJP5V%3A55%244'
         34        SEND_VAL                                                 ~12
         35        SEND_VAL                                                 <array>
         36        DO_FCALL                                      0  $13     
         37        IS_IDENTICAL                                     ~14     $13, <array>
         38        SEND_VAL                                                 ~14
         39        DO_FCALL                                      0          
   57    40        INIT_FCALL                                               'print_r'
         41        INIT_FCALL                                               'array_group'
         42        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FJJP5V%3A57%245'
         43        SEND_VAL                                                 ~16
         44        SEND_VAL                                                 <array>
         45        DO_FCALL                                      0  $17     
         46        SEND_VAR                                                 $17
         47        DO_ICALL                                                 
         48      > RETURN                                                   1

Function span:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 31
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/JJP5V
function name:  span
number of ops:  35
compiled vars:  !0 = $p, !1 = $xs, !2 = $xs_, !3 = $x, !4 = $ys, !5 = $zs
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   11     2        ISSET_ISEMPTY_CV                                         !1
          3      > JMPZ                                                     ~6, ->5
   12     4    > > RETURN                                                   <array>
   15     5    >   ASSIGN                                                   !2, !1
   16     6        INIT_FCALL                                               'array_shift'
          7        SEND_REF                                                 !2
          8        DO_ICALL                                         $8      
          9        ASSIGN                                                   !3, $8
   18    10        INIT_DYNAMIC_CALL                                        !0
         11        SEND_VAR_EX                                              !3
         12        DO_FCALL                                      0  $10     
         13      > JMPZ                                                     $10, ->31
   19    14    >   INIT_FCALL_BY_NAME                                       'span'
         15        SEND_VAR_EX                                              !0
         16        SEND_VAR_EX                                              !2
         17        DO_FCALL                                      0  $11     
         18        FETCH_LIST_R                                     $12     $11, 0
         19        ASSIGN                                                   !4, $12
         20        FETCH_LIST_R                                     $14     $11, 1
         21        ASSIGN                                                   !5, $14
         22        FREE                                                     $11
   21    23        INIT_FCALL                                               'array_merge'
         24        INIT_ARRAY                                       ~16     !3
         25        SEND_VAL                                                 ~16
         26        SEND_VAR                                                 !4
         27        DO_ICALL                                         $17     
         28        INIT_ARRAY                                       ~18     $17
         29        ADD_ARRAY_ELEMENT                                ~18     !5
         30      > RETURN                                                   ~18
   24    31    >   INIT_ARRAY                                       ~19     <array>
         32        ADD_ARRAY_ELEMENT                                ~19     !1
         33      > RETURN                                                   ~19
   25    34*     > RETURN                                                   null

End of function span

Function array_group:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/JJP5V
function name:  array_group
number of ops:  38
compiled vars:  !0 = $eq, !1 = $xs, !2 = $x, !3 = $eqx, !4 = $ys, !5 = $zs
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   34     2        ISSET_ISEMPTY_CV                                         !1
          3      > JMPZ                                                     ~6, ->5
   35     4    > > RETURN                                                   <array>
   38     5    >   INIT_FCALL                                               'array_shift'
          6        SEND_REF                                                 !1
          7        DO_ICALL                                         $7      
          8        ASSIGN                                                   !2, $7
   40     9        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FJJP5V%3A40%240'
         10        BIND_LEXICAL                                             ~9, !0
         11        BIND_LEXICAL                                             ~9, !2
         12        ASSIGN                                                   !3, ~9
   42    13        INIT_FCALL                                               'span'
         14        SEND_VAR                                                 !3
         15        SEND_VAR                                                 !1
         16        DO_FCALL                                      0  $11     
         17        FETCH_LIST_R                                     $12     $11, 0
         18        ASSIGN                                                   !4, $12
         19        FETCH_LIST_R                                     $14     $11, 1
         20        ASSIGN                                                   !5, $14
         21        FREE                                                     $11
   44    22        INIT_FCALL                                               'array_merge'
         23        INIT_FCALL                                               'array_merge'
         24        INIT_ARRAY                                       ~16     !2
         25        SEND_VAL                                                 ~16
         26        SEND_VAR                                                 !4
         27        DO_ICALL                                         $17     
         28        INIT_ARRAY                                       ~18     $17
         29        SEND_VAL                                                 ~18
         30        INIT_FCALL_BY_NAME                                       'array_group'
         31        SEND_VAR_EX                                              !0
         32        SEND_VAR_EX                                              !5
         33        DO_FCALL                                      0  $19     
         34        SEND_VAR                                                 $19
         35        DO_ICALL                                         $20     
         36      > RETURN                                                   $20
   45    37*     > RETURN                                                   null

End of function array_group

Function %00%7Bclosure%7D%2Fin%2FJJP5V%3A40%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/JJP5V
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $y, !1 = $eq, !2 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
          3        INIT_DYNAMIC_CALL                                        !1
          4        SEND_VAR_EX                                              !2
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $3      
          7      > RETURN                                                   $3
          8*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FJJP5V%3A40%240

Function it:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 16
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 16
Branch analysis from position: 13
Branch analysis from position: 16
filename:       /in/JJP5V
function name:  it
number of ops:  17
compiled vars:  !0 = $m, !1 = $p
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2      > JMPZ                                                     !1, ->5
          3    >   QM_ASSIGN                                        ~2      'OK'
          4      > JMP                                                      ->6
          5    >   QM_ASSIGN                                        ~2      'NO'
          6    >   ROPE_INIT                                     3  ~4      '+It+'
          7        ROPE_ADD                                      1  ~4      ~4, !0
          8        ROPE_END                                      2  ~3      ~4, '%0A'
          9        CONCAT                                           ~6      ~2, ~3
         10        ECHO                                                     ~6
         11        BOOL_NOT                                         ~7      !1
         12      > JMPZ                                                     ~7, ->16
         13    >   FETCH_W                      global              $8      'GLOBALS'
         14        ASSIGN_DIM                                               $8, 'f'
         15        OP_DATA                                                  1
         16    > > RETURN                                                   null

End of function it

Function done:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/JJP5V
function name:  done
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   BEGIN_SILENCE                                    ~0      
          1        FETCH_R                      global              ~1      'GLOBALS'
          2        FETCH_DIM_R                                      ~2      ~1, 'f'
          3        END_SILENCE                                              ~0
          4      > JMPZ                                                     ~2, ->6
          5    > > EXIT                                                     1
          6    > > RETURN                                                   null

End of function done

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

End of function %00%7Bclosure%7D%2Fin%2FJJP5V%3A50%241

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

End of function %00%7Bclosure%7D%2Fin%2FJJP5V%3A51%242

Function %00%7Bclosure%7D%2Fin%2FJJP5V%3A52%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/JJP5V
function name:  {closure}
number of ops:  3
compiled vars:  !0 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
          1      > RETURN                                                   <false>
          2*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FJJP5V%3A52%243

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

End of function %00%7Bclosure%7D%2Fin%2FJJP5V%3A55%244

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

End of function %00%7Bclosure%7D%2Fin%2FJJP5V%3A57%245

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
163.2 ms | 1418 KiB | 29 Q