Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fusonic Linq Presentation

Fusonic Linq Presentation

fusonic-linq is a PHP library inspired by the LINQ 2 Objects extension methods in .NET.

For a full introduction read my blog-post: http://www.fusonic.net/en/blog/2013/08/14/fusonic-linq-write-less-do-more/

This is a presentation which i held @VlbgWebDev meetup in Dornbirn (Austria)

David Roth

June 11, 2014
Tweet

Other Decks in Programming

Transcript

  1. - Inconsistent and messy naming conventions - uasort(), uarsort(), usort(),

    ursort() ,array_filter(), array_walk(), … - Bad composability array array_filter (array $array = array() [, callable $callback = Function() ] ) bool usort (array &$array , callable $cmp_function) - Only for arrays -> not usable for custom collections - Missing lots of useful stuff (grouping, aggregating, projections ...) - Lots of imperative bookkeeping necessary e.x: foreach, counter variables, if-statements PHP array functions: Status quo
  2. - Simple functional programming in php - One consistent and

    easy to use API - Full autocompletion & documentation - Explore as you type - Full composability - Rich filtering, sorting, aggregating … Fusonic Linq - Some key features
  3. Fusonic Linq examples - Average (1) // Calculate the average

    filesize of all files in a directory. ### Plain PHP: ### $sum = 0; $i = 0; foreach($files as $file) { $sum += filesize($file); $i++; } $avg = $sum / $i; ### Linq: ### $avgL = Linq::from($files) ->select(function($f) { return filesize($f); }) ->average();
  4. Fusonic Linq examples - Filter (2) ### Plain PHP: ###

    $sum = 0; $i = 0; foreach($files as $file) { $currentSize = filesize($file); if($currentSize > 1024) { $sum += $currentSize; $i++; } } $avg = $sum / $i; ### Linq: ### $avgL = Linq::from($files) ->select(function($f) { return filesize($f); }) ->where(function($fs) { return $fs > 1024; }) ->average(); // Calculate the average filesize of all files greater than 1024 bytes in a directory.
  5. Fusonic Linq examples - Skip/Take (3) ### Plain PHP: ###

    $sum = 0; $i = 0; $y = 0; foreach($files as $file) { $currentSize = filesize($file); if($currentSize > 1024) { if($y < 2) { $y++; continue; } else if ($y > 5) { break; } $y++; $sum += $currentSize; $i++; } } $avg = $sum / $i; ### Linq: ### $avgL = Linq::from($files) ->select(function($x) { return filesize($x); }) ->where(function($x) { return $x > 1024; }) ->skip(2) ->take(4) ->average(); // Calculate the average filesize of all files greater than 1024 bytes in a directory // but skip the very first 2 files and then take only 4 files.
  6. Fusonic Linq examples - Order (4) // Sort all files

    in a directory by filsize in descending order ### Plain PHP: ### $data = array(); foreach($files as $file) { $currentSize = filesize($file); $data[] = array("name" => $file, "size" => $currentSize); } uasort($data, function($a, $b) { $as = $a['size']; $bs = $b['size']; if($as == $bs) { return 0; } else return $as < $bs ? 1 : -1; }); ### Linq: ### $linq = Linq::from($files) ->select(function($x) { return array("name" => $x, "size" => filesize($x)); }) ->orderByDescending(function($x) { return $x['size']; });
  7. Fusonic Linq examples - Grouping (5) // Group all files

    by its filesize. ### Plain PHP: ### $data = array(); foreach($files as $file) { $currentSize = filesize($file); $data[] = array("name" => $file, "size" => $currentSize); } uasort($data, function($a, $b) { $as = $a['size']; $bs = $b['size']; if($as == $bs) { return 0; } else return $as < $bs ? 1 : -1; }); $grouped = array(); foreach($data as $x) { if(isset($grouped[$x['size']])) { $grouped[$x['size']][] = $x; } else { $grouped[$x['size']] = array($x); } } ### Linq: ### $linq = Linq::from($files) ->select(function($x) { return array("name" => $x, "size" => filesize($x)); }) ->orderByDescending(function($x) { return $x['size']; }) ->groupBy(function($x) { return $x['size']; })
  8. - Very lightweight / minimal overhead - Lazy execution /

    Lazy evaluation - Wrapps built-in php functions in some cases for faster execution (e.x. orderBy is build on top of asort, arsort) Fusonic Linq - Performance
  9. - Fully unit-tested (99 % coverage) - Full PSR-1 &

    PSR-2 compatible - Performs type checks! (DEMO) - Easily extendable Fusonic Linq - Quality & Design
  10. /* Throws an UnexpectedValueException if the provided callback function does

    not return a boolean */ Linq::from(array("1", "1")) ->where(function($x) { return "NOT A BOOLEAN"; }); /* Throws an UnexpectedValueException if one of the values is not convertible to a numeric value:*/ Linq::from(array(1, 2, "Not a numeric value")) ->sum(); Fusonic Linq - Type checks