Download the PHP package widmogrod/php-functional without Composer
On this page you can find all versions of the php package widmogrod/php-functional. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download widmogrod/php-functional
More information about widmogrod/php-functional
Files in widmogrod/php-functional
Package php-functional
Short Description Functors, Applicative and Monads are fascinating concept. Purpose of this library is to explore them in OOP PHP world.
License MIT
Informations about the package php-functional
PHP Functional
Introduction
Functional programing is a fascinating concept.
The purpose of this library is to explore Functors
, Applicative Functors
and Monads
in OOP PHP, and provide examples of real world use case.
Monad types available in the project:
State Monad
IO Monad
Free Monad
Either Monad
Maybe Monad
Reader Monad
Writer Monad
Exploring functional programing space I noticed that working with primitive values from PHP is very hard and complicates implementation of many functional structures. To simplify this experience, set of higher order primitives is introduced in library:
Num
Sum
Product
Stringg
Listt
(a.k.a List Monad, sincelist
is a protected keyword in PHP)
Applications
Known applications of this project
- Algorithm W implemented in PHP based on Martin Grabmüller work.
Installation
Development
This repository follows semantic versioning concept. If you want to contribute, just follow CONTRIBUTING.md
Testing
Quality assurance is brought to you by:
- PHPUnit
- Eris - QuickCheck and property-based testing tools to the PHP and PHPUnit ecosystem.
- PHP-CS-Fixer - A tool to automatically fix PHP coding standards issues
Use Cases
You can find more use cases and examples in the example directory.
NOTE: Don't be confused when browsing thought examples you will see phrase like "list functor" and in this library you will see
Widmogrod\Primitive\Listt
. Monad is Functor and Applicative. You could say that Monad implements Functor and Applicative.
List Functor
List Applicative Functor
Apply function on list of values and as a result, receive list of all possible combinations of applying function from the left list to a value in the right one.
Maybe Monoid
Using Maybe as an instance of Monoid simplifies concat and reduce operations by using Maybe's abstraction over potentially missing values. See an example of constructing a person's full name from first, middle, and last without having to explicitly check if each part exists.
Maybe and List Monad
Extracting from a list of uneven values can be tricky and produce nasty code full of if (isset)
statements.
By combining List and Maybe Monad, this process becomes simpler and more readable.
Either Monad
In php
world, the most popular way of saying that something went wrong is to throw an exception.
This results in nasty try catch
blocks and many of if statements.
Either Monad shows how we can fail gracefully without breaking the execution chain and making the code more readable.
The following example demonstrates combining the contents of two files into one. If one of those files does not exist the operation fails gracefully.
IO Monad
Example usage of IO Monad
. Read input from stdin
, and print it to stdout
.
Writer Monad
The Writer monad
is useful to keep logs in a pure way. Coupled with filterM
for example, this allows you to know exactly why an element was filtered.
Reader Monad
The Reader monad
provides a way to share a common environment, such as configuration information or class instances, across multiple functions.
Free Monad in PHP
Imagine that you first write business logic and don't care about implementation details like:
- how and from where get user discounts
- how and where save products in basket
- and more ...
When your business logic is complete, then you can concentrate on those details.
Free monad enables you to do exactly that, and more:
- Write business logic first
- Write your own DLS (domain specific language)
- Decouple implementation from interpretation.
Echo program
Example Free Monad example of echo program
can be found here:
- See source code of Haskell implementation
DSL for BDD
tests
Example that use Free Monad
to creates simple DSL (Domain Specific Language) to define BDD type of framework:
- See source code of FreeBddStyleDSLTest.php
Free Monad Calculator example
Example of a DSL
for a naive calculator that is implemented by using FreeMonad.
Free monad can be interpreted as a real calculator or calculation formatter a.k.a. pretty printer. Additional thing that I wanted to tackle was a Free Monad Optimisation.
Considering that Free Monad is like AST, question arose in my mind - can I travers it and update it to simplify computation? Hot to do it? What are limitation of Free Monad? Calculator example is an outcome of those questions.
- See source code of FreeCalculatorTest.php
Haskell do notation
in PHP
Why Haskell's do notation is interesting?
In Haskell is just an "syntax sugar" and in many ways is not needed, but in PHP control flow of monads can be hard to track.
Consider example, that use only chaining bind()
and compare it to the same version but with do notation
in PHP.
Control flow without do notation
Control flow with do notation
Everyone needs to judge by itself, but in my opinion do notation
improve readability of code in PHP.
Book Functional PHP
by Gilles Crettenand
In recently published book Functional PHP
by Gilles Crettenand, you can learn more applications of widmogrod/php-functional
, see how it compares to other projects and how in an effortless way apply functional thinking in daily work.
References
Here links to their articles/
libraries that help me understood the domain:
- http://drboolean.gitbooks.io/mostly-adequate-guide
- https://github.com/fantasyland/fantasy-land
- http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html
- http://learnyouahaskell.com/functors-applicative-functors-and-monoids
- http://learnyouahaskell.com/starting-out#im-a-list-comprehension
- http://robotlolita.me/2013/12/08/a-monad-in-practicality-first-class-failures.html
- http://robotlolita.me/2014/03/20/a-monad-in-practicality-controlling-time.html
- https://github.com/folktale/data.either
- https://github.com/widmogrod/php-algorithm-w