Skip to content

Commit 997e4a4

Browse files
committed
Reworked function internals
1 parent 57528ab commit 997e4a4

6 files changed

Lines changed: 81 additions & 68 deletions

File tree

src/Functional/Internal/_Partial.php

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
namespace Chemem\Bingo\Functional\Internal;
1212

13-
require_once __DIR__ . '/_Merge.php';
14-
require_once __DIR__ . '/_Size.php';
13+
require_once __DIR__ . '/_Fold.php';
1514

1615
const _partial = __NAMESPACE__ . '\\_partial';
1716

@@ -34,20 +33,54 @@ function _partial(
3433
): callable {
3534
$argCount = (new \ReflectionFunction($func))
3635
->getNumberOfRequiredParameters();
36+
$merge = function (...$item): array {
37+
return _fold(
38+
function (array $acc, $value) {
39+
if (\is_array($value)) {
40+
foreach ($value as $entry) {
41+
$acc['size'] += 1;
42+
$acc['acc'][] = $entry;
43+
}
44+
}
3745

38-
$acc = function (...$inner) use (&$acc, $func, $argCount, $left) {
46+
return $acc;
47+
},
48+
$item,
49+
[
50+
'size' => 0,
51+
'acc' => [],
52+
]
53+
);
54+
};
55+
$acc = function (...$inner) use (
56+
&$acc,
57+
$func,
58+
$argCount,
59+
$left,
60+
$merge
61+
): callable {
3962
return function (...$innermost) use (
4063
$inner,
4164
$acc,
4265
$func,
4366
$left,
44-
$argCount
67+
$argCount,
68+
$merge
4569
) {
46-
$final = $left ?
47-
_merge(false, $inner, $innermost) :
48-
_merge(false, \array_reverse($innermost), \array_reverse($inner));
70+
[
71+
'size' => $size,
72+
'acc' => $final
73+
] = $merge(
74+
false,
75+
$left ?
76+
$inner :
77+
\array_reverse($innermost),
78+
$left ?
79+
$innermost :
80+
\array_reverse($inner)
81+
);
4982

50-
if ($argCount <= _size($final)) {
83+
if ($argCount <= $size) {
5184
return $func(...$final);
5285
}
5386

src/Functional/Intersects.php

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,14 @@
2828
*/
2929
function intersects($first, $second): bool
3030
{
31-
if (\is_object($first) && \is_object($second)) {
32-
$first = \get_object_vars($first);
33-
$second = \get_object_vars($second);
34-
}
35-
36-
$fsize = size($first);
37-
$ssize = size($second);
38-
$intersect = false;
39-
40-
if ($fsize > $ssize) {
41-
foreach ($second as $val) {
42-
if (has($first, $val)) {
43-
$intersect = true;
44-
45-
if ($intersect == true) {
46-
break;
47-
}
48-
}
49-
}
50-
} else {
51-
foreach ($first as $val) {
52-
if (has($second, $val)) {
53-
$intersect = true;
31+
$intersects = false;
5432

55-
if ($intersect == true) {
56-
break;
57-
}
58-
}
33+
foreach ($first as $value) {
34+
if (has($second, $value)) {
35+
$intersects = true;
36+
break;
5937
}
6038
}
6139

62-
return $intersect;
40+
return $intersects;
6341
}

src/Functional/IsArrayOf.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,34 @@
1818
*
1919
* isArrayOf :: [a] -> String
2020
*
21-
* @param array $list
22-
* @return string
21+
* @param array|object $list
22+
* @return string|null
2323
* @example
2424
*
2525
* isArrayOf(['foo', 'bar', 'baz'])
2626
* => 'string'
2727
*/
28-
function isArrayOf($list): string
28+
function isArrayOf($list): ?string
2929
{
30-
$types = fold(
31-
function ($types, $entry) {
32-
$types[] = \gettype($entry);
30+
$cache = [];
31+
$type = null;
32+
$idx = 0;
3333

34-
return unique($types);
35-
},
36-
$list,
37-
[]
38-
);
34+
foreach ($list as $value) {
35+
$tmp = \gettype($value);
3936

40-
return size($types) > 1 ? 'mixed' : head($types);
37+
if ($idx++ > 0) {
38+
if (\in_array($tmp, $cache)) {
39+
$type = $tmp;
40+
} else {
41+
$type = 'mixed';
42+
break;
43+
}
44+
} else {
45+
$cache[] = $tmp;
46+
$type = $tmp;
47+
}
48+
}
49+
50+
return $type;
4151
}

src/Functional/Tail.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,5 @@
2727
*/
2828
function tail($list)
2929
{
30-
[, $final] = fold(function ($acc, $val) {
31-
[$count, $lst] = $acc;
32-
$count = $count + 1;
33-
34-
if ($count < 2) {
35-
if (\is_object($lst)) {
36-
unset($lst->{indexOf($lst, $val)});
37-
} else {
38-
if (\is_array($lst)) {
39-
unset($lst[indexOf($lst, $val)]);
40-
}
41-
}
42-
}
43-
44-
return [$count, $lst];
45-
}, $list, [0, $list]);
46-
47-
return $final;
30+
return dropLeft($list, 1);
4831
}

tests/Functional/IsArrayOfTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public static function contextProvider()
1414
[[[2], ['foo']], 'array'],
1515
[[1.2, 3.2], 'double'],
1616
[[3, 'foo', new \stdClass(2)], 'mixed'],
17+
[(object) [], null],
18+
[[], null],
19+
[(object) ['foo' => 'qux', 'bar' => 'quux'], 'string'],
1720
];
1821
}
1922

tests/Functional/TailTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ class TailTest extends \PHPUnit\Framework\TestCase
99
public static function contextProvider()
1010
{
1111
return [
12-
[(object) \range(1, 3), (object) [1 => 2, 2 => 3]],
13-
[['foo', 'bar'], [1 => 'bar']],
12+
[
13+
(object) \range(1, 3),
14+
(object) [1 => 2, 2 => 3],
15+
],
16+
[
17+
['foo', 'bar'],
18+
[1 => 'bar'],
19+
],
1420
];
1521
}
1622

0 commit comments

Comments
 (0)