Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: apcu
ini-values: apc.enable_cli=1
- name: Validate composer.json and composer.lock
run: composer validate
- name: Get Composer cache directory
Expand Down
7 changes: 6 additions & 1 deletion changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## v2.5.0

- Added `Chemem\Bingo\Functional\listFromPaths`
- Added the following function(s)
- `Chemem\Bingo\Functional\listFromPaths`
- Added nullable type definitions to arguments in the following functions
- `Chemem\Bingo\Functional\paths`
- `Chemem\Bingo\Functional\toException`
Expand All @@ -23,6 +24,10 @@
- `Chemem\Bingo\Functional\fromPairs`
- `Chemem\Bingo\Functional\has`
- `Chemem\Bingo\Functional\tail`
- `Chemem\Bingo\Functional\intersperse`
- `Chemem\Bingo\Functional\Functors\Monads\mapM`
- `Chemem\Bingo\Functional\Functors\Monads\filterM`
- `Chemem\Bingo\Functional\Functors\Monads\foldM`
- Improved iteration patterns in functions subsumed in the following classes
- `Chemem\Bingo\Functional\Immutable\Collection`
- `Chemem\Bingo\Functional\Immutable\Tuple`
Expand Down
4 changes: 2 additions & 2 deletions src/Functional/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ function filter(callable $func, $list, int $mode = 0)
{
return fold(
function ($acc, $val, $idx) use ($func, $mode) {
$filter = equals($mode, ARRAY_FILTER_USE_KEY) ?
$filter = equals($mode, \ARRAY_FILTER_USE_KEY) ?
$func($idx) :
(
equals($mode, ARRAY_FILTER_USE_BOTH) ?
equals($mode, \ARRAY_FILTER_USE_BOTH) ?
$func($val, $idx) :
$func($val)
);
Expand Down
17 changes: 13 additions & 4 deletions src/Functional/Intersperse.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@
* intersperse('foo', range(1, 3));
* => [1, 'foo', 2, 'foo', 3]
*/
function intersperse($element, array $list): array
function intersperse($element, $list): array
{
$elem = \array_pad([], \count($list), $element);
$res = \array_merge(...\array_map(null, $list, $elem));
$acc = [];

return dropRight($res, 1);
foreach ($list as $key => $value) {
if (\is_string($key) && !\is_numeric($key)) {
$acc[$key] = $value;
} else {
$acc[] = $value;
}

$acc[] = $element;
}

return dropRight($acc, 1);
}
4 changes: 3 additions & 1 deletion src/Functional/Memoize.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public function __construct(callable $function, bool $apc)

public function __invoke(...$args)
{
$key = \md5(
$key = \sprintf(
'%s\\memoize::%s',
__NAMESPACE__,
\extension_loaded('igbinary') ?
\igbinary_serialize($args) :
\serialize($args)
Expand Down
3 changes: 1 addition & 2 deletions src/Functors/Monads/FilterM.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use function Chemem\Bingo\Functional\equals;
use function Chemem\Bingo\Functional\extend;
use function Chemem\Bingo\Functional\head;
use function Chemem\Bingo\Functional\size;
use function Chemem\Bingo\Functional\tail;

const filterM = __NAMESPACE__ . '\\filterM';
Expand All @@ -34,7 +33,7 @@ function filterM(callable $function, $list): Monad
$monad = $function(head($list));

$filter = function ($collection) use (&$filter, $function, $monad) {
if (equals(size($collection), 0)) {
if (!$collection) {
return $monad::of($collection);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Functors/Monads/FoldM.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use function Chemem\Bingo\Functional\equals;
use function Chemem\Bingo\Functional\head;
use function Chemem\Bingo\Functional\size;
use function Chemem\Bingo\Functional\tail;

const foldM = __NAMESPACE__ . '\\foldM';
Expand All @@ -32,7 +31,7 @@ function foldM(callable $function, $list, $acc): Monad
$monad = $function($acc, head($list));

$fold = function ($acc, $collection) use (&$fold, $monad, $function) {
if (equals(size($collection), 0)) {
if (!$collection) {
return $monad::of($acc);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Functors/Monads/MapM.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use function Chemem\Bingo\Functional\equals;
use function Chemem\Bingo\Functional\extend;
use function Chemem\Bingo\Functional\head;
use function Chemem\Bingo\Functional\size;
use function Chemem\Bingo\Functional\tail;

const mapM = __NAMESPACE__ . '\\mapM';
Expand All @@ -33,7 +32,7 @@ function mapM(callable $function, $list): Monad
$monad = $function(head($list));

$map = function ($collection) use (&$map, $function, $monad) {
if (equals(size($collection), 0)) {
if (!$collection) {
return $monad::of($collection);
}

Expand Down
30 changes: 30 additions & 0 deletions tests/Functional/IntersperseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,36 @@ public static function contextProvider()
return [
[\range(1, 4), 'foo', [1, 'foo', 2, 'foo', 3, 'foo', 4]],
[['foo', 'bar'], 2, ['foo', 2, 'bar']],
[
(object) [
'foo' => 3.332,
'bar' => 'quux',
'baz' => null,
],
false,
[
'foo' => 3.332,
'0' => false,
'bar' => 'quux',
'1' => false,
'baz' => null,
],
],
[
(object) [
false,
null,
\range(1, 3),
],
'qux',
[
false,
'qux',
null,
'qux',
\range(1, 3),
],
],
];
}

Expand Down
80 changes: 67 additions & 13 deletions tests/Functional/MemoizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,83 @@ public static function contextProvider()
{
return [
[
$fact = function ($val) use (&$fact) {
return $val < 2 ? 1 : $val * $fact($val - 1);
},
15,
1307674368000,
[
function (int $x): int {
return $x * 2;
},
true,
],
[10],
20,
],
[
$fib = function ($val) use (&$fib) {
return $val < 2 ? $val : $fib($val - 2) + $fib($val - 1);
},
11,
89,
[
function (int $x): int {
return $x * 2;
},
true,
],
[10],
20,
],
[
[
function (int $x, int $y = 3): int {
return $x + $y;
},
],
[20, 3],
23,
],
[
[
function (int $x, int $y = 3): int {
return $x + $y;
},
],
[20, 3],
23,
],
];
}

/**
* @dataProvider contextProvider
*/
public function testmemoizeCachesFunction($func, $arg, $res)
public function testmemoizeCachesFunction($memoargs, $fargs, $res)
{
$memoized = f\memoize($func);
$memoized = f\memoize(...$memoargs);

$this->assertEquals($res, $memoized(...$fargs));

if (!\extension_loaded('apcu')) {
$this->assertNotEmpty(
f\filter(
function (string $key): bool {
return (bool) \preg_match(
'/^(Chemem\\\\Bingo\\\\Functional\\\\memoize)/',
$key
);
},
$GLOBALS,
\ARRAY_FILTER_USE_KEY
)
);
} else {
$acc = [];

foreach (new \APCUIterator() as $key => $value) {
if (
(bool) \preg_match(
'/^(Chemem\\\\Bingo\\\\Functional\\\\memoize)/',
$key
)
) {
$acc[$key] = $value;
}
}

$this->assertEquals($res, $memoized($arg));
$this->assertNotEmpty($acc);
}
}
}