Unverified Commit ff118eb2 authored by Arkadiusz Kondas's avatar Arkadiusz Kondas Committed by GitHub
Browse files

Implement LambdaTransformer (#381)

parent c1c9873b
<?php
declare(strict_types=1);
namespace Phpml\Preprocessing;
final class LambdaTransformer implements Preprocessor
{
/**
* @var callable
*/
private $lambda;
public function __construct(callable $lambda)
{
$this->lambda = $lambda;
}
public function fit(array $samples, ?array $targets = null): void
{
// nothing to do
}
public function transform(array &$samples, ?array &$targets = null): void
{
foreach ($samples as &$sample) {
$sample = call_user_func($this->lambda, $sample);
}
}
}
<?php
declare(strict_types=1);
namespace Phpml\Tests\Preprocessing;
use Phpml\Preprocessing\LambdaTransformer;
use PHPUnit\Framework\TestCase;
final class LambdaTransformerTest extends TestCase
{
public function testLambdaSampleTransformation(): void
{
$transformer = new LambdaTransformer(static function ($sample): int {
return $sample[0] + $sample[1];
});
$samples = [
[1, 2],
[3, 4],
[5, 6],
];
$transformer->transform($samples);
self::assertEquals([3, 7, 11], $samples);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment