Skip to content

Commit 904fbb1

Browse files
committed
Refactor README into docs.
1 parent 3c464aa commit 904fbb1

13 files changed

Lines changed: 3919 additions & 3851 deletions

README.md

Lines changed: 187 additions & 3851 deletions
Large diffs are not rendered by default.

docs/file-iteration.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# File Iteration
2+
3+
[Back to main README](../README.md)
4+
5+
Tools for iterating over file contents.
6+
7+
---
8+
9+
### Read CSV
10+
Iterate the lines of a CSV file.
11+
12+
```File::readCsv(resource $fileHandle, string $separator = ',', string $enclosure = '"', string $escape = '\\')```
13+
14+
```php
15+
use IterTools\File;
16+
17+
$fileHandle = \fopen('path/to/file.csv', 'r');
18+
19+
foreach (File::readCsv($fileHandle) as $row) {
20+
print_r($row);
21+
}
22+
// Each column field is an element of the array
23+
```
24+
25+
### Read Lines
26+
Iterate the lines of a file.
27+
28+
```File::readLines(resource $fileHandle)```
29+
30+
```php
31+
use IterTools\File;
32+
33+
$fileHandle = \fopen('path/to/file.txt', 'r');
34+
35+
foreach (File::readLines($fileHandle) as $line) {
36+
print($line);
37+
}
38+
```

docs/infinite-iteration.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Infinite Iteration
2+
3+
[Back to main README](../README.md)
4+
5+
Tools for creating infinite iterators.
6+
7+
---
8+
9+
### Count
10+
Count sequentially forever.
11+
12+
```Infinite::count(int $start = 1, int $step = 1)```
13+
14+
```php
15+
use IterTools\Infinite;
16+
17+
$start = 1;
18+
$step = 1;
19+
20+
foreach (Infinite::count($start, $step) as $i) {
21+
print($i);
22+
}
23+
// 1, 2, 3, 4, 5 ...
24+
```
25+
26+
### Cycle
27+
Cycle through the elements of a collection sequentially forever.
28+
29+
```Infinite::cycle(iterable $iterable)```
30+
31+
```php
32+
use IterTools\Infinite;
33+
34+
$hands = ['rock', 'paper', 'scissors'];
35+
36+
foreach (Infinite::cycle($hands) as $hand) {
37+
RockPaperScissors::playHand($hand);
38+
}
39+
// rock, paper, scissors, rock, paper, scissors, ...
40+
```
41+
42+
### Repeat (Infinite)
43+
Repeat an item forever.
44+
45+
```Infinite::repeat(mixed $item)```
46+
47+
```php
48+
use IterTools\Infinite;
49+
50+
$dialogue = 'Are we there yet?';
51+
52+
foreach (Infinite::repeat($dialogue) as $repeated) {
53+
print($repeated);
54+
}
55+
// 'Are we there yet?', 'Are we there yet?', 'Are we there yet?', ...
56+
```

docs/math-iteration.md

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# Math Iteration
2+
3+
[Back to main README](../README.md)
4+
5+
Tools for mathematical iteration operations.
6+
7+
---
8+
9+
### Frequencies
10+
Returns a frequency distribution of the data.
11+
12+
```Math::frequencies(iterable $data, bool $strict = true): \Generator```
13+
14+
Defaults to [strict type](../README.md#strict-and-coercive-types) comparisons. Set strict to false for type coercion comparisons.
15+
16+
```php
17+
use IterTools\Math;
18+
19+
$grades = ['A', 'A', 'B', 'B', 'B', 'C'];
20+
21+
foreach (Math::frequencies($grades) as $grade => $frequency) {
22+
print("$grade: $frequency" . \PHP_EOL);
23+
}
24+
// A: 2, B: 3, C: 1
25+
```
26+
27+
### Relative Frequencies
28+
Returns a relative frequency distribution of the data.
29+
30+
```Math::relativeFrequencies(iterable $data, bool $strict = true): \Generator```
31+
32+
Defaults to [strict type](../README.md#strict-and-coercive-types) comparisons. Set strict to false for type coercion comparisons.
33+
34+
```php
35+
use IterTools\Math;
36+
37+
$grades = ['A', 'A', 'B', 'B', 'B', 'C'];
38+
39+
foreach (Math::relativeFrequencies($grades) as $grade => $frequency) {
40+
print("$grade: $frequency" . \PHP_EOL);
41+
}
42+
// A: 0.33, B: 0.5, C: 0.166
43+
```
44+
45+
### Running Average
46+
Accumulate the running average over a list of numbers.
47+
48+
```Math::runningAverage(iterable $numbers, int|float $initialValue = null)```
49+
50+
```php
51+
use IterTools\Math;
52+
53+
$grades = [100, 80, 80, 90, 85];
54+
55+
foreach (Math::runningAverage($grades) as $runningAverage) {
56+
print($runningAverage);
57+
}
58+
// 100, 90, 86.667, 87.5, 87
59+
```
60+
61+
### Running Difference
62+
Accumulate the running difference over a list of numbers.
63+
64+
```Math::runningDifference(iterable $numbers, int|float $initialValue = null)```
65+
66+
```php
67+
use IterTools\Math;
68+
69+
$credits = [1, 2, 3, 4, 5];
70+
71+
foreach (Math::runningDifference($credits) as $runningDifference) {
72+
print($runningDifference);
73+
}
74+
// -1, -3, -6, -10, -15
75+
```
76+
Provide an optional initial value to lead off the running difference.
77+
```php
78+
use IterTools\Math;
79+
80+
$dartsScores = [50, 50, 25, 50];
81+
$startingScore = 501;
82+
83+
foreach (Math::runningDifference($dartsScores, $startingScore) as $runningScore) {
84+
print($runningScore);
85+
}
86+
// 501, 451, 401, 376, 326
87+
```
88+
89+
### Running Max
90+
Accumulate the running maximum over a list of numbers.
91+
92+
```Math::runningMax(iterable $numbers, int|float $initialValue = null)```
93+
94+
```php
95+
use IterTools\Math;
96+
97+
$numbers = [1, 2, 1, 3, 5];
98+
99+
foreach (Math::runningMax($numbers) as $runningMax) {
100+
print($runningMax);
101+
}
102+
// 1, 2, 2, 3, 5
103+
```
104+
105+
### Running Min
106+
Accumulate the running minimum over a list of numbers.
107+
108+
```Math::runningMin(iterable $numbers, int|float $initialValue = null)```
109+
110+
```php
111+
use IterTools\Math;
112+
113+
$numbers = [3, 4, 2, 5, 1];
114+
115+
foreach (Math::runningMin($numbers) as $runningMin) {
116+
print($runningMin);
117+
}
118+
// 3, 3, 2, 2, 1
119+
```
120+
121+
### Running Product
122+
Accumulate the running product over a list of numbers.
123+
124+
```Math::runningProduct(iterable $numbers, int|float $initialValue = null)```
125+
126+
```php
127+
use IterTools\Math;
128+
129+
$numbers = [1, 2, 3, 4, 5];
130+
131+
foreach (Math::runningProduct($numbers) as $runningProduct) {
132+
print($runningProduct);
133+
}
134+
// 1, 2, 6, 24, 120
135+
```
136+
137+
Provide an optional initial value to lead off the running product.
138+
```php
139+
use IterTools\Math;
140+
141+
$numbers = [1, 2, 3, 4, 5];
142+
$initialValue = 5;
143+
144+
foreach (Math::runningProduct($numbers, $initialValue) as $runningProduct) {
145+
print($runningProduct);
146+
}
147+
// 5, 5, 10, 30, 120, 600
148+
```
149+
150+
### Running Total
151+
Accumulate the running total over a list of numbers.
152+
153+
```Math::runningTotal(iterable $numbers, int|float $initialValue = null)```
154+
155+
```php
156+
use IterTools\Math;
157+
158+
$prices = [1, 2, 3, 4, 5];
159+
160+
foreach (Math::runningTotal($prices) as $runningTotal) {
161+
print($runningTotal);
162+
}
163+
// 1, 3, 6, 10, 15
164+
```
165+
166+
Provide an optional initial value to lead off the running total.
167+
```php
168+
use IterTools\Math;
169+
170+
$prices = [1, 2, 3, 4, 5];
171+
$initialValue = 5;
172+
173+
foreach (Math::runningTotal($prices, $initialValue) as $runningTotal) {
174+
print($runningTotal);
175+
}
176+
// 5, 6, 8, 11, 15, 20
177+
```

docs/multi-iteration.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Multi Iteration
2+
3+
[Back to main README](../README.md)
4+
5+
Tools for iterating multiple collections simultaneously.
6+
7+
---
8+
9+
### Chain
10+
Chain multiple iterables together into a single continuous sequence.
11+
12+
```Multi::chain(iterable ...$iterables)```
13+
```php
14+
use IterTools\Multi;
15+
16+
$prequels = ['Phantom Menace', 'Attack of the Clones', 'Revenge of the Sith'];
17+
$originals = ['A New Hope', 'Empire Strikes Back', 'Return of the Jedi'];
18+
19+
foreach (Multi::chain($prequels, $originals) as $movie) {
20+
print($movie);
21+
}
22+
// 'Phantom Menace', 'Attack of the Clones', 'Revenge of the Sith', 'A New Hope', 'Empire Strikes Back', 'Return of the Jedi'
23+
```
24+
25+
### Zip
26+
Iterate multiple iterable collections simultaneously.
27+
28+
```Multi::zip(iterable ...$iterables)```
29+
```php
30+
use IterTools\Multi;
31+
32+
$languages = ['PHP', 'Python', 'Java', 'Go'];
33+
$mascots = ['elephant', 'snake', 'bean', 'gopher'];
34+
35+
foreach (Multi::zip($languages, $mascots) as [$language, $mascot]) {
36+
print("The {$language} language mascot is an {$mascot}.");
37+
}
38+
// The PHP language mascot is an elephant.
39+
// ...
40+
```
41+
42+
Zip works with multiple iterable inputs--not limited to just two.
43+
```php
44+
$names = ['Ryu', 'Ken', 'Chun Li', 'Guile'];
45+
$countries = ['Japan', 'USA', 'China', 'USA'];
46+
$signatureMoves = ['hadouken', 'shoryuken', 'spinning bird kick', 'sonic boom'];
47+
48+
foreach (Multi::zip($names, $countries, $signatureMoves) as [$name, $country, $signatureMove]) {
49+
$streetFighter = new StreetFighter($name, $country, $signatureMove);
50+
}
51+
```
52+
Note: For uneven lengths, iteration stops when the shortest iterable is exhausted.
53+
54+
### ZipEqual
55+
Iterate multiple iterable collections with equal lengths simultaneously.
56+
57+
Throws `\LengthException` if lengths are not equal, meaning that at least one iterator ends before the others.
58+
59+
```Multi::zipEqual(iterable ...$iterables)```
60+
61+
```php
62+
use IterTools\Multi;
63+
64+
$letters = ['A', 'B', 'C'];
65+
$numbers = [1, 2, 3];
66+
67+
foreach (Multi::zipEqual($letters, $numbers) as [$letter, $number]) {
68+
// ['A', 1], ['B', 2], ['C', 3]
69+
}
70+
```
71+
72+
### ZipFilled
73+
Iterate multiple iterable collections simultaneously, using a default filler value if lengths are not equal.
74+
75+
```Multi::zipFilled(mixed $filler, iterable ...$iterables)```
76+
77+
```php
78+
use IterTools\Multi;
79+
80+
$default = '?';
81+
$letters = ['A', 'B'];
82+
$numbers = [1, 2, 3];
83+
84+
foreach (Multi::zipFilled($default, $letters, $numbers) as [$letter, $number]) {
85+
// ['A', 1], ['B', 2], ['?', 3]
86+
}
87+
```
88+
89+
### ZipLongest
90+
Iterate multiple iterable collections simultaneously.
91+
92+
```Multi::zipLongest(iterable ...$iterables)```
93+
94+
For uneven lengths, the exhausted iterables will produce `null` for the remaining iterations.
95+
96+
```php
97+
use IterTools\Multi;
98+
99+
$letters = ['A', 'B', 'C'];
100+
$numbers = [1, 2];
101+
102+
foreach (Multi::zipLongest($letters, $numbers) as [$letter, $number]) {
103+
// ['A', 1], ['B', 2], ['C', null]
104+
}
105+
```

0 commit comments

Comments
 (0)