Skip to content

paul-mateescu/lazy-fluent-functional-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 

Repository files navigation

lazy-fluent-functional-php

This is a lazy, fluent and functional library for PHP 7.

I have one for PHP 5 in the works as well :)

Please bear with me, I will update this, create test files etc as fast and as often I can.

Contents

Examples

Installation

Usage

Reference

Factorial function:

function factorial($n){
    return \LFF7\from_range(1,$n) -> product();
}

Finding the sum of the first one million natural numbers:

$sum = 
    \LFF7\naturals()
        ->take(1000000)
        ->sum();

Finding the sum of all the values in the third column of a CSV file:

$sum = 
    \LFF7\from_csv_file('myfile.csv')
        ->column(2)
        ->sum();

(It will work with a 1TB file, because it's all lazy :) )

Finding the product of the even numbers of an array:

$product = 

\LFF7\from_array([1, 5, 7, 9, 0, 2, 3, 6, 5, 2, 6])
        ->evens()
        ->product();

(It will stop after finding a zero and return it as the final value of the computation).

A complex computation from a CSV file:

Let's say we have a CSV file with the following structure:

Name Date Sales
Danny 2017-01-03 100.25
George 2017-01-03 90.00
Linda 2017-01-03 190.00
Danny 2017-01-04 200.00
George 2017-01-04 254.00
Linda 2017-01-04 810.99
... ... ...

We would like to obtain the sum of the sales on each person, ordered by name.

$arr = 
    \LFF7\from_csv_file('sales.csv')
        ->columns(0, 2)
        ->reindex()
        ->group_on_column(0)
        ->sort_asc_on_key()
        ->map(
            function($v){
                return 
                \LFF\from_array($v)
                    ->column(1)
                    ->sum();
            }
        )
        ->to_array();

Just require LFF7.php.

  1. Obtain a generator wrapped in an object using the generator creation functions from_array, from_file a.s.o. Example:
$generator = from_array([1, 2, 3, 4, 5, 6, 7, 8, 9]);
  1. Fluently chain methods that express your desired computation. For example:
$computation = $generator->odds()->less_than(6);
  1. Perform the actual computation (note the function invocation):
foreach($computation() as $value){
    echo $value, " ";
}

Functions that return generator objects

From an array:

$gen = \LFF7\from_array([1, 2, 3, 4, 5]);

From a range of integers:

$gen = \LFF7\from_range(10, 2000000);

(the range of integers starting with 10 and ending with 2000000)

All natural numbers:

$gen = \LFF7\naturals();

(returns a generator lazily yielding all natural numbers)

From a file:

$gen = \LFF7\from_file('myfile.txt');

(returns a generator that lazily yields one row at a time, as string)

From a CSV file:

$gen = \LFF7\from_csv_file('myfile.csv');

(returns a generator that lazily yields one row at a time, as an array)

Returns a new generator that drops the first $noOfRowsToDrop rows from the current generator. Lazy/Fluent

Example: \LFF7\from_array([1, 2, 3, 4, 5])->drop(2)->to_array() produces the array [3, 4, 5].

Returns a new generator that drops rows from the current one until $predicate returns true. Lazy/Fluent

Example: \LFF7\from_array([1, 2, 3, 4, 5])->drop_until(function($n){return $n > 3;})->to_array() produces the array [4, 5].

Returns a new generator that applies the $transform callable to items yielded from the current generator. Lazy/Fluent

Example: \LFF7\from_array([1, 2, 3, 4, 5])->map(function($x){return $x++;})->to_array() produces the array [2, 3, 4, 5, 6].

Returns a new generator that will yield at most $noOfRowsToTake from the current generator. Lazy/Fluent

Example: \LFF7\naturals()->take(100) will yield the first 100 natural numbers, i.e 0, 1, 2, ..., 99.

Returns a new generator that yields rows from the current one while $predicate returns true. Lazy/Fluent

Example: \LFF7\from_array([1, 2, 3, 4, 5])->take_while(function($n){return $n < 3;})->to_array() produces the array [1, 2].

Returns a new generator that yields rows from the current one for which $predicate returns true. Lazy/Fluent

Example: \LFF7\from_array([1, 2, 3, 4, 5])->filter(function($n){return $n % 2 == 0;})->to_array() produces the array [2, 4].

(Please note that method evens() does the same thing.)

Returns a new generator that yields rows from the current one for which $predicate returns false. Lazy/Fluent

Example: \LFF7\from_array([1, 2, 3, 4, 5])->exclude(function($n){return $n % 2 == 0;})->to_array() produces the array [1, 3, 5].

Returns a new generator that yields only the column with the $column_key key from what the current generator yields, considering that every item yielded is an array. Lazy/Fluent

Please note that, since we are interested in just a column, this method does NOT yield an array with one element, it yields the desired column value directly. The key information is lost.

Example: \LFF7\from_array([1, 2], [3, 4], [5, 6]])->column(0)->to_array() produces the array [1, 3, 5].

Returns a new generator that yields only the columns with the keys given in $column_keys from what the current generator yields, considering that every item yielded is an array. Lazy/Fluent

Key information is preserved.

Example: \LFF7\from_array([1, 2, 7], [3, 4, 8], [5, 6, 9]])->columns(1, 2)->to_array() produces the array [1=>2, 2=>7], [1=>4, 2=>8], [1=>6, 2=>9]].

Returns a new generator that removes the values having the $column_keys elements from what the current generator yields, considering that every item yielded is an array. Lazy/Fluent

For the elements that are left in the array, key information is preserved.

About

No description or website provided.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages