PHP code example of vector / core

1. Go to this page and download the library: Download vector/core library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vector / core example snippets


          array_sum(
              array_map(
                  fn($a) => $a + 1,
                  [1, 2, 3]
              )
          );
          // 9
        

          collect([1, 2, 3])
              ->map(fn($a) => $a + 1)
              ->sum();
              // 9
        

           vector([1, 2, 3])
               ->pipe(Arrays::map(Math::add(1))) // or `fn($a) => $a + 1)` 
               ->pipe(Math::sum())();
               // [2, 3, 4]
        

$addOne = Arrays::map(Math::add(1));
$addOne([1, 2, 3]); // [2, 3, 4]

$addSix = Lambda::compose(Math::add(4), Math::add(2)); // (Or ::pipe for the opposite flow direction)
$addSix(4); // 10;

Pattern::match([
    fn(Just $value) => fn ($unwrapped) => $unwrapped,
    fn(Nothing $value) => 'nothing',
])(Maybe::just('just')); // 'just'

$errorHandler = function (Err $err) {
    return Pattern::match([
        function (QueryException $exception) {
            Log::info($exception);
            return response(404);
        },
        function (DBException $exception) {
            Log::error($exception);
            return response(500);
        },
    ]);
};

return Pattern::match([
    fn(Ok $value) => fn (User $user) => $user,
    $errorHandler
])(Result::from(fn() => User::findOrFail(1)));

use Vector\Core\Curry;
use Vector\Core\Module;

class MyModule
{
    use Module;
    
    #[Curry]
    protected static function myCurriedFunction($a, $b)
    {
        return $a + $b;
    }
}