$30 off During Our Annual Pro Sale. View Details »

Refactoring to Collections

Refactoring to Collections

A presentation inspired by Adam Wathan's book "Refactoring to Collections". Presented June 2nd, 2016 @ the Laravel Austin (@LaravelAustin)

Hunter Skrasek

June 02, 2016
Tweet

More Decks by Hunter Skrasek

Other Decks in Programming

Transcript

  1. REFACTORING TO
    COLLECTIONS
    1

    View Slide

  2. HIGHER ORDER FUNCTIONS
    A function that takes another function as a parameter, returns a
    function, or does both.
    2

    View Slide

  3. "Higher order functions are powerful because they let us create
    abstractions around common programming patterns"
    3

    View Slide

  4. FUNCTIONAL BUILDING BLOCKS
    4

    View Slide

  5. EACH
    No more than a foreach loop wrapped inside of a higher order
    function.
    5

    View Slide

  6. function each($items, $func)
    {
    foreach ($items as $item) {
    $func($item);
    }
    }
    6

    View Slide

  7. Imagine a world without a foreach loop
    7

    View Slide

  8. function each($items, $func)
    {
    for ($i = 0; $i < count($items); $i++) {
    $func($items[$i]);
    }
    }
    8

    View Slide

  9. MAP
    Used to transform each item in an array into something else, creating a
    new array in the process.
    9

    View Slide

  10. function map($items, $func)
    {
    $result = [];
    foreach ($items as $item) {
    $result[] = $func($item);
    }
    return $result;
    }
    10

    View Slide

  11. FILTER
    Used to filter out any elements of an array that you don't want.
    11

    View Slide

  12. function filter($items, $func)
    {
    $result = [];
    foreach ($items as $item) {
    if ($func($item)) {
    $result[] = $func($item);
    }
    }
    return $result;
    }
    12

    View Slide

  13. REJECT
    A close cousin of filter, that simply flips the conditional to be more
    expressive.
    13

    View Slide

  14. REDUCE
    Used to take some array of items and reduce it down to a single value.
    14

    View Slide

  15. function reduce($items, $callback, $initial)
    {
    $accumulator = $initial;
    foreach ($items as $item) {
    $accumulator = $callback($accumulator, $item);
    }
    return $accumulator;
    }
    15

    View Slide

  16. Speaking of patterns....
    16

    View Slide

  17. function map($items, $func)
    {
    return reduce($items, function ($mapped, $item) use ($func) {
    $mapped[] = $func($item);
    return $mapped;
    });
    }
    17

    View Slide

  18. THINKING IN STEPS
    Break a problem into many steps. Turn "I can't because..." into "I could
    if..."
    18

    View Slide

  19. THE PROBLEM WITH PRIMITIVES
    We have to operate on them from the outside by passing them as
    parameters into other functions.
    19

    View Slide

  20. $camelString = lcfirst(
    str_replace(' ', '',
    ucwords(str_replace('_', ' ', $snakeString))
    )
    );
    20

    View Slide

  21. $camelString = $snakeString->replace('_', ' ')
    ->ucwords()
    ->replace(' ', '')
    ->lcfirst();
    21

    View Slide

  22. PRACTICE
    22

    View Slide