Skip to content

Commit 067e8d5

Browse files
committed
docs: add toc, higher order msg
1 parent 43522a6 commit 067e8d5

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

readme.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ Although all of them are available with helper function `underscore($data)` or `
2323
the methods are grouped and organized in different heriarchy and classes according as their scope.
2424
This keeps it maintainable and saves from having a God class.
2525

26+
#### Contents
27+
28+
- [Underscore](#underscore)
29+
- [UnderscoreFunction](#underscorefunction)
30+
- [UnderscoreArray](#underscorearray)
31+
- [UnderscoreCollection](#underscorecollection)
32+
- [UnderscoreBase](#underscorebase)
33+
- [HigherOrderMessage](#higherordermessage)
34+
- [ArrayAccess](#arrayaccess)
35+
- [Arrayizes](#arrayizes)
36+
37+
2638
---
2739
### Underscore
2840

@@ -865,6 +877,56 @@ Alias of <a href="#unique">unique()</a>.
865877

866878
Alias of <a href="#difference">difference()</a>.
867879

880+
---
881+
### HigherOrderMessage
882+
883+
A syntatic sugar to use elegant shorthand oneliner for complex logic often wrapped in closures.
884+
See example below:
885+
886+
```php
887+
// Higher Order Messaging
888+
class HOM
889+
{
890+
protected $n;
891+
public $square;
892+
893+
public function __construct($n)
894+
{
895+
$this->n = $n;
896+
$this->square = $n * $n;
897+
}
898+
899+
public function even()
900+
{
901+
return $this->n % 2 === 0;
902+
}
903+
}
904+
905+
$u = [new HOM(1), new HOM(2), new HOM(3), new HOM(4)];
906+
907+
// Filter `even()` items
908+
$evens = $u->filter->even(); // 'even()' method of each items!
909+
910+
// Map each evens to their squares
911+
$squares = $evens->map->square; // 'square' prop of each items!
912+
// Gives an Underscore instance
913+
914+
// Get the data
915+
$squares->get();
916+
// [1 => 4, 3 => 16]
917+
```
918+
919+
Without higher order messaging that would look like:
920+
921+
```php
922+
$evens = $u->filter(function ($it) {
923+
return $it->even();
924+
});
925+
926+
$squares = $evens->map(function ($it) {
927+
return $it->square;
928+
});
929+
```
868930

869931
---
870932
### \ArrayAccess

0 commit comments

Comments
 (0)