Git Product home page Git Product logo

vector's Introduction

Vector Core Badge Status

The Elevator Pitch

Vector gives you php functional superpowers.

  • The evolution:

    • Native PHP
        array_sum(
            array_map(
                fn($a) => $a + 1,
                [1, 2, 3]
            )
        );
        // 9
      • ๐Ÿ‘Ž More than 1 or 2 function chains is unmaintainable
    • Laravel Collections
        collect([1, 2, 3])
            ->map(fn($a) => $a + 1)
            ->sum();
            // 9
      • ๐Ÿ‘ More than 1 or 2 function chains is unmaintainable
      • ๐Ÿ‘Ž Unfortunately you can't do this with every type in the same elegant way (only works with collections)
    • Vector
         vector([1, 2, 3])
             ->pipe(Arrays::map(Math::add(1))) // or `fn($a) => $a + 1)` 
             ->pipe(Math::sum())();
             // [2, 3, 4]
      • ๐Ÿ‘ Works super similarly to collections, but just accepts & returns normal arrays (no ->toArray()-ing necessary)
      • ๐Ÿ‘ Works super similarly to collections for everything else too!
      • ๐Ÿ‘Ž Unfortunately it is an extra dependency (we don't have the native pipe operator yet https://wiki.php.net/rfc/pipe-operator-v2)
  • You can add currying to any function, it isn't only limited to Vector built ins.

    • Module::curry('explode')(',')('a,b,c')(PHP_INT_MAX) // ['a', 'b', 'c']

PHP Version Support

  • 8.0+

Install

composer require vector/core

Show Me Some More Code

More automatic currying.

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

First class composition (Functional Pipelines).

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

Pattern Matching (Maybe & Result monads included).

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

Granular control flow (without try/catch).

$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)));

Make your own modules with auto-curried methods

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

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

vector's People

Contributors

joseph-walker avatar loganhenson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

vector's Issues

ArrayList \ DropLast

dropLast :: Int -> [a] -> [a]
Returns the given list sans the last n elements

ArrayList \ Zip

zip :: [a] -> [b] -> [[a, b]]
Zip two lists together into a 2-d array pairwise

ArrayList \ All

all :: (a -> Bool) -> [a] -> Bool
Returns true if every element of [a] passes the given test.

Missing Documentation \ Module

The module lacks documentation. Things that should be covered in the docs:

  • How Module works
  • How to create your own module

Object \ Call

call :: String -> Object -> a
Call the method String on the given Object and return its result

ArrayList \ DropWhile

dropWhile :: (a -> Bool) -> [a] -> [a]
Returns the list sans the first elements that pass the given test

Math \ Range

range :: Int -> Int -> [Int]
Returns all integers between the given arguments, inclusive

ArrayList \ FindIndex

findIndex :: (a -> Bool) -> [a] -> Maybe Int
Attempts to find an element's index by using a given test

ArrayList \ Adjust

adjust :: (a -> a) -> Int -> [a] -> [a]
Applies a function to the value at a given index.

Math \ Negate

negate :: Num -> Num
Returns the given number * -1

Monad \ Sequence

sequence :: Monad m => [m a] -> m [a]
Convert a list of monad into a monad of list

Math \ Max

max :: Num -> Num -> Num
Returns the maximum of its two arguments

Math \ Min

min :: Num -> Num -> Num
Returns the minimum of its two arguments

ArrayList \ TakeWhile

takeWhile :: (a -> Bool) -> [a] -> [a]
Take items from a list while a given test passes

ArrayList \ Any

any :: (a -> Bool) -> [a] -> Bool
Returns true if any element of [a] passes the given test

Math \ Product

product :: [Num] -> Num
Returns the product of the given list

Strings \ Trim

trim :: String -> String
Returns a string with leading and trailing whitespace removed

ArrayList \ Drop

drop :: Int -> [a] -> [a]
Returns the given list sans the first n elements

Memoize 'using' on module import.

This is low hanging fruit for optimization. When calling Module::using() in multiple places and requesting the same function, memoize and store the result of the internal reflection call so that a single reflection is never duplicated.

ArrayList \ Find

find :: (a -> Bool) -> [a] -> Maybe a
Attempts to find an element by using a given test

Math \ Sum

sum :: [Num] -> Num
Returns the sum of the given list

Math \ Mod

mod :: Int -> Int -> Int
Modulus operator, divisor is first argument

ArrayList \ DropLastWhile

dropLastWhile :: (a -> Bool) -> [a] -> [a]
Returns the list sans the last elements that pass the given test

Logic \ Or

or :: Bool -> Bool -> Bool
Logical or

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.