Giter VIP home page Giter VIP logo

garp-functional's Introduction

PHPUnit PHPCS PHPStan

Garp Functional

Utility library embracing functional programming paradigms.

Documentation

Developed with ❤️ by GRRR

Philosophy

Garp\Functional is a practical functional library that strives to embrace functional paradigms.

  • Functions are pure, referentially transparent, without side-effects.
  • Data immutability is favored over mutating existing properties of given parameters.
  • In general, functions are curried. Almost all of the functions in the library can be called partially applied, to a point where this makes sense.
  • Function parameters are ordered to promote currying. Data is usually the last thing to go in, making every function a fine candidate to pass to native array_map, array_filter and the like, without having to create a closure around the call.
  • Higher-order functions are provided to fill the gaps. Functions like compose, partial, partial_right or not are legos for you to use in your own implementation.
  • Nothing is type-hinted too strictly. I don't care if you pass a string or an array to prop, as long as it allows accessing members thru bracket syntax [], I'll allow it. Nothing irks me more about PHP than not being able to toss a Traversable object into the native array_map.
  • Typeclasses are added, broadening the applicability of the functions. Next to primitives, they will now work with your objects as well, so long as they implement the right interface.

Usage

Installation:

composer require grrr-amsterdam/garp-functional

That'll do, all functions are available to you.

Read the docs for a complete reference

Thanks

This library is inspired by beautiful languages like Haskell and Clojure,
as well as the awesome RamdaJS library.

garp-functional's People

Contributors

hammenws avatar hansterdam avatar harmenjanssen avatar marcoworms avatar martijngastkemper avatar schoenkaft avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

garp-functional's Issues

prop doesn't call isset before accessing a property

The following object will fail when passed to prop():

$arrayAccessObj = new class implements ArrayAccess {
	protected $_data = [];
	public function offsetExists($offset) {
		return isset($this->_data[$offset]);
	}
	public function offsetGet($offset) {
		throw new Exception('Don\'t call get when offset is not set');
	}
	public function offsetSet($offset, $value) {}
	public function offsetUnset($offset) {}
};

No issue, this lib is awesome

I recently moved to PHP from JS at my work and I was searching for something like https://github.com/ramda/ramda

Thanks a lot for this, your lib has the best usage and UX from all the functional libs I saw until now

Don't let this die, I'll use it a lot and will try to open a PR if i find something to add here :D

concat_with is not curried correctly

It incorrectly passes array_slice(func_get_args(), 1) to partial when given <= 2 arguments.

This would be the correct call:

        return call_user_func(
            partial,
            concat_with,
            ...func_get_args()
        );

This needs a test.

Validate function arguments

Not every function that accepts a callable arguments validates it as such.
It would make the library a little more robust to throw an InvalidArgumentException for non-callable arguments in those cases.

Reduce example in docs doesn't work because `f` namespace shortcut doesn't always work

So, this is a really cool issue. If we get the reduce example:

$sum = f\reduce('f\add', 0, $numbers); // 90

this will explode:

PHP Warning:  array_reduce() expects parameter 2 to be a valid callback, function 'f\add' not found or invalid function name in /home/worms/Documents/phpplayground/vendor/grrr-amsterdam/garp-functional/library/Garp/Functional/Reduce.php on line 21

and that happens because PHP doesn't translate 'f' inside the string to 'Garp\Functional'. You either have to send the whole namespace:

$sum = f\reduce('Garp\Functional\add', 0, $numbers); // 90

Or do a nifty ugly little workaround like I did:

$f = 'Garp\Functional\\';
$sum = f\reduce($f.'add', 0, $numbers); // 90

So this opens up 2 issues:

  1. (Solved in PR: #5) We need to fix the docs example because it doesn't work and may cause confusion.

  2. Is there a better way to send functions inside renamed namespaces as parameters?

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.