<commit_before><?php

namespace Laiz\Test\Monad;

use Laiz\Monad\MonadList;
use Laiz\Monad\MonadList\Cons;

class MonadListTest extends \PHPUnit_Framework_TestCase
{
    use MonadTrait;
    use MonadPlusTrait;
    use ControlTrait;

    /**
     * @override
     */
    public function getMonadContext()
    {
        return MonadList::mzero();
    }

    /**
     * @expectedException InvalidArgumentException
     */
    public function testNull()
    {
        $c = $this->getMonadContext();
        $z = $c->ret(null);
    }

    public function testMplus()
    {
        $c = $this->getMonadContext();
        $m1 = MonadList::cast([1, 2, 3]);
        $m2 = MonadList::cast([4, 5, 6]);

        $this->assertEquals([1,2,3,4,5,6],
                            $m1->mplus($m2)->toArray());
    }

    public function testDoubleBind()
    {
        $c = $this->getMonadContext();
        $m1 = MonadList::cast([1, 2, 3]);
        $m2 = MonadList::cast([4, 5, 6]);

        $ret = $m1->bind(function($a) use($c, $m2){
            return $m2->bind(function($b) use($c, $a){
                return $c->ret([$a, $b]);
            });
        });

        $this->assertEquals([[1, 4], [1, 5], [1, 6],
                             [2, 4], [2, 5], [2, 6],
                             [3, 4], [3, 5], [3, 6]],
                            $ret->toArray());<commit_msg>add fmap test of monadlist<commit_after><?php

namespace Laiz\Test\Monad;

use Laiz\Monad\MonadList;
use Laiz\Monad\MonadList\Cons;

class MonadListTest extends \PHPUnit_Framework_TestCase
{
    use MonadTrait;
    use MonadPlusTrait;
    use ControlTrait;

    /**
     * @override
     */
    public function getMonadContext()
    {
        return MonadList::mzero();
    }

    /**
     * @expectedException InvalidArgumentException
     */
    public function testNull()
    {
        $c = $this->getMonadContext();
        $z = $c::ret(null);
    }

    public function testMplus()
    {
        $c = $this->getMonadContext();
        $m1 = MonadList::cast([1, 2, 3]);
        $m2 = MonadList::cast([4, 5, 6]);

        $this->assertEquals([1,2,3,4,5,6],
                            $m1->mplus($m2)->toArray());
    }

    public function testDoubleBind()
    {
        $c = $this->getMonadContext();
        $m1 = MonadList::cast([1, 2, 3]);
        $m2 = MonadList::cast([4, 5, 6]);

        $ret = $m1->bind(function($a) use($c, $m2){
            return $m2->bind(function($b) use($c, $a){
                return $c::ret([$a, $b]);
            });
        });

        $this->assertEquals([[1, 4], [1, 5], [1, 6],
                             [2, 4], [2, 5], [2, 6],
                             [3, 4], [3, 5], [3, 6]],
                            $ret->toArray());
    }

    public function testFmap()
    {
        $c = $this->getMonadContext();
        $m1 = MonadList::cast([1, 2, 3]);
        $m2 = MonadList::cast([4, 5, 6]);

        // use fmap & bind in MonadTrait
        $ret = $m1->fmap(function($a) use($c, $m2){
            return $m2->fmap(function($b) use($c, $a){
                return [$a, $b];
            });
        });

        $expected = [[[1, 4], [1, 5], [1, 6]],
                    [[2, 4], [2, 5], [2, 6]],
                    [[3, 4], [3, 5], [3, 6]]];
        $this->assertEquals($expected, $ret->toArray());

        // use map & MapIterator in MonadList\Cons
        $ret = $m1->map(function($a) use($c, $m2){
            return $m2->map(function($b) use($c, $a){
                return [$a, $b];
            });
        });
        $this->assertEquals($expected, $ret->toArray());
{'fraction_non_alphanumeric': 0.19218967921896793, 'fraction_numerical': 0.03403068340306834, 'mean_word_length': 1.7311500380807312, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3095458', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1231, 'n_words': 251}