Skip to content

Commit b5cf38a

Browse files
authored
Merge pull request #88 from widmogrod/feature/readme-update
Readme improvements
2 parents 282ed2b + e275098 commit b5cf38a

File tree

4 files changed

+77
-38
lines changed

4 files changed

+77
-38
lines changed

CHANGELOG.md

-8
This file was deleted.

CONTRIBUTING.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# Creating pull request
1+
# Contribution guidelines
2+
3+
## Creating pull request
24
1. Open a related issue in the [Issue Tracker](https://github.com/EduardoGR/php-functional/issues)
35
2. Fork repository on Web Page
46
3. Clone this repository `git clone https://github.com/{your-username}/php-functional`
@@ -7,5 +9,11 @@
79
6. Push your changes `git push origin {your-username}-{what-i-am-going-to-develop}`
810
7. Create a Pull Request to master on Web Page
911

10-
# Code style
11-
php-functional follows the PSR-4 autoloading standard.
12+
## Code style
13+
Code style rules are defined and automated via [PHP-CS-Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer)
14+
to automatically applied them to code base please run:
15+
```
16+
composer fix
17+
```
18+
19+
`php-functional` follows the PSR-4 autoloading standard.

README.md

+66-27
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
## Introduction
55

66
Functional programing is a fascinating concept.
7-
The purpose of this library is to explore `Functors`, `Applicative Functors` and `Monads` in OOP PHP, and provide examples of real world use case.
7+
The purpose of this library is to explore `Functors`, `Applicative Functors`
8+
and `Monads` in OOP PHP, and provide examples of real world use case.
89

910
Monad types available in the project:
1011
* `State Monad`
@@ -15,33 +16,36 @@ Monad types available in the project:
1516
* `Reader Monad`
1617
* `Writer Monad`
1718

18-
Exploring functional programing space I noticed that working with primitive values from PHP is very hard and complicates implementation of many functional structures.
19+
Exploring functional programing space I noticed that working with primitive values from PHP
20+
is very hard and complicates implementation of many functional structures.
1921
To simplify this experience, set of higher order primitives is introduced in library:
2022
* `Num`
2123
* `Sum`
2224
* `Product`
2325
* `Stringg`
2426
* `Listt` (a.k.a List Monad, since `list` is a protected keyword in PHP)
25-
27+
2628
## Installation
2729

2830
```
2931
composer require widmogrod/php-functional
3032
```
3133

3234
## Development
33-
3435
This repository follows [semantic versioning concept](http://semver.org/).
35-
If you want to contribute, just follow [GitHub workflow](https://guides.github.com/introduction/flow/) and open a pull request.
36+
If you want to contribute, just follow [CONTRIBUTING.md](/CONTRIBUTING.md)
3637

37-
More information about changes you can find in [change log](/CHANGELOG.md)
3838

3939
## Testing
4040

41-
Quality assurance is brought to you by [PHPSpec](http://www.phpspec.net/)
41+
Quality assurance is brought to you by:
42+
- [PHPUnit](https://phpunit.de)
43+
- [Eris](https://github.com/giorgiosironi/eris) - QuickCheck and property-based testing tools to the PHP and PHPUnit ecosystem.
44+
- [PHP-CS-Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer) - A tool to automatically fix PHP coding standards issues
4245

4346
```
4447
composer test
48+
composer fix
4549
```
4650

4751
## Use Cases
@@ -51,7 +55,7 @@ You can find more use cases and examples in the [example directory](/example/).
5155
Monad is Functor and Applicative. You could say that Monad implements Functor and Applicative.
5256

5357
### List Functor
54-
``` php
58+
```php
5559
use Widmogrod\Functional as f;
5660
use Widmogrod\Primitive\Listt;
5761

@@ -72,11 +76,11 @@ assert($result === f\fromIterable([2, 3, 4]));
7276
Apply function on list of values and as a result, receive list of all possible combinations
7377
of applying function from the left list to a value in the right one.
7478

75-
``` haskell
79+
```haskell
7680
[(+3),(+4)] <*> [1, 2] == [4, 5, 5, 6]
7781
```
7882

79-
``` php
83+
```php
8084
use Widmogrod\Functional as f;
8185
use Widmogrod\Primitive\Listt;
8286

@@ -104,7 +108,7 @@ Using Maybe as an instance of Monoid simplifies concat and reduce operations by
104108
Extracting from a list of uneven values can be tricky and produce nasty code full of `if (isset)` statements.
105109
By combining List and Maybe Monad, this process becomes simpler and more readable.
106110

107-
``` php
111+
```php
108112
use Widmogrod\Monad\Maybe;
109113
use Widmogrod\Primitive\Listt;
110114

@@ -138,7 +142,7 @@ This results in nasty `try catch` blocks and many of if statements.
138142
Either Monad shows how we can fail gracefully without breaking the execution chain and making the code more readable.
139143
The following example demonstrates combining the contents of two files into one. If one of those files does not exist the operation fails gracefully.
140144

141-
``` php
145+
```php
142146
use Widmogrod\Functional as f;
143147
use Widmogrod\Monad\Either;
144148

@@ -164,7 +168,7 @@ assert($concat->extract() === 'File "aaa" does not exists');
164168
### IO Monad
165169
Example usage of `IO Monad`. Read input from `stdin`, and print it to `stdout`.
166170

167-
``` php
171+
```php
168172
use Widmogrod\Monad\IO as IO;
169173
use Widmogrod\Functional as f;
170174

@@ -176,7 +180,7 @@ $readFromInput(Monad\Identity::of('Enter something and press <enter>'))->run();
176180
### Writer Monad
177181
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.
178182

179-
``` php
183+
```php
180184

181185
use Widmogrod\Monad\Writer as W;
182186
use Widmogrod\Functional as f;
@@ -202,7 +206,7 @@ list($result, $log) = f\filterM($filter, $data)->runWriter();
202206

203207
The `Reader monad` provides a way to share a common environment, such as configuration information or class instances, across multiple functions.
204208

205-
``` php
209+
```php
206210

207211
use Widmogrod\Monad\Reader as R;
208212
use Widmogrod\Functional as f;
@@ -276,22 +280,57 @@ $result = $scenario->Run([
276280
]);
277281
```
278282

279-
### Sequencing Monad operations
280-
This variant of `sequence_` ignores the result.
283+
## Haskell `do notation` in PHP
284+
Why Haskell's do notation is interesting?
281285

282-
``` php
283-
use Widmogrod\Monad\IO as IO;
284-
use Widmogrod\Functional as f;
286+
In Haskell is just an "syntax sugar" and in many ways is not needed,
287+
but in PHP control flow of monads can be hard to track.
288+
289+
Consider example, that use only chaining `bind()`
290+
and compare it to the same version but with `do notation` in PHP.
291+
292+
### Control flow without do notation
285293

286-
f\sequence_([
287-
IO\putStrLn('Your name:'),
288-
IO\getLine(),
289-
IO\putStrLn('Your surname:'),
290-
IO\getLine(),
291-
IO\putStrLn('Thank you'),
292-
])->run();
294+
```php
295+
$result = Identity::of(1)
296+
->bind(function ($a) {
297+
return Identity::of(3)
298+
->bind(function ($b) use ($a) {
299+
return Identity::of($a + $b)
300+
->bind(function ($c) {
301+
return Identity::of($c * $c);
302+
});
303+
});
304+
});
305+
306+
$this->assertEquals(Identity::of(16), $result);
293307
```
294308

309+
### Control flow with do notation
310+
311+
```php
312+
$result = doo(
313+
let('a', Identity::of(1)),
314+
let('b', Identity::of(3)),
315+
let('c', in(['a', 'b'], function (int $a, int $b): Identity {
316+
return Identity::of($a + $b);
317+
})),
318+
in(['c'], function (int $c): Identity {
319+
return Identity::of($c * $c);
320+
})
321+
);
322+
323+
assert($result === Identity::of(16));
324+
```
325+
326+
Everyone needs to judge by itself, but in my opinion `do notation`improve readability of code in PHP.
327+
328+
#### Book `Functional PHP` by Gilles Crettenand <a href="https://www.packtpub.com/application-development/functional-php"><img align="right" height="160" src="functional-php.png"></a>
329+
330+
In recently published book [`Functional PHP` by Gilles Crettenand](https://www.packtpub.com/application-development/functional-php), 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.
331+
332+
[Buy the book at PacktPub](https://www.packtpub.com/application-development/functional-php)
333+
295334
## References
296335
Here links to their articles`/`libraries that help me understood the domain:
297336
* http://drboolean.gitbooks.io/mostly-adequate-guide

functional-php.png

43.6 KB
Loading

0 commit comments

Comments
 (0)