<commit_before><?php
namespace Widmogrod\Monad;

use Widmogrod\Common;
use Widmogrod\FantasyLand;
use Widmogrod\Functional as f;

class Collection implements
    FantasyLand\Monad,
    FantasyLand\Foldable,
    FantasyLand\Traversable,
    Common\ValueOfInterface
{
    use Common\PointedTrait;

    const of = 'Widmogrod\Monad\Collection::of';

    /**
     * @param array $value
     */
    public function __construct($value)
    {
        $this->value = f\isNativeTraversable($value)
            ? $value
            : [$value];
    }

    /**
     * @inheritdoc
     */
    public function map(callable $transformation)
    {
        $result = [];
        foreach ($this->value as $key => $value) {
            $result[$key] = call_user_func($transformation, $value);
        }

        return self::of($result);
    }

    /**
     * @inheritdoc
     *
     * fs <*> xs = [f x | f <- fs, x <- xs]
     */
    public function ap(FantasyLand\Apply $applicative)
    {
        // Since we don't have List comprehension in PHP, use a foreach
        $result = [];
        $isCollection = $applicative instanceof Collection;

        foreach ($this->extract() as $value) {
            $partial = f\valueOf($applicative->map($value));
            if ($isCollection) {
                $result = f\push($result, $partial);
            } else {
                $result[] = $partial;
            }
        }

        return $applicative instanceof FantasyLand\Pointed ? $applicative::of($result) : $result;
    }

    /**
     * @inheritdoc
     */
    public function bind(callable $transformation)
    {
        // xs >>= f = concat (map f xs)<commit_msg>revert the instanceof check so that we don't forget to fix it better later on<commit_after><?php
namespace Widmogrod\Monad;

use Widmogrod\Common;
use Widmogrod\FantasyLand;
use Widmogrod\Functional as f;

class Collection implements
    FantasyLand\Monad,
    FantasyLand\Foldable,
    FantasyLand\Traversable,
    Common\ValueOfInterface
{
    use Common\PointedTrait;

    const of = 'Widmogrod\Monad\Collection::of';

    /**
     * @param array $value
     */
    public function __construct($value)
    {
        $this->value = f\isNativeTraversable($value)
            ? $value
            : [$value];
    }

    /**
     * @inheritdoc
     */
    public function map(callable $transformation)
    {
        $result = [];
        foreach ($this->value as $key => $value) {
            $result[$key] = call_user_func($transformation, $value);
        }

        return self::of($result);
    }

    /**
     * @inheritdoc
     *
     * fs <*> xs = [f x | f <- fs, x <- xs]
     */
    public function ap(FantasyLand\Apply $applicative)
    {
        // Since we don't have List comprehension in PHP, use a foreach
        $result = [];
        $isCollection = $applicative instanceof Collection;

        foreach ($this->extract() as $value) {
            $partial = f\valueOf($applicative->map($value));
            if ($isCollection) {
                $result = f\push($result, $partial);
            } else {
                $result[] = $partial;
            }
        }

        return $applicative::of($result);
    }

    /**
     * @inheritdoc
     */
    public function bind(callable $transformation)
    {
        // xs >>= f = concat (map f xs)
{'fraction_non_alphanumeric': 0.13095956547978274, 'fraction_numerical': 0.0, 'mean_word_length': 1.9757630161579893, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4087355', 'n_tokens_mistral': 1048, 'n_tokens_neox': 987, 'n_words': 227}