Skip to content

Commit 0671a72

Browse files
committed
upd
1 parent 9cc09ea commit 0671a72

File tree

2 files changed

+70
-60
lines changed

2 files changed

+70
-60
lines changed

src/Lists.php

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Php\Pairs\Data\Lists;
44

5+
use Closure;
6+
57
use function Php\Pairs\Pairs\cons as pairsCons;
68
use function Php\Pairs\Pairs\car;
79
use function Php\Pairs\Pairs\cdr;
@@ -16,7 +18,7 @@
1618
* isList(false); // false
1719
* isList('hello'); // false
1820
*/
19-
function isList($mix)
21+
function isList(mixed $mix): bool
2022
{
2123
if ($mix === null) {
2224
return true;
@@ -29,7 +31,12 @@ function isList($mix)
2931
return false;
3032
}
3133

32-
function checkList($list)
34+
/**
35+
* @param mixed $list
36+
* @return void
37+
* @throws \Exception
38+
*/
39+
function checkList(mixed $list): void
3340
{
3441
if (!isList($list)) {
3542
if (isPair($list)) {
@@ -47,9 +54,9 @@ function checkList($list)
4754
/**
4855
* Creates new list with given $elements
4956
* @param mixed[] $elements elements to add
50-
* @return callable list
57+
* @return Closure list
5158
*/
52-
function l(...$elements)
59+
function l(...$elements): ?Closure
5360
{
5461
return array_reduce(array_reverse($elements), fn($acc, $item) => cons($item, $acc));
5562
}
@@ -59,32 +66,32 @@ function l(...$elements)
5966
* @example
6067
* cons(5, l(1, 0)); // (5, 1, 0)
6168
*/
62-
function cons($element, $list)
69+
function cons($element, $list): Closure
6370
{
6471
checkList($list);
6572
return pairsCons($element, $list);
6673
}
6774

6875
/**
6976
* Get list's head
70-
* @param callable $list
77+
* @param Closure $list
7178
* @return mixed
7279
* @example
7380
* head(l(10, 15, 20)); // 10
7481
*/
75-
function head($list)
82+
function head($list): mixed
7683
{
7784
checkList($list);
7885
return car($list);
7986
}
8087

8188
/**
8289
* Get list's tail
83-
* @param callable $list
90+
* @param Closure $list
8491
* @example
8592
* tail(l(10, 15, 20)); // (15, 20)
8693
*/
87-
function tail($list)
94+
function tail($list): mixed
8895
{
8996
checkList($list);
9097
return cdr($list);
@@ -97,7 +104,7 @@ function tail($list)
97104
* isEmpty(l(0)); // false
98105
* isEmpty(l('a', 5)); // false
99106
*/
100-
function isEmpty($list)
107+
function isEmpty($list): bool
101108
{
102109
checkList($list);
103110
return $list === null;
@@ -110,7 +117,7 @@ function isEmpty($list)
110117
* isEqual(l(), l(8, 3)); // false
111118
* isEqual(l(1, 2, 10), l(1, 2, 10)); // true
112119
*/
113-
function isEqual($list1, $list2)
120+
function isEqual($list1, $list2): bool
114121
{
115122
checkList($list1);
116123
checkList($list2);
@@ -134,7 +141,7 @@ function isEqual($list1, $list2)
134141
* has(numbers, 0); // false
135142
* has(numbers, 'wow'); // false
136143
*/
137-
function has($list, $element)
144+
function has($list, $element): bool
138145
{
139146
checkList($list);
140147
if (isEmpty($list)) {
@@ -149,10 +156,10 @@ function has($list, $element)
149156

150157
/**
151158
* Reverse list $list
152-
* @param callable $list list
153-
* @return callable result
159+
* @param Closure $list list
160+
* @return Closure result
154161
*/
155-
function reverse($list)
162+
function reverse($list): ?Closure
156163
{
157164
$iter = function ($items, $acc) use (&$iter) {
158165
return isEmpty($items) ? $acc : $iter(tail($items), cons(head($items), $acc));
@@ -162,12 +169,12 @@ function reverse($list)
162169
}
163170

164171
/**
165-
* Filters list $list using callable function $func
166-
* @param callable $list list
167-
* @param callable $func function
168-
* @return callable list
172+
* Filters list $list using Closure function $func
173+
* @param Closure $list list
174+
* @param Closure $func function
175+
* @return Closure list
169176
*/
170-
function filter($list, callable $func)
177+
function filter(mixed $list, Closure $func): ?Closure
171178
{
172179
if (isEmpty($list)) {
173180
return l();
@@ -187,7 +194,7 @@ function filter($list, callable $func)
187194
* $numbers = s(3, 4, 3, 5, 5);
188195
* toString($numbers) // '(4, 3, 5)'
189196
*/
190-
function s(...$elements)
197+
function s(...$elements): ?Closure
191198
{
192199
$reversed = array_reverse($elements);
193200
return array_reduce($reversed, fn($acc, $item) => (has($acc, $item) ? $acc : conj($acc, $item)), l());
@@ -200,7 +207,7 @@ function s(...$elements)
200207
* conj($numbers, 5); // (3, 4, 5, 8)
201208
* conj($numbers, 9); // (9, 3, 4, 5, 8)
202209
*/
203-
function conj($list, $element)
210+
function conj(mixed $list, $element): ?Closure
204211
{
205212
return has($list, $element) ? $list : cons($element, $list);
206213
}
@@ -211,19 +218,19 @@ function conj($list, $element)
211218
* $numbers = l(5, 4, 5, 8);
212219
* disj($numbers, 5); // (4, 8)
213220
*/
214-
function disj($list, $element)
221+
function disj($list, $element): ?Closure
215222
{
216223
return filter($list, fn($e) => $e !== $element);
217224
}
218225

219226

220227
/**
221-
* Applies callable function $func to list $list
222-
* @param callable $list list
223-
* @param callable $func function
224-
* @return callable list
228+
* Applies Closure function $func to list $list
229+
* @param Closure $list list
230+
* @param Closure $func function
231+
* @return Closure list
225232
*/
226-
function map($list, callable $func)
233+
function map(mixed $list, Closure $func): ?Closure
227234
{
228235
checkList($list);
229236
if (isEmpty($list)) {
@@ -236,13 +243,13 @@ function map($list, callable $func)
236243
}
237244

238245
/**
239-
* Collapses the list $list using callable function $func
240-
* @param callable $list list
241-
* @param callable $func function
246+
* Collapses the list $list using Closure function $func
247+
* @param Closure $list list
248+
* @param Closure $func function
242249
* @param mixed $acc
243250
* @return mixed
244251
*/
245-
function reduce($list, callable $func, $acc = null)
252+
function reduce(mixed $list, Closure $func, $acc = null): mixed
246253
{
247254
$iter = function ($items, $acc) use (&$iter, $func) {
248255
return isEmpty($items) ? $acc : $iter(tail($items), $func(head($items), $acc));
@@ -260,7 +267,7 @@ function reduce($list, callable $func, $acc = null)
260267
* concat(l(), l(1, 10)); (1, 10)
261268
* concat(l(1, 10), l()); // (1, 10)
262269
*/
263-
function concat($list1, $list2)
270+
function concat(mixed $list1, mixed $list2): ?Closure
264271
{
265272
checkList($list1);
266273
checkList($list2);
@@ -273,10 +280,10 @@ function concat($list1, $list2)
273280

274281
/**
275282
* Returns length of list
276-
* @param callable $list list
283+
* @param Closure $list list
277284
* @return integer
278285
*/
279-
function length($list)
286+
function length(mixed $list): int
280287
{
281288
checkList($list);
282289
if (isEmpty($list) || !isList($list)) {
@@ -294,7 +301,7 @@ function length($list)
294301
* get(1, $numbers); // 4
295302
* get(3, $numbers); // 8
296303
*/
297-
function get(int $index, $list)
304+
function get(int $index, mixed $list): mixed
298305
{
299306
checkList($list);
300307
if ($index === 0) {
@@ -310,7 +317,7 @@ function get(int $index, $list)
310317
* $numbers = l(3, 4, 5, 8);
311318
* random($numbers); // one random item from 3, 4, 5, 8
312319
*/
313-
function random($list)
320+
function random(mixed $list): mixed
314321
{
315322
checkList($list);
316323
$n = rand(0, length($list) - 1);
@@ -324,7 +331,7 @@ function random($list)
324331
* @param callalble $list
325332
* @return string
326333
*/
327-
function toString($list)
334+
function toString(mixed $list): string
328335
{
329336
if (!isList($list)) {
330337
if (isPair($list)) {
@@ -338,15 +345,15 @@ function toString($list)
338345
return '()';
339346
}
340347

341-
$rec = function ($p) use (&$rec) {
348+
$iter = function ($p) use (&$iter) {
342349
$first = head($p);
343350
$rest = tail($p);
344351
if (isEmpty($rest)) {
345352
return toString($first);
346353
}
347354

348-
return toString($first) . ', ' . $rec($rest);
355+
return toString($first) . ', ' . $iter($rest);
349356
};
350357

351-
return '(' . $rec($list) . ')';
358+
return '(' . $iter($list) . ')';
352359
}

0 commit comments

Comments
 (0)