Skip to content

Commit

Permalink
Merge pull request #30 from pitchart/feature/length
Browse files Browse the repository at this point in the history
Adds support for length
  • Loading branch information
pitchart authored Mar 31, 2020
2 parents 22ed84a + 3ba7ed0 commit c74d579
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Reducer/Termination/Length.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php


namespace Pitchart\Transformer\Reducer\Termination;


use Pitchart\Transformer\Termination;

class Length implements Termination
{
public function init()
{
return 0;
}

public function step($result, $current)
{
return ++$result;
}

public function complete($result)
{
return $result;
}
}
5 changes: 5 additions & 0 deletions src/Transducer/transducers.php
Original file line number Diff line number Diff line change
Expand Up @@ -629,3 +629,8 @@ function to_operation(string $operator)
{
return new Reducer\Termination\Operation($operator);
}

function length()
{
return new Reducer\Termination\Length();
}
8 changes: 8 additions & 0 deletions src/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,14 @@ public function concat()
return $this->terminate(t\to_operation('.'));
}

/**
* @return string
*/
public function length()
{
return $this->terminate(t\length());
}

/**
* Processes transducing with a Termination
*
Expand Down
38 changes: 38 additions & 0 deletions tests/Reducer/Termination/LengthTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Pitchart\Transformer\Tests\Reducer\Termination;

use Pitchart\Transformer\Reducer;
use Pitchart\Transformer\Reducer\Termination\Length;
use PHPUnit\Framework\TestCase;
use Pitchart\Transformer\Termination;
use function Pitchart\Transformer\transform;

class LengthTest extends TestCase
{
public function test_is_a_reducer()
{
$length = new Length;
self::assertInstanceOf(Reducer::class, $length);
}

public function test_is_a_termination()
{
$length = new Length;
self::assertInstanceOf(Termination::class, $length);
}

public function test_is_zero_if_collection_is_empty()
{
$length = transform([])->length();

$this->assertEquals(0, $length);
}

public function test_returns_collection_size_for_not_empty_collections()
{
$length = transform([1, 2, 3, 4])->length();

$this->assertEquals(4, $length);
}
}

0 comments on commit c74d579

Please sign in to comment.