PHP


$result = array_sum ( $intArray ) ;
$headOfArr = array_slice ( $someArray , 1 ) ;
$moreThan = array_filter ( function ($x) { return $x > 3 ; } , array (1,2,3,4,5,6) ) ;

пример

in script file te.php:

<?php
$ys = array (1,2,3,4,5,6) ;
$moreThan = array_filter ( $ys , function ($x) { return $x > 2 ; }  ) ;
print_r ( $moreThan ) ;
?>

in shell:

[user@cat:~/langs/php]$ php -f te.php 
Array
(
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)
 

mapping

$result = array_map ('strtoupper', $strArray) ;

reducing

$result = array_reduce ( $intArray, function ($a, $b) { return $a + $b ; }, 0 ) ;

замыкания

$rate = .12 ;

$findInterest = function ( $val ) use ( $rate ) 
{
    return $val * $rate ;
} ;

print_r ( $findInterest (100) ) ;

рекурсия

function sum ( $bs ) 
{
    if (empty ( $bs ) ) return 0 ;
    else return $bs[0] + sum ( array_slice ( $bs , 1 )) ;

} ;

$total = sum ( array (1, 2, 3) ) ; 

print ($total) ;

HOF

$ys = array (0, 2, 3, 4, 5, 6) ;

$f1 = function ($x) 
{
    return ($x % 2) == 0 ;
} ;

$f2 = array_filter ( $ys , $f1 ) ;

$f3 = array_filter ( $ys , function($x) 
    {
      return ($x % 2) == 0 ;
    }
  ) ;

print_r ($f2) ;
print_r ($f3) ;

function f9 ( $min )
{
    return function ( $x ) use ( $min ) 
    {
        return $x > $min ;
    } ;
}

$ys = array (1, 2, 3, 4, 5, 6) ;

$f = array_filter ( $ys , f9 (3) ) ;

print_r ( $f ) ;