Skip to content

Commit

Permalink
Functional\drop test + implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
widmogrod committed Dec 21, 2017
1 parent ce9b2e0 commit 0c43a64
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Functional/listt.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

const fromIterable = 'Widmogrod\Functional\fromIterable';

// TODO extract
class SnapshotIterator extends \IteratorIterator
{
private $inMemoryValid;
Expand Down
33 changes: 27 additions & 6 deletions src/Functional/sublist.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@
* take n, applied to a list xs, returns the prefix of xs of length n, or xs itself if n > length xs:
*
* @param int $n
* @param Listt $a
* @param Listt $xs
* @return Listt
*/
function take(int $n, Listt $a = null)
function take(int $n, Listt $xs = null)
{
return curryN(2, function (int $n, Listt $a): Listt {
return curryN(2, function (int $n, Listt $xs): Listt {
if ($n < 1) {
return fromNil();
}

if ($n > length($xs)) {
return $xs;
}

try {
return prepend(head($a), take($n - 1, tail($a)));
return prepend(head($xs), take($n - 1, tail($xs)));
} catch (EmptyListError $e) {
return fromNil();
}
Expand All @@ -33,10 +37,27 @@ function take(int $n, Listt $a = null)
* drop :: Int -> [a] -> [a]
*
* drop n xs returns the suffix of xs after the first n elements, or [] if n > length xs:
* @param int $n
* @param Listt $xs
* @return Listt
*/
function drop()
function drop(int $n, Listt $xs = null)
{
// TODO
return curryN(2, function (int $n, Listt $xs): Listt {
if ($n < 1) {
return $xs;
}

if ($n > length($xs)) {
return fromNil();
}

try {
return drop($n - 1, tail($xs));
} catch (EmptyListError $e) {
return fromNil();
}
})(...func_get_args());
}

/**
Expand Down
61 changes: 61 additions & 0 deletions test/Functional/DropTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace test\Functional;

use Widmogrod\Primitive\Listt;
use function Widmogrod\Functional\drop;
use function Widmogrod\Functional\fromIterable;
use function Widmogrod\Functional\fromNil;

class DropTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provideData
*/
public function test_it(
Listt $a,
int $n,
Listt $expected
) {
$result = drop($n, $a);

$r = print_r($result->extract(), true);
$e = print_r($expected->extract(), true);

$this->assertTrue(
$result->equals($expected),
"$e != $r"
);
}

public function provideData()
{
return [
'should return empty list from when input is empty list' => [
'$a' => fromNil(),
'$n' => 1,
'$expected' => fromNil(),
],
'should provided list when n is zero' => [
'$a' => fromIterable([1, 2, 3, 4, 5]),
'$n' => 0,
'$expected' => fromIterable([1, 2, 3, 4, 5]),
],
'should provided list when n is negative' => [
'$a' => fromIterable([1, 2, 3, 4, 5]),
'$n' => random_int(-1000, -1),
'$expected' => fromIterable([1, 2, 3, 4, 5]),
],
'should return part of finite list' => [
'$a' => fromIterable([1, 2, 3, 4, 5]),
'$n' => 3,
'$expected' => fromIterable([4, 5]),
],
'should return nil list when drop more than in the list' => [
'$a' => fromIterable([1, 2, 3, 4, 5]),
'$n' => 3000,
'$expected' => fromNil(),
],
];
}
}

0 comments on commit 0c43a64

Please sign in to comment.