Giter Club home page Giter Club logo

knapsack's Introduction

Knapsack

Collection pipeline library for PHP

SensioLabsInsight Build Status Code Coverage Scrutinizer Code Quality

Knapsack is a collection library for PHP >= 5.6 that implements most of the sequence operations proposed by Clojures sequences plus some additional ones. All its features are available as functions (for functional programming) and as a collection pipeline object methods.

The heart of Knapsack is its Collection class. However its every method calls a simple function with the same name that does the actual heavy lifting. These are located in DusanKasan\Knapsack namespace and you can find them here. Collection is a Traversable implementor (via IteratorAggregate) that accepts Traversable object, array or even a callable that produces a Traversable object or array as constructor argument. It provides most of Clojures sequence functionality plus some extra features. It is also immutable - operations preformed on the collection will return new collection (or value) instead of modifying the original collection.

Most of the methods of Collection return lazy collections (such as filter/map/etc.). However, some return non-lazy collections (reverse) or simple values (count). For these operations all of the items in the collection must be iterated over (and realized). There are also operations (drop) that iterate over some items of the collection but do not affect/return them in the result. This behaviour as well as laziness is noted for each of the operations.

If you want more example usage beyond what is provided here, check the specs and/or scenarios. There are also performance tests you can run on your machine and see the computation time impact of this library (the output of these is included below).

Feel free to report any issues you find. I will do my best to fix them as soon as possible, but community pull requests to fix them are more than welcome.

Documentation

Check out the documentation (which is prettified version of this readme) at http://dusankasan.github.io/Knapsack

Installation

Require this package using Composer.

composer require dusank/knapsack

Usage

Instantiate via static or dynamic constructor

use DusanKasan\Knapsack\Collection;

$collection1 = new Collection([1, 2, 3]);
$collection2 = Collection::from([1, 2, 3]); //preferred since you can call methods on its result directly.

Work with arrays, Traversable objects or callables that produce Traversables

$collection1 = Collection::from([1, 2, 3]);
$collection2 = Collection::from(new ArrayIterator([1, 2, 3]);

//Used because Generator can not be rewound
$collection2 = Collection::from(function() { //must have 0 arguments
    foreach ([1, 2, 3] as $value) {
        yield $value;
    }
});

Basic map/reduce

$result = Collection::from([1, 2])
    ->map(function($v) {return $v*2;})
    ->reduce(function($tmp, $v) {return $tmp+$v;}, 0);
    
echo $result; //6

The same map/reduce using Knapsack's collection functions

$result = reduce(
    map(
        [1, 2], 
        function($v) {return $v*2;}
    ),
    function($tmp, $v) {return $tmp+$v;},
    0
);

echo $result; //6

Get first 5 items of Fibonacci's sequence

$result = Collection::iterate([1,1], function($v) {
        return [$v[1], $v[0] + $v[1]]; //[1, 2], [2, 3] ...
    })
    ->map('\DusanKasan\Knapsack\first') //one of the collection functions
    ->take(5);
    
foreach ($result as $item) {
    echo $item . PHP_EOL;
}

//1
//1
//2
//3
//5

If array or Traversable would be returned from functions that return an item from the collection, it can be converted to Collection using the optional flag. By default it returns the item as is.

$result = Collection::from([[[1]]])
    ->first(true)
    ->first();
    
var_dump($result); //[1]

Collections are immutable

function multiplyBy2($v)
{
    return $v * 2;
}

function multiplyBy3($v)
{
    return $v * 3;
}

function add($a, $b)
{
    return $a + $b;
}

$collection = Collection::from([1, 2]);

$result = $collection
    ->map('multiplyBy2')
    ->reduce(0, 'add');
    
echo $result; //6

//On the same collection
$differentResult = $collection
    ->map('multiplyBy3')
    ->reduce(0, 'add');
    
echo $differentResult; //9

Keys are not unique by design

It would harm performance. This is only a problem if you need to call toArray(), then you should call values() before.

$result = Collection::from([1, 2])->concat([3,4]);
    
//arrays have unique keys
$result->toArray(); //[3,4]
$result->values()->toArray(); //[1, 2, 3, 4]

//When iterating, you can have multiple keys.
foreach ($result as $key => $item) {
    echo $key . ':' . $item . PHP_EOL;
}

//0:1
//1:2
//0:3
//1:4

Collection trait is provided

If you wish to use all the Collection methods in your existing classes directly, no need to proxy their calls, you can just use the provided CollectionTrait. This will work on any Traversable by default. In any other class you will have to override the getItems() method provided by the trait. Keep in mind that after calling filter or any other method that returns collection, the returned type will be actually Collection, not the original Traversable.

class AwesomeIterator extends ArrayIterator {
    use CollectionTrait;
}

$iterator = new AwesomeIterator([1, 2, 3]);
$iterator->size(); //3

Performance tests

PHP 5.6

+------------------------------------------------------------------------------------+-----------------------+---------------------------+----------------------+
| operation details                                                                  | native execution time | collection execution time | difference (percent) |
+------------------------------------------------------------------------------------+-----------------------+---------------------------+----------------------+
| array_map vs Collection::map on 10000 integers (addition)                          | 0.0034945011138916s   | 0.0034625053405762s       | 99%                  |
| array_map vs Collection::map on 10000 strings (concatenation)                      | 0.004361891746521s    | 0.0049739360809326s       | 114%                 |
| array_map vs Collection::map on 10000 objects (object to field value)              | 0.02332329750061s     | 0.027161455154419s        | 116%                 |
| array_map vs Collection::map on 10000 md5 invocations                              | 0.0086771726608276s   | 0.0080755949020386s       | 93%                  |
| array_map vs Collection::map on 10000 integers n, counting sum(0, n) the naive way | 1.5985415458679s      | 1.580038356781s           | 98%                  |
+------------------------------------------------------------------------------------+-----------------------+---------------------------+----------------------+

PHP 7.1.1

+------------------------------------------------------------------------------------+-----------------------+---------------------------+----------------------+
| operation details                                                                  | native execution time | collection execution time | difference (percent) |
+------------------------------------------------------------------------------------+-----------------------+---------------------------+----------------------+
| array_map vs Collection::map on 10000 integers (addition)                          | 0.00082111358642578s  | 0.001681661605835s        | 204%                 |
| array_map vs Collection::map on 10000 strings (concatenation)                      | 0.00081214904785156s  | 0.0015116214752197s       | 186%                 |
| array_map vs Collection::map on 10000 objects (object to field value)              | 0.0015491008758545s   | 0.0036969423294067s       | 238%                 |
| array_map vs Collection::map on 10000 md5 invocations                              | 0.0032038688659668s   | 0.0039427280426025s       | 123%                 |
| array_map vs Collection::map on 10000 integers n, counting sum(0, n) the naive way | 0.93844709396362s     | 0.93354930877686s         | 99%                  |
+------------------------------------------------------------------------------------+-----------------------+---------------------------+----------------------+

Constructors

These are ways how to create the Collection class. There is one default constructor and few named (static) ones.

new(iterable|callable $input)

The default constructor accepts array, Traversable or a callable that takes no arguments and produces Traversable or array. The use case for the callable argument is for example a Generator, which can not be rewound so the Collection must be able to reconstruct it when rewinding itself.

$collection = new Collection([1, 2, 3]);
$collection = new Collection(new ArrayIterator([1, 2, 3]));
$generatorFactory = function () {
    foreach ([1, 2] as $value) {
        yield $value;
    }
};

$collection = new Collection($generatorFactory);

from(iterable|callable $input)

Collection::from is a static alias of the default constructor. This is the preferred way to create a Collection.

$collection = Collection::from([1, 2, 3]);
$collection = Collection::from(new ArrayIterator([1, 2, 3]));
$generatorFactory = function () {
    foreach ([1, 2] as $value) {
        yield $value;
    }
};

$collection = Collection::from($generatorFactory);

iterate(mixed $input, callable $function)

Returns lazy collection of values, where first value is $input and all subsequent values are computed by applying $function to the last value in the collection. By default this produces an infinite collection. However you can end the collection by throwing a NoMoreItems exception.

$collection = Collection::iterate(1, function ($value) {return $value + 1;}); // 1, 2, 3, 4 ...

repeat(mixed $value, int $times = -1)

Returns a lazy collection of $value repeated $times times. If $times is not provided the collection is infinite.

Collection::repeat(1); //infinite collection of ones
Collection::repeat(1, 4)->toArray(); //[1, 1, 1, 1]

range(int $start = 0, int $end = null, int step = 1)

Returns a lazy collection of numbers starting at $start, incremented by $step until $end is reached.

Collection::range(0, 6, 2)->toArray(); //[0, 2, 4, 6]

Operations

These are the operations (methods) provided by Collection class. For each one, there is a function with the same name in Knapsack namespace. The function has the same footprint as the method, except it has one extra argument prepended - the collection (array or Traversable).

Standard Iterator methods

It implements http://php.net/manual/en/class.iterator.php

append(mixed $item, mixed $key = null) : Collection

Returns a lazy collection of items of this collection with $item added as last element. If $key is not provided, its key will be the next integer in the sequence.

Collection::from([1, 3, 3, 2])
    ->append(1)
    ->toArray(); //[1, 3, 3, 2, 1]
Collection::from([1, 3, 3, 2])
    ->append(1, 'key')
    ->toArray(); //[1, 3, 3, 2, 'key' => 1]
toArray(append([1, 3, 3, 2], 1, 'key')); //[1, 3, 3, 2, 'key' => 1]

average() : int|float

Returns average of values in this collection.

Collection::from([1, 2, 3, 2])->average(); //2
Collection::from([1, 2, 3, 2, 2])->average(); //2.2
Collection::from([])->average(); //0
average([1, 2, 3]); //2

combine(iterable $collection, bool $strict = false) : Collection

Combines the values of this collection as keys, with values of $collection as values. The resulting collection has length equal to the size of smaller collection. If $strict is true, the size of both collections must be equal, otherwise ItemNotFound is thrown. When strict, the collection is realized immediately.

Collection::from(['a', 'b'])
    ->combine([1, 2])
    ->toArray(); //['a' => 1, 'b' => 2]
toArray(combine(['a', 'b'], [1, 2])); //['a' => 1, 'b' => 2]

concat(iterable ...$collections) : Collection

Returns a lazy collection with items from this collection followed by items from the collection from first argument, then second and so on.

Collection::from([1, 3, 3, 2])
    ->concat([4, 5]) //If we would convert to array here, we would loose 2 items because of same keys [4, 5, 3, 2]
    ->values() 
    ->toArray(); //[1, 3, 3, 2, 4, 5]
toArray(values(concat([1, 3, 3, 2], [4, 5]))); //[1, 3, 3, 2, 4, 5]  

contains(mixed $needle) : bool

Returns true if $needle is present in the collection.

Collection::from([1, 3, 3, 2])->contains(2); //true
contains([1, 3, 3, 2], 2); //true

countBy(callable $function) : Collection

Returns a collection of items whose keys are the return values of $function(value, key) and values are the number of items in this collection for which the $function returned this value.

Collection::from([1, 2, 3, 4, 5])
    ->countBy(function ($value) {
        return $value % 2 == 0 ? 'even' : 'odd';
    })
    ->toArray(); //['odd' => 3, 'even' => 2]  
toArray(countBy([1, 2, 3, 4, 5], function ($value) {return $value % 2 == 0 ? 'even' : 'odd';}));      

cycle() : Collection

Returns an infinite lazy collection of items in this collection repeated infinitely.

Collection::from([1, 3, 3, 2])
    ->cycle()
    ->take(8) //we take just 8 items, since this collection is infinite
    ->values()
    ->toArray(); //[1, 3, 3, 2, 1, 3, 3, 2]
toArray(values(take(cycle([1, 3, 3, 2]), 8))); //[1, 3, 3, 2, 1, 3, 3, 2]

diff(iterable ...$collections) : Collection

Returns a lazy collection of items that are in $collection but are not in any of the other arguments, indexed by the keys from the first collection. Note that the ...$collections are iterated non-lazily.

Collection::from([1, 3, 3, 2])
    ->diff([1, 3])
    ->toArray(); //[3 => 2]
toArray(diff([1, 3, 3, 2], [1, 3])); //[3 => 2]

distinct() : Collection

Returns a lazy collection of distinct items. The comparison whether the item is in the collection or not is the same as in in_array.

Collection::from([1, 3, 3, 2])
    ->distinct()
    ->toArray(); //[1, 3, 3 => 2] - each item has key of the first occurrence
toArray(distinct([1, 3, 3, 2])); //[1, 3, 3 => 2] - each item has key of the first occurrence

drop(int $numberOfItems) : Collection

A form of slice that returns all but first $numberOfItems items.

Collection::from([1, 2, 3, 4, 5])
    ->drop(4)
    ->toArray(); //[4 => 5]
toArray(drop([1, 2, 3, 4, 5], 4)); //[4 => 5]    

dropLast($numberOfItems = 1) : Collection

Returns a lazy collection with last $numberOfItems items skipped. These are still realized, just skipped.

Collection::from([1, 2, 3])
    ->dropLast()
    ->toArray(); //[1, 2]
Collection::from([1, 2, 3])
$collection
    ->dropLast(2)
    ->toArray(); //[1]
toArray(dropLast([1, 2, 3], 2)); //[1]    

dropWhile(callable $function) : Collection

Returns a lazy collection by removing items from this collection until first item for which $function(value, key) returns false.

Collection::from([1, 3, 3, 2])
    ->dropWhile(function ($v) {
        return $v < 3;
    })
    ->toArray(); //[1 => 3, 2 => 3, 3 => 2])
Collection::from([1, 3, 3, 2])
    ->dropWhile(function ($v, $k) {
        return $k < 2 && $v < 3;
    })
    ->toArray(); //[1 => 3, 2 => 3, 3 => 2])
Collection::from([1, 3, 3, 2])
    ->dropWhile(function ($v, $k) {
        return $k < 2 && $v < 3;
    })
    ->toArray(); //[1 => 3, 2 => 3, 3 => 2])
toArray(values(dropWhile([1, 3, 3, 2], function ($v) {return $v < 3;}))); // [3, 3, 2]

dump(int $maxItemsPerCollection = null, $maxDepth = null) : array

Dumps this collection into array (recursively).

  • scalars are returned as they are,
  • array of class name => properties (name => value and only properties accessible for this class) is returned for objects,
  • arrays or Traversables are returned as arrays,
  • for anything else result of calling gettype($input) is returned

If specified, $maxItemsPerCollection will only leave specified number of items in collection, appending a new element at end '>>>' if original collection was longer.

If specified, $maxDepth will only leave specified n levels of nesting, replacing elements with '^^^' once the maximum nesting level was reached.

If a collection with duplicate keys is encountered, the duplicate keys (except the first one) will be change into a format originalKey//duplicateCounter where duplicateCounter starts from 1 at the first duplicate. So [0 => 1, 0 => 2] will become [0 => 1, '0//1' => 2]

Collection::from([1, 3, 3, 2])->dump(); //[1, 3, 3, 2]
$collection = Collection::from(
    [
        [
            [1, [2], 3],
            ['a' => 'b'],
            new ArrayIterator([1, 2, 3])
        ],
        [1, 2, 3],
        new ArrayIterator(['a', 'b', 'c']),
        true,
        new \DusanKasan\Knapsack\Tests\Helpers\Car('sedan', 5),
        \DusanKasan\Knapsack\concat([1], [1])
    ]
);

$collection->dump(2, 3);
//[
//    [
//        [1, '^^^', '>>>'],
//        ['a' => 'b'],
//        '>>>'
//    ],
//    [1, 2, '>>>'],
//    '>>>'
//]

$collection->dump();
//[
//    [
//        [1, [2], 3],
//        ['a' => 'b'],
//        [1, 2, 3]
//    ],
//    [1, 2, 3],
//    ['a', 'b', 'c'],
//    true,
//    [
//        'DusanKasan\Knapsack\Tests\Helpers\Car' => [
//            'numberOfSeats' => 5,
//        ],
//    ],
//    [1, '0//1' => 1]
//]
dump([1, 3, 3, 2], 2); // [1, 3, '>>>']

each(callable $function) : Collection

Returns a lazy collection in which $function(value, key) is executed for each item.

Collection::from([1, 2, 3, 4, 5])
    ->each(function ($i) {
        echo $i . PHP_EOL;
    })
    ->toArray(); //[1, 2, 3, 4, 5]

//1
//2
//3
//4
//5
each([1, 2, 3, 4, 5], function ($v) {echo $v . PHP_EOL;});

//1
//2
//3
//4
//5

every(callable $function) : bool

Returns true if $function(value, key) returns true for every item in this collection, false otherwise.

Collection::from([1, 3, 3, 2])
    ->every(function ($v) {
        return $v < 3;
    }); //false
Collection::from([1, 3, 3, 2])
    ->every(function ($v, $k) {
       return $v < 4 && $k < 2;
    }); //false
every([1, 3, 3, 2], function ($v) {return $v < 5;}); //true

except(iterable $keys) : Collection

Returns a lazy collection without the items associated to any of the keys from $keys.

Collection::from(['a' => 1, 'b' => 2])
    ->except(['a'])
    ->toArray(); //['b' => 2]
toArray(except(['a' => 1, 'b' => 2], ['a'])); //['b' => 2]

extract(mixed $keyPath) : Collection

Returns a lazy collection of data extracted from $collection items by dot separated key path. Supports the * wildcard. If a key contains \ or * it must be escaped using \ character.

$collection = Collection::from([['a' => ['b' => 1]], ['a' => ['b' => 2]], ['c' => ['b' => 3]]])
$collection->extract('a.b')->toArray(); //[1, 2]
$collection->extract('*.b')->toArray(); //[1, 2, 3]
toArray(extract([['a' => ['b' => 1]], ['a' => ['b' => 2]]], 'a.b')); //[1, 2]

filter(callable $function = null) : Collection

Returns a lazy collection of items for which $function(value, key) returned true.

Collection::from([1, 3, 3, 2])
    ->filter(function ($value) {
        return $value > 2;
    })
    ->values()
    ->toArray(); //[3, 3]
Collection::from([1, 3, 3, 2])
    ->filter(function ($value, $key) {
        return $value > 2 && $key > 1;
    })
    ->toArray(); //[2 => 3]
toArray(values(filter([1, 3, 3, 2], function ($value) {return $value > 2;}))); //[3, 3]

If $function is not provided, \DusanKasan\Knapsack\identity is used so every falsy value is removed.

Collection::from([0, 0.0, false, null, "", []])
    ->filter()
    ->isEmpty(); //true
isEmpty(values(filter([0, 0.0, false, null, "", []]))); //true

find(callable $function, mixed $ifNotFound = null, bool $convertToCollection = false) : mixed|Collection

Returns first value for which $function(value, key) returns true. If no item is matched, returns $ifNotFound. If $convertToCollection is true and the return value is an iterable an instance of Collection will be returned.

Collection::from([1, 3, 3, 2])
    ->find(function ($value) {
       return $value < 3;
    }); //1
Collection::from([1, 3, 3, 2])
    ->find(function ($value) {
       return $value > 3;
    }, 10); //10
Collection::from([1, 3, 3, 2])
    ->find(function ($value, $key) {
      return $value < 3 && $key > 1;
    }); //2
//if the output can be converted to Collection (it's array or Traversable), it will be.
Collection::from([1, [4, 5], 3, 2])
    ->find(function ($value) {
      return is_array($value);
    }, [], true)
    ->size(); //2 
find([1, 3, 3, 2], function ($value) {return $value > 2;}); //3

first(bool $convertToCollection = false) : mixed|Collection

Returns first value in the collection or throws ItemNotFound if the collection is empty. If $convertToCollection is true and the return value is an iterable an instance of Collection will be returned.

Collection::from([1, 2, 3])->first(); //1
Collection::from([[1], 2, 3])->first(); //[1]
Collection::from([])->first(); //throws ItemNotFound
first([1, 2, 3]); //1

flatten(int $depth = -1) : Collection

Returns a lazy collection with one or multiple levels of nesting flattened. Removes all nesting when no $depth value is passed.

Collection::from([1,[2, [3]]])
    ->flatten()
    ->values() //1, 2 and 3 have all key 0
    ->toArray(); //[1, 2, 3]
Collection::from([1,[2, [3]]])
    ->flatten(1)
    ->values() //1, 2 and 3 have all key 0
    ->toArray(); //[1, 2, [3]]
toArray(values(flatten([1, [2, [3]]]))); //[1, 2, 3]

flip() : Collection

Returns a lazy collection where keys and values are flipped.

Collection::from(['a' => 0, 'b' => 1])
    ->flip()
    ->toArray(); //['a', 'b']
toArray(flip(['a' => 0, 'b' => 1])); //['a', 'b']

frequencies() : Collection

Returns a collection where keys are distinct items from this collection and their values are number of occurrences of each value.

Collection::from([1, 3, 3, 2])
    ->frequencies()
    ->toArray(); //[1 => 1, 3 => 2, 2 => 1]
toArray(frequencies([1, 3, 3, 2])); //[1 => 1, 3 => 2, 2 => 1]

get(mixed $key, bool $convertToCollection = false) : mixed|Collection

Returns value at the key $key. If multiple values have this key, return first. If no value has this key, throw ItemNotFound. If $convertToCollection is true and the return value is an iterable an instance of Collection will be returned.

Collection::from([1, 3, 3, 2])->get(2); //3
Collection::from([1, [1, 2]])->get(1, true)->toArray(); //[1, 2]
Collection::from([1, 3, 3, 2])->get(5); //throws ItemNotFound
get([1, 3, 3, 2], 2); //3

getOrDefault(mixed $key, mixed $default = null, bool $convertToCollection = false) : mixed|Collection

Returns value at the key $key. If multiple values have this key, return first. If no value has this key, return $default. If $convertToCollection is true and the return value is an iterable an instance of Collection is returned.

Collection::from([1, 3, 3, 2])->getOrDefault(2); //3
Collection::from([1, 3, 3, 2])->getOrDefault(5); //null
Collection::from([1, 3, 3, 2])->getOrDefault(5, 'asd'); //'asd'
Collection::from([1, 3, 3, 2])->getOrDefault(5, [1, 2], true)->toArray(); //[1, 2]
getOrDefault([1, 3, 3, 2], 5, 'asd'); //'asd'

groupBy(callable $function) : Collection

Returns collection which items are separated into groups indexed by the return value of $function(value, key).

Collection::from([1, 2, 3, 4, 5])
    ->groupBy(function ($value) {
        return $value % 2;
    })
    ->toArray(); //[1 => [1, 3, 5], 0 => [2, 4]]
toArray(groupBy([1, 2, 3, 4, 5], function ($value) {return $value % 2;})); //[1 => [1, 3, 5], 0 => [2, 4]]

groupByKey(mixed $key) : Collection

Returns collection where items are separated into groups indexed by the value at given key.

Collection::from([
        ['letter' => 'A', 'type' => 'caps'],
        ['letter' => 'a', 'type' => 'small'],
        ['letter' => 'B', 'type' => 'caps'],
    ])
    ->groupByKey('type')
    ->map('DusanKasan\Knapsack\toArray')
    ->toArray();
    // [ 'caps' => [['letter' => 'A', 'type' => 'caps'], ...], 'small' => [['letter' => 'a', 'type' => 'small']]]
$data = [
    ['letter' => 'A', 'type' => 'caps'],
    ['letter' => 'a', 'type' => 'small'],
    ['letter' => 'B', 'type' => 'caps'],
];
toArray(map(groupByKey($data, 'type'), 'toArray')); //[ 'caps' => [['letter' => 'A', 'type' => 'caps'], ...], 'small' => [['letter' => 'a', 'type' => 'small']]]

has(mixed $key) : bool

Checks for the existence of $key in this collection.

Collection::from(['a' => 1])->has('a'); //true
has(['a' => 1], 'a'); //true

indexBy(callable $function) : Collection

Returns a lazy collection by changing keys of this collection for each item to the result of $function(value, key) for that key/value.

Collection::from([1, 3, 3, 2])
    ->indexBy(function ($v) {
        return $v;
    })
    ->toArray(); //[1 => 1, 3 => 3, 2 => 2]
toArray(indexBy([1, 3, 3, 2], '\DusanKasan\Knapsack\identity')); //[1 => 1, 3 => 3, 2 => 2]

interleave(iterable ...$collections) : Collection

Returns a lazy collection of first item from first collection, first item from second, second from first and so on. Works with any number of arguments.

Collection::from([1, 3, 3, 2])
    ->interleave(['a', 'b', 'c', 'd', 'e'])
    ->values()
    ->toArray(); //[1, 'a', 3, 'b', 3, 'c', 2, 'd', 'e']
toArray(interleave([1, 3, 3, 2], ['a', 'b', 'c', 'd', 'e'])); //[1, 'a', 3, 'b', 3, 'c', 2, 'd', 'e']

interpose(mixed $separator) : Collection

Returns a lazy collection of items of this collection separated by $separator item.

Collection::from([1, 2, 3])
    ->interpose('a')
    ->values() // we must reset the keys, because each 'a' has undecided key
    ->toArray(); //[1, 'a', 2, 'a', 3]
toArray(interpose([1, 2, 3], 'a')); //[1, 'a', 2, 'a', 3] 

intersect(iterable ...$collections) : Collection

Returns a lazy collection of items that are in $collection and all the other arguments, indexed by the keys from the first collection. Note that the ...$collections are iterated non-lazily.

Collection::from([1, 2, 3])
    ->intersect([1, 3])
    ->toArray(); //[1, 2 => 3]
toArray(intersect([1, 2, 3],[1, 3])); //[1, 2 => 3] 

isEmpty() : bool

Returns true if is collection is empty. False otherwise.

Collection::from([1, 3, 3, 2])->isEmpty(); //false
isEmpty([1]); //false

isNotEmpty() : bool

Opposite of isEmpty

Collection::from([1, 3, 3, 2])->isNotEmpty(); //true
isNotEmpty([1]); //true

keys() : Collection

Returns a lazy collection of the keys of this collection.

Collection::from(['a' => [1, 2], 'b' => [2, 3]])
    ->keys()
    ->toArray(); //['a', 'b']
toArray(keys(['a' => [1, 2], 'b' => [2, 3]])); //['a', 'b']

last(bool $convertToCollection = false) : mixed|Collection

Returns last value in the collection or throws ItemNotFound if the collection is empty. If $convertToCollection is true and the return value is an iterable an instance of Collection is returned.

Collection::from([1, 2, 3])->last(); //3
Collection::from([1, 2, [3]])->last(true)->toArray(); //[1]
Collection::from([])->last(); //throws ItemNotFound
last([1, 2, 3]); //3

map(callable $function) : Collection

Returns collection where each value is changed to the output of executing $function(value, key).

Collection::from([1, 3, 3, 2])
    ->map(function ($value) {
        return $value + 1;
    })
    ->toArray(); //[2, 4, 4, 3]
toArray(map([1, 3, 3, 2], '\DusanKasan\Knapsack\increment')); //[2, 4, 4, 3]

mapcat(callable $mapper) : Collection

Returns a lazy collection which is a result of calling map($mapper(value, key)) and then flatten(1).

Collection::from([1, 3, 3, 2])
    ->mapcat(function ($value) {
        return [[$value]];
    })
    ->values()
    ->toArray(); //[[1], [3], [3], [2]]
Collection::from([1, 3, 3, 2])
    ->mapcat(function ($key, $value) {
        return [[$key]];
    })
    ->values()
    ->toArray(); //[[0], [1], [2], [3]]
toArray(values(mapcat([1, 3, 3, 2], function ($value) {return [[$value]];}))); //[[1], [3], [3], [2]]

max() : mixed

Returns mthe maximal value in this collection.

Collection::from([1, 2, 3, 2])->max(); //3
Collection::from([])->max(); //null
max([1, 2, 3, 2]); //3

min() : mixed

Returns mthe minimal value in this collection.

Collection::from([1, 2, 3, 2])->min(); //1
Collection::from([])->min(); //null
min([1, 2, 3, 2]); //1

only(iterable $keys) : Collection

Returns a lazy collection of items associated to any of the keys from $keys.

Collection::from(['a' => 1, 'b' => 2])
    ->only(['b'])
    ->toArray(); //['b' => 2]
toArray(only(['a' => 1, 'b' => 2], ['b'])); //['b' => 2]

partition(int $numberOfItems, int $step = 0, iterable $padding = []) : Collection

Returns a lazy collection of collections of $numberOfItems items each, at $step step apart. If $step is not supplied, defaults to $numberOfItems, i.e. the partitionsdo not overlap. If a $padding collection is supplied, use its elements asnecessary to complete last partition up to $numberOfItems items. In case there are not enough padding elements, return a partition with less than $numberOfItems items.

Collection::from([1, 3, 3, 2])
    ->partition(3, 2, [0, 1])
    ->toArray(); //[[1, 3, 3], [2 => 3, 3 => 2, 0 => 0]]
Collection::from([1, 3, 3, 2])
    ->partition(3, 2)
    ->toArray(); //[[1, 3, 3], [2 => 3, 3 => 2]]
Collection::from([1, 3, 3, 2])
    ->partition(3)
    ->toArray(); //[[1, 3, 3], [3 => 2]]
toArray(partition([1, 3, 3, 2], 3)); //[[1, 3, 3], [3 => 2]]

partitionBy(callable $function) : Collection

Creates a lazy collection of collections created by partitioning this collection every time $function(value, key) will return different result.

Collection::from([1, 3, 3, 2])
    ->partitionBy(function ($v) {
        return $v % 3 == 0;
    })
    ->toArray(); //[[1], [1 => 3, 2 => 3], [3 => 2]]
toArray(partitionBy([1, 3, 3, 2], function ($value) {return $value % 3 == 0;})); //[[1], [1 => 3, 2 => 3], [3 => 2]]

prepend(mixed $item, mixed $key = null) : Collection

Returns a lazy collection of items of this collection with $item added as first element. Its key will be $key. If $key is not provided, its key will be the next numerical index.

Collection::from([1, 3, 3, 2])
    ->prepend(1)
    ->values() //both 1 have 0 key
    ->toArray(); //[1, 1, 3, 3, 2]
Collection::from([1, 3, 3, 2])
    ->prepend(1, 'a')
    ->toArray(); //['a' => 1, 0 => 1, 1 => 3, 2 => 3, 3 => 2]

printDump(int $maxItemsPerCollection = null, $maxDepth = null) : Collection

Calls dump on $input and then prints it using the var_export. Returns the collection. See dump function for arguments and output documentation.

Collection::from([1, 3, 3, 2])
    ->printDump()
    ->toArray(); //[1, 3, 3, 2]
toArray(printDump([1, 3, 3, 2])); //[1, 3, 3, 2]

realize() : Collection

Realizes collection - turns lazy collection into non-lazy one by iterating over it and storing the key/values.

$helper->setValue(1); 

$realizedCollection = Collection::from([1, 3, 3, 2])
    ->map(function ($item) use ($helper) {return $helper->getValue() + $item;})
    ->realize();
    
$helper->setValue(10);
$realizedCollection->toArray(); // [2, 4, 4, 3]
toArray(realize([1, 3, 3, 2])); //[1, 3, 3, 2]

reduce(callable $function, mixed $start, bool $convertToCollection = false) : mixed|Collection

Reduces the collection to single value by iterating over the collection and calling $function(tmp, value, key) while passing $start and current key/item as parameters. The output of callable is used as $start in next iteration. The output of callable on last element is the return value of this function. If $convertToCollection is true and the return value is an iterable an instance of Collection is returned.

Collection::from([1, 3, 3, 2])
    ->reduce(
        function ($tmp, $i) {
            return $tmp + $i;
        }, 
        0
    ); //9
    
Collection::from([1, 3, 3, 2])
    ->reduce(
        function ($tmp, $i) {
            $tmp[] = $i + 1;
            return $tmp;
        }, 
        [],
        true
    )
    ->first(); //2
reduce([1, 3, 3, 2], function ($tmp, $value) {return $tmp + $value;}, 0); //9

reduceRight(callable $function, mixed $start, bool $convertToCollection = false) : mixed|Collection

Like reduce, but walks from last item to the first one. If $convertToCollection is true and the return value is an iterable an instance of Collection is returned.

Collection::from([1, 3, 3, 2])
    ->reduceRight(
        function ($tmp, $i) {
            return $tmp + $i;
        }, 
        0
    ); //9
    
Collection::from([1, 3, 3, 2])
    ->reduce(
        function ($tmp, $i) {
            $tmp[] = $i + 1;
            return $tmp;
        }, 
        [],
        true
    )
    ->first(); //3
reduceRight([1, 3, 3, 2], function ($tmp, $value) {return $tmp + $value;}, 0); //9

reductions(callable $reduction, mixed $start) : Collection

Returns a lazy collection of reduction steps.

Collection::from([1, 3, 3, 2])
    ->reductions(function ($tmp, $i) {
        return $tmp + $i;
    }, 0)
    ->toArray(); //[1, 4, 7, 9]
toArray(reductions([1, 3, 3, 2], function ($tmp, $value) {return $tmp + $value;}, 0)); //[1, 4, 7, 9]

reject(callable $filter) : Collection

Returns a lazy collection of items for which $filter(value, key) returned false.

Collection::from([1, 3, 3, 2])
    ->reject(function ($value) {
        return $value > 2;
    })
    ->toArray(); //[1, 3 => 2]
Collection::from([1, 3, 3, 2])
    ->reject(function ($value, $key) {
        return $value > 2 && $key > 1;
    })
    ->toArray(); //[1, 1 => 3, 3 => 2]
toArray(reject([1, 3, 3, 2], function ($value) {return $value > 2;})); //[1, 1 => 3, 3 => 2]

replace(iterable $replacementMap) : Collection

Returns a lazy collection with items from this collection equal to any key in $replacementMap replaced for their value.

Collection::from([1, 3, 3, 2])
    ->replace([3 => 'a'])
    ->toArray(); //[1, 'a', 'a', 2]
toArray(replace([1, 3, 3, 2], [3 => 'a'])); //[1, 'a', 'a', 2]

replaceByKeys(iterable $replacementMap) : Collection

Returns a lazy collection with items from $collection, but items with keys that are found in keys of $replacementMap are replaced by their values.

Collection::from([1, 3, 3, 2])
    ->replace([3 => 'a'])
    ->toArray(); //[1, 3, 3, 'a']
toArray(replace([1, 3, 3, 2], [3 => 'a'])); //[1, 3, 3, 'a']

reverse() : Collection

Returns a non-lazy collection of items in this collection in reverse order.

Collection::from([1, 2, 3])
    ->reverse()
    ->toArray(); //[2 => 3, 1 => 2, 0 => 1]
toArray(reverse([1, 2, 3])); //[2 => 3, 1 => 2, 0 => 1]

second(bool $convertToCollection = false) : mixed|Collection

Returns the second item of $collection or throws ItemNotFound if $collection is empty or has 1 item. If $convertToCollection is true and the return value is an iterable it is converted to Collection.

Collection::from([1, 3, 3, 2])->second(); //3
second([1, 3]); //3

shuffle() : Collection

Returns a collection of shuffled items from this collection

Collection::from([1, 3, 3, 2])
    ->shuffle()
    ->toArray(); //something like [2 => 3, 0 => 1, 3 => 2, 1 => 3]
toArray(shuffle([1, 3, 3, 2])); //something like [2 => 3, 0 => 1, 3 => 2, 1 => 3]

size() : int

Returns the number of items in this collection.

Collection::from([1, 3, 3, 2])->size(); //4
size([1, 3, 3, 2]); //4

sizeIsBetween(int $fromSize, int $toSize) : bool

Checks whether this collection has between $fromSize to $toSize items. $toSize can be smaller than $fromSize.

Collection::from([1, 3, 3, 2])->sizeIsBetween(3, 5); //true
sizeIsBetween([1, 3, 3, 2], 3, 5); //true

sizeIs(int $size) : bool

Checks whether this collection has exactly $size items.

Collection::from([1, 3, 3, 2])->sizeIs(4); //true
sizeIs([1, 3, 3, 2], 4); //true

sizeIsGreaterThan(int $size) : bool

Checks whether this collection has more than $size items.

Collection::from([1, 3, 3, 2])->sizeIsGreaterThan(3); //true
sizeIsGreaterThan([1, 3, 3, 2], 3); //true

sizeIsLessThan(int $size) : bool

Checks whether this collection has less than $size items.

Collection::from([1, 3, 3, 2])->sizeIsLessThan(5); //true
sizeIsLessThan([1, 3, 3, 2], 5); //true

slice(int $from, int $to) : Collection

Returns a lazy collection of items which are part of the original collection from item number $from to item number $to inclusive. The items before $from are also realized, just not returned.

Collection::from([1, 2, 3, 4, 5])
    ->slice(2, 4)
    ->toArray(); //[2 => 3, 3 => 4]
Collection::from([1, 2, 3, 4, 5])
    ->slice(4)
    ->toArray(); //[4 => 5]
toArray(slice([1, 2, 3, 4, 5], 4)); //[4 => 5]

some(callable $function) : bool

Returns true if $function(value, key) returns true for at least one item in this collection, false otherwise.

Collection::from([1, 3, 3, 2])
    ->some(function ($value) {
       return $value < 3;
    }); //true
Collection::from([1, 3, 3, 2])
    ->some(function ($value, $key) {
       return $value < 4 && $key < 2;
    }); //true
some([1, 3, 3 ,2], function ($value) {return $value < 3;}); //true

sort(callable $function) : Collection

Returns collection sorted using $function(value1, value2, key1, key2). $function should return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

Collection::from([3, 1, 2])
    ->sort(function ($a, $b) {
        return $a > $b;
    })
    ->toArray(); //[1 => 1, 2 => 2, 0 => 3]
Collection::from([3, 1, 2])
    ->sort(function ($v1, $v2, $k1, $k2) {
        return $v1 < $v2;
    })
    ->toArray(); //[2 => 2, 1 => 1, 0 => 3]
toArray(sort([3, 1, 2], function ($a, $b) {return $a > $b;})); //[1 => 1, 2 => 2, 0 => 3]

splitAt(int $position) : Collection

Returns a collection of lazy collections: [take($position), drop($position)].

Collection::from([1, 3, 3, 2])
    ->splitAt(2)
    ->toArray(); //[[1, 3], [2 => 3, 3 => 2]]
toArray(splitAt([1, 3, 3, 2], 2)); //[[1, 3], [2 => 3, 3 => 2]]

splitWith(callable $function) : Collection

Returns a collection of lazy collections: [takeWhile($function(value, key)), dropWhile($function(value, key))].

Collection::from([1, 3, 3, 2])
    ->splitWith(function ($value) {
        return $value < 3;
    })
    ->toArray(); //[[1], [1 => 3, 2 => 3, 3 => 2]]
Collection::from([1, 3, 3, 2])
    ->splitWith(function ($value, $key) {
        return $key < 2 && $value < 3;
    })
    ->toArray(); //[[1], [1 => 3, 2 => 3, 3 => 2]]
toArray(splitWith([1, 3, 3, 2], function ($value) {return $value < 3;})); //[[1], [1 => 3, 2 => 3, 3 => 2]]

sum() : int|float

Returns a sum of all values in this collection.

Collection::from([1, 2, 3])->sum(); //6
Collection::from([1, 2, 3, 1.5])->sum(); //7.5
Collection::from([])->sum(); //0
sum([1, 2, 3]); //6

take(int $numberOfItems) : Collection

A form of slice that returns first $numberOfItems items.

Collection::from([1, 2, 3, 4, 5])
    ->take(2)
    ->toArray(); //[1, 2]
toArray(take([1, 2, 3, 4, 5], 2)); //[1, 2]

takeNth(int $step) : Collection

Returns a lazy collection of every nth item in this collection

Collection::from([1, 3, 3, 2])
    ->takeNth(2)
    ->toArray(); //[1, 2 => 3]
toArray(takeNth([1, 3, 3, 2], 2)); //[1, 2 => 3]

takeWhile(callable $function) : Collection

Returns a lazy collection of items from the start of the collection until the first item for which $function(value, key) returns false.

Collection::from([1, 3, 3, 2])
    ->takeWhile(function ($value) {
        return $value < 3;
    })
    ->toArray(); //[1]
Collection::from([1, 3, 3, 2])
    ->takeWhile(function ($value, $key) {
        return $key < 2 && $value < 3;
    })
    ->toArray(); //[1]
toArray(takeWhile([1, 3, 3, 2], function ($value) {return $value < 3;})); //[1]

transform(callable $transformer) : Collection

Uses a $transformer callable on itself that takes a Collection and returns Collection. This allows for creating a separate and reusable algorithms.

$transformer = function (Collection $collection) {
    return $collection
        ->filter(function ($item) {
            return $item > 1;
        })
        ->map('\DusanKasan\Knapsack\increment');
};

Collection::from([1, 3, 3, 2])
    ->transform($transformer)
    ->toArray(); //[4, 4, 3]

toArray() : array

Converts the collection to array recursively. Obviously this is not lazy since all the items must be realized. Calls iterator_to_array internaly.

Collection::from([1, 3, 3, 2])->toArray(); //[1, 3, 3, 2]
toArray([1, 3, 3, 2]); //[1, 3, 3, 2]

toString() : string

Returns a string by concatenating this collection's values into a string.

Collection::from([1, 'a', 3, null])->toString(); //'1a3'
Collection::from([])->toString(); //''
toString([1, 'a', 3, null]); //'1a3'

transpose(iterable $collection) : string

Returns a non-lazy collection by interchanging each row and the corresponding column. The input must be a multi-dimensional collection or an InvalidArgumentException is thrown.

$arr = Collection::from([
    new Collection([1, 2, 3]),
    new Collection([4, 5, new Collection(['foo', 'bar'])]),
    new Collection([7, 8, 9]),
])->transpose()->toArray();

// $arr =
// [
//    new Collection([1, 4, 7]),
//    new Collection([2, 5, 8]),
//    new Collection([3, new Collection(['foo', 'bar']), 9]),
// ]

zip(iterable ...$collections) : Collection

Returns a lazy collection of non-lazy collections of items from nth position from this collection and each passed collection. Stops when any of the collections don't have an item at the nth position.

Collection::from([1, 2, 3])
    ->zip(['a' => 1, 'b' => 2, 'c' => 4])
    ->map('\DusanKasan\Knapsack\toArray')
    ->toArray(); //[[1, 'a' => 1], [1 => 2, 'b' => 2], [2 => 3, 'c' => 4]]

Collection::from([1, 2, 3])
    ->zip(['a' => 1, 'b' => 2])
    ->map('\DusanKasan\Knapsack\toArray')
    ->toArray(); //[[1, 'a' => 1], [1 => 2, 'b' => 2]]
toArray(map(zip([1, 2, 3], ['a' => 1, 'b' => 2, 'c' => 4]), '\DusanKasan\Knapsack\toArray')); //[[1, 'a' => 1], [1 => 2, 'b' => 2], [2 => 3, 'c' => 4]]

Utility functions

These are the functions bundled with Knapsack to make your life easier when transitioning into functional programming.

identity(mixed $value)

Returns $value

$value === identity($value); //true

compare(mixed $a, mixed $b)

Default comparator function. Returns a negative number, zero, or a positive number when $a is logically 'less than', 'equal to', or 'greater than' $b.

compare(1, 2); //-1

increment(int $value)

Returns value of $value incremented by one.

increment(0) === 1; //true

decrement(int $value)

Returns value of $value decremented by one.

decrement(2) === 1; //true

knapsack's People

Contributors

andysnell avatar boobiq avatar dusankasan avatar gzumba avatar issei-m avatar jdreesen avatar mlebkowski avatar ntzm avatar petemcfarlane avatar shadowhand avatar shameerc avatar tjlytle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

knapsack's Issues

Return composed string

I've tried to convert this:

$errors = '';
foreach ($validator->errors() as $field => $keyErrors) {
    $errors .= "Field \"$field\" must be follow theses rules:" . PHP_EOL;
    foreach ($keyErrors as $rule) {
        $error .= " $rule" . PHP_EOL;
    }
}

return $errors;

So I wrote this:

$errors = '';

(new Collection($this->validator->errors()))
    ->each(
        function (array $rules, string $field) use (&$errors) {
            $errors .= "Field \"$field\" must be follow theses rules:" . PHP_EOL;
            (new Collection($rules))
                ->each(
                    function (string $rule) use (&$errors) {
                        $errors .= " - $rule" . PHP_EOL;
                    }
                )
                ->toArray()
            ;
        }
    )
    ->toArray()
;

return $errors;

It works perfectly, but it is a little bit complicated, have you better idea?

groupBy - Allow group by keys

Hey,

First of all thanks for the great library.
I was just wondering if we can add an option to groupBy a key in the array. This is more useful when dealing with associated arrays. This feature is there in almost all collection libraries (Laravel, CakePHP, etc)

An example usecase.

$categories = [
   ['id' => 1, 'name' => 'Category 1', 'parent_id' => 0],
   ['id' => 2, 'name' => 'Category 2', 'parent_id' => 1],
   ['id' => 3, 'name' => 'Category 3', 'parent_id' => 1],
];

$grouped = Collection::from($categories)->groupBy('parent_id');

Cheers

Generify collections using Psalm

Since this collections lib is quite popular.
I would like to suggest it to use generics/template types like doctrine/collections does. This will make it more type safe to use this collections

slice function params

The docs for the slice function says:

Returns a lazy collection of items which are part of the original collection from item number $from to item number $to inclusive. The items before $from are also realized, just not returned.

But its $to param usage is exclusive in the implementation:
if ($index >= $from && ($index < $to || $to == -1))

Also, naming an index (starting at zero) as the item number may be a little confusing.

Did I get something wrong, please? If this is an issue, is it a documentation or implementation one? Is it intended to be inclusive or exclusive?

Implement relevant native PHP interfaces

  • Countable
    • Renaming size() to count() will require new major release, wait?
    • is it worth it? Don't we want users to call count/size directly on the object?
  • ArrayAccess
    • Allows list() language construct to be used
    • Is it worth it? 4 new methods polluting the interface, how will we handle multiple same keys when unsetting, etc?

Q: Strict collections intersection

Hello,

is there any way how to compare collections strictly in the intersect() method?

The problem is that the in_array function used at https://github.com/DusanKasan/Knapsack/blob/master/src/collection_functions.php#L1301 causes the "Nesting level too deep - recursive dependency?" error when using a Collection of big objects.

I have been thinking about sending a PR with the third param set to true; however then I realised that it could be a big BC break for some users so I would like to ask you if you have some workaround suggestion instead, pelase.

Thanks!

Documentation mistakes

  • minimum/maximum functions are called min/max in title
  • unfinished code block at the concatenate description

Planned features and changes for 11.0.0

So since this came up in multiple PRs/issues, I guess I will summarize what will be done in near future.

The biggest problem is the limitation of the CollectionTrait that causes the collection methods on every class that uses it to return instance of Collection where you would prefer it to return an instance of the original class. To solve this, every collection method/function will return CollectionInterface (which the class will implement because it uses the CollectionTrait) instead of the included Collection and a mechanism will be provided by the implementor to build a new instance of the original class -- probably a factory method of sorts, where the input will be an iterable. The exact implementation is till open to discussion.

This will have to go hand in hand with introduction of Generic documentation on the collection methods to describe the return types (suggested in #61). We will still have to deal with losing type information during some operations (like filter) but it should help anyway.

Also most of the reported issues should have their fixes merged before the release of 11.0.0.

Collection pipeline with more "advanced" filtering

Hi,
first of all - what a library! I tip my hat.

I'm looking for a lib where I could filter a collection (or create a "collection pipeline") based on a boolean expressions that are not only AND-ed.

Example:
I have a collection:

$testCollection = [
    ['testField' => 'bar', 'otherValue' => 2],
    ['testField' => 'foo', 'otherValue' => 3],
    ['testField' => 'foo', 'otherValue' => 4],
];

and would like to apply a logical filter that looks like A OR (B AND C) where:
A - testField contains a letter o
B - otherValue is even
C - testField consists of 3 letters and otherValue is lower than 10

The boolean statement and A, B, C are just random examples.

I know it can be done by creating two collections: one for A, one for B AND C and then concatenating them. I'm looking for a more streamlined way though as the way to do it would differ with a way the statement look and would not scale well.

Any ideas?

Create Collection trait

Create a Collection trait that can be easily used in any Traversable to provide Collection functionality. See Cake Collections for example.

Add more collection methods?

Currently implemented methods should be enough to express any collection operation. Nevertheless, review other popular collection libraries and their methods and consider porting them here.

No methods that are used in single scope should be ported (think form values processing).

New release?

Some changes were merged in early February 2017, but never included in a tagged release, and I've got something I could use transpose() for. Any chance we can see a new release soon?

partition with -1 as step doesn't work as intended

Despite the default parameter of the partition() function for step being -1 according to the signature (in collection_functions.php), the step is used as -1 and the test fails.

It should be clear that 0 is the default parameter value.

toArray - Documented vs Current Behavior

I'm looking for some clarification about the intended behavior for toArray(). As per the current project documentation, the toArray() method (emphasis mine):

Converts the collection to array recursively.

A recursive operation implies that a collection of collections would be converted into an array of arrays; however, the current behavior just calls iterator_to_array on the top level, so that an array of collections is returned. If this is the intended behavior, the documentation should be updated to indicate that the operation is not actually recursive.

I can see places where the current behavior is desirable, but it would be nice to be able to reliable cast a whole collection into an array in a single operation without having to manually recurse the iterator. I could see this functionally added in a BC friendly way as toArray($recursive = false);

Happy to help, just need to know what direction this method is supposed to go in.

Add merge method

The real-world scenario:
I want to display day-wise data for the last month
I have an incomplete Collection from en external data source - data for some days but not all. The collection is as follows:

  • key is the date
  • value is the data

I want to fill in the blanks as to have an entry for every date, but with blank/default values (so that I can display them in a graph)

Expected/naive solution:

$externalCollection; //However I get the data
$defaultCollection = Collection::from([/**something that gives me an entry with key for each day */]);

$dataWithFilledDefault = $defaultCollection->merge($externalCollection);

Ugly solution 1 - bypass collection

$externalCollection; //However I get the data
$defaultCollection = Collection::from([/**something that gives me an entry with key for each day */]);

$dataWithFilledDefault = Collection::from(array_merge($defaultCollection->toArray(), $externalCollection->toArray()));

Ugly solution 2 - search for each item in the other collection manually

$externalCollection; //However I get the data
$defaultCollection = Collection::from([/**something that gives me an entry with key for each day */]);

$dataWithFilledDefault = $defaultCollection->map(static function($value, $key) use ($externalCollection) {
            return $externalCollection->has($key) ? $externalCollection->get($key) : $value;
        });

first throws for an empty collection

In clojure (first nil) and (first ()) returns nil. Knapsack throws, which is quite disconcerting. It would be great if this behavior was consistent with how clojure does this.

Specifying version in composer not working

Seems like no matter what version I specify in composer, the latest is being fetched. Even then, the utility_functions.php doesn't seem to match what I see in master when I don't specify.

Can anyone speak to this?

New Feature: Allow $default to be a callback

Hello,

It would be nice if the getOrDefault & find methods would execute and return the result of a closure if provided.

return $default instanceof \Closure ? $default() : $default

This would allow for code like this:

Collection::from()->getOrDefault('foo', function() {
	return new SomeExpensiveObject();
});

Collection::from()->getOrDefault('foo', function() {
	throw new \Exception('foo is required');
});

I would be happy to build & submit a PR if this is a desired feature for this project.

Thank You

groupBy recurses for each value added to a group

The implementation of groupBy causes issues (recursion depth grows huge) when a lot of entries resolve to the same group as the group is built using a lazy collection.

Easy to demonstrate with eg (enable xdebug with recursion limit to see the issue):

        $res = Collection::from(range(1, 300))
            ->groupBy(function ($val) {return 1;})
            ->map('DusanKasan\Knapsack\toArray')
            ->toArray()
            ;

Quite simple to fix by changing the groupBy function to use an array instead of Collection to build the groups.

Extend Collection

Is it possible to extend the Collection class to add my own methods.

I tried the basic way, but it appears the functions explicitly return DusanKasan\Knapsack\Collection instances.

Add more scenarios

Add more and more complex scenarios, showcasing the usage of collections in place of nested loops. These also serve as a more complex test cases.

Example: Transform CSV rows into object collections, filter, map, ...

Replace keys of array in Collection ?

Hey!

I would like to know if you have an idea to improve this with your lib?

public function transform(iterable $iterable)
{
    Collection::from($iterable)
        ->each(
            function ($value, $key) use (&$iterable) {
                if (mb_stristr($key, '.')) {
                    $iterable[strtr($key, '.', ':')] = $value;
                    unset($iterable[$key]);
                }

                if (is_iterable($value)) {
                    $iterable[$key] = $this->transform($value);
                }
            }
        )
        ->realize()
    ;

    return $iterable;
}

Thanks :)

Inconsistent conversion of return values

Some of the methods (e.g. reduce, first, last) convert their return value to collection if possible. This should be either removed, put behind a flag (another argument in function call) or add a getter for the original collection, because now it's not possible to get to the real item they return.

Example:

$a = new ArrayCollection([2, 3]);
$b = Collection::from([1, $b]);
$c = $b->last(); // returns Collection and there is no way to get the original ArrayCollection

Use correctly find() with object

Hi!

I have originaly this part of code:

public function speed(): ?float
{
    /** @var AdditionalInformation $additionalEventInfo */
    foreach ($this->additionalEventInfo as $additionalEventInfo) {
        if ($additionalEventInfo->name() === 'Speed') {
            return (float) $additionalEventInfo->value();
        }
    }

    return null;
}

I've "translated" this code with Knapsack:

public function speed(): ?float
{
    $speed = Collection::from($this->additionalEventInfo)
        ->find(
            function (AdditionalInformation $additionalInformation) {
                return $additionalInformation->name() === 'Speed';
            }
        )
    ;

    if ($speed instanceof AdditionalInformation) {
        return (float) $speed->value();
    }

    return null;
}

Finally, I don't like this code.
It is not really more readable that original.

Is it a better solution?

Thanks

add Export-ignore

File .gitattributes

* text=auto
* eol=lf

.git				export-ignore
.gitattributes		export-ignore
.gitignore			export-ignore
ci				export-ignore
.scrunitizer.yml	export-ignore
.sensiolabs.yml	export-ignore
phpspec.yml		export-ignore
phpunit.xml.dist	export-ignore
CONTRIBUTING.md	export-ignore

this removed useless files from composer-installed package

Throw more meaningful exceptions

Introduce new exceptions to make it more obvious what went wrong. For example when doing strict combine a better exception than the current ItemNotFound would be NonEqualLength.

extract method does not support collections of stdClass

"Extract" method filters items by isCollection. isCollection by unknown reason does not treat stdClass as container. Though stdClass is not Traversable, it's still efficient and popular key-value storage, suitable for foreach.

Documentation Bug - mapcat

The examples for mapcat appear to be wrong.

Collection::from([1, 3, 3, 2])
    ->mapcat(function ($value) {
        return [[$value]];
    })
    ->toArray()

outputs [[2]] not [[1], [3], [3], [2]]

Presumably a call to values needs adding in the examples. eg

Collection::from([1, 3, 3, 2])
    ->mapcat(function ($value) {
        return [[$value]];
    })
    ->values()
    ->toArray()

Find key?

I hope I'm not missing something obvious..

I want to use find() but have it return key rather than value so that I can use it with replace(). I could do this with reduce() instead I guess but was hoping I could make it more granular. Is there way to do this?

Best way to do with return in foreach?

Hi!

I would like to know how can I use pipeline collection with this code:

foreach ($myArrayOfAsciiCode as $ascii) {
    if (strpos($path, chr($ascii))) {
        return true;
    }
}

return false;

I do this:

return !Collection::from(self::INVALID_PATH_ASCII)
                  ->filter(
                      function (string $ascii) use ($path): bool {
                          return strpos($path, chr($ascii));
                      }
                  )
                  ->isEmpty();

Is this the better way?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.