2017 © Pedro Peláez
 

library plinq

Functional style operations on arrays

image

andybursh/plinq

Functional style operations on arrays

  • Sunday, October 27, 2013
  • by AndyBursh
  • Repository
  • 1 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

plinq (PHP >= 5.4.0)

A php library for working with arrays, inspired by .NET's LINQ. Currently supports a small set of actions, but will be expanded. Also supports lazy evaluation., (*1)

Available methods

  • on/with - Starts a chain of actions, (*2)

  • where - Filters the input through the provided expression, (*3)

  • count - Counts the number of elements in the input (optionally those matching an expression)
  • select - Transforms each element of the input
  • aggregate - Applies an accumulator function over each element in the input, returning the final accumulator value
  • head - Returns the first element in the input (optionally returns the key value pair)
  • tail - Returns all but the first element in the input
  • max - Returns the largest element in the input. If the input contains not only numbers, a comparator function must be provided.
  • min - Returns the smallest element in the input. If the input contains not only numbers, a comparator function must be provided.
  • all - Finds if all elements in the input match the expression
  • any - Finds if any element in the input matches the expression. If no expression is provided, returns true if the input is not empty.
  • first - Returns the first element in the input which matches the expression, otherwise null.

Example

include("plinq.php");
use plinq\plinq;

$numIsEven = function($k, $v)
{
    return ($v % ")==0;
};

$doubleNum = function($k, $v)
{
    return $v * 2;
}

$input = [1, 2, 3, 4, 5, 6];

//plinq can perform chains of actions
$filtered = plinq::with($input)
                 ->where($numIsEven)
                 ->select($doubleNum);

//Or a single action
$numOfElements = plinq::count($input);

The result of a plinq chain is a plinqWrapper object. This can be manipulated like an array but, crucially, will not be recognised as one by functions hinting an array. To get around this, either cast the result to an array or call toArray., (*4)

$castArray = (array)$filtered;
$methodArray = $filtered->toArray();

Until a result is converted to an array, you can still call plinq methods on it., (*5)

$newFilter = plinq::with($input)
                  ->where($numIsEven);

print "The first even number is ".$newFilter[0];
print "The double of the last number is " + $newFilter->select($doubleNum)[2];

Lazy evaluation

Using lazy evaluation with Plinq is done almost precisely like normal. The only difference is there's no access to the resulting value until exec has been called, returning an array (not an ArrayObject like standard plinq)., (*6)

include("lazyPlinq.php");
use plinq\lazyPlinq;

$numIsEven = function($k, $v)
{
    return ($v % ")==0;
};

$doubleNum = function($k, $v)
{
    return $v * 2;
}

$input = [1, 2, 3, 4, 5, 6];

$filtered = lazyPlinq::with($input)
                     ->where($numIsEven)
                     ->select($doubleNum)
                     ->exec();

The Versions

27/10 2013

dev-master

9999999-dev

Functional style operations on arrays

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

by Mathieu Maes