Slide 1

Slide 1 text

Curing the Common Loop @adamwathan

Slide 2

Slide 2 text

$totalRevenue = 0; $minSales = 75000; foreach ($employees as $employee) { $employeeType = $employee->getType(); if ($employeeType == 'Sales') { $customers = $employee->getCustomers(); foreach ($customers as $customer) { $customerTotalSales = 0; foreach ($customer->getOrders() as $order) { $customerTotalSales += $order->total; } if ($customerTotalSales >= $minSales) { $totalRevenue += $customerTotalSales; } } } } return $totalRevenue;

Slide 3

Slide 3 text

Higher Order FUNCTIONS

Slide 4

Slide 4 text

Route::get('/', function () { return view('welcome'); });

Slide 5

Slide 5 text

Mail::send('order-shipped', $data, function ($m) { $m->to($this->customer->email); $m->subject("Your order has shipped!"); });

Slide 6

Slide 6 text

DB::transaction(function () use ($user) { $user->subscription()->delete(); $user->update([ 'cancelled_at' => Carbon::now() ]); });

Slide 7

Slide 7 text

Afford more powerful ABSTRACTIONS

Slide 8

Slide 8 text

For example…

Slide 9

Slide 9 text

$customerEmails = []; foreach ($customers as $customer) { $customerEmails[] = $customer->email; } return $customerEmails;

Slide 10

Slide 10 text

$stockTotals = []; foreach ($inventoryItems as $item) { $stockTotals[] = [ 'product' => $item->productName, 'total_value' => $item->quantity * $item->price, ]; } return $stockTotals;

Slide 11

Slide 11 text

$customerEmails = []; foreach ($customers as $customer) { $customerEmails[] = $customer->email; } return $customerEmails;

Slide 12

Slide 12 text

$customerEmails = []; foreach ($customers as $customer) { $email = $customer->email; $customerEmails[] = $email; } return $customerEmails;

Slide 13

Slide 13 text

$stockTotals = []; foreach ($inventoryItems as $item) { $stockTotals[] = [ 'product' => $item->productName, 'total_value' => $item->quantity * $item->price, ]; } return $stockTotals;

Slide 14

Slide 14 text

$stockTotals = []; foreach ($inventoryItems as $item) { $stockTotal = [ 'product' => $item->productName, 'total_value' => $item->quantity * $item->price, ]; $stockTotals[] = $stockTotal; } return $stockTotals;

Slide 15

Slide 15 text

$stockTotals = []; foreach ($inventoryItems as $item) { $stockTotal = [ 'product' => $item->productName, 'total_value' => $item->quantity * $item->price, ]; $stockTotals[] = $stockTotal; } return $stockTotals;

Slide 16

Slide 16 text

$results = []; foreach ($items as $item) { $result = [ 'product' => $item->productName, 'total_value' => $item->quantity * $item->price, ]; $results[] = $result; } return $results;

Slide 17

Slide 17 text

$results = []; foreach ($items as $item) { $result = $item->email; $results[] = $result; } return $results;

Slide 18

Slide 18 text

$results = []; foreach ($items as $item) { $result = $item->email; $results[] = $result; } return $results;

Slide 19

Slide 19 text

$callback = function ($item) { return $item->email; }; $results = []; foreach ($items as $item) { $result = $callback($item); $results[] = $result; } return $results;

Slide 20

Slide 20 text

$results = []; foreach ($items as $item) { $result = [ 'product' => $item->productName, 'total_value' => $item->quantity * $item->price, ]; $results[] = $result; } return $results;

Slide 21

Slide 21 text

$callback = function ($item) { return [ 'product' => $item->productName, 'total_value' => $item->quantity * $item->price, ]; }; $results = []; foreach ($items as $item) { $result = $callback($item); $results[] = $result; } return $results;

Slide 22

Slide 22 text

$callback = function ($item) { return [ 'product' => $item->productName, 'total_value' => $item->quantity * $item->price, ]; }; $results = []; foreach ($items as $item) { $result = $callback($item); $results[] = $result; } return $results;

Slide 23

Slide 23 text

$callback = function ($item) { return $item->email; }; $results = []; foreach ($items as $item) { $result = $callback($item); $results[] = $result; } return $results;

Slide 24

Slide 24 text

function array_map($callback, $items) { } $results = []; foreach ($items as $item) { $result = $callback($item); $results[] = $result; } return $results;

Slide 25

Slide 25 text

$customerEmails = []; foreach ($customers as $customer) { $customerEmails[] = $customer->email; } return $customerEmails;

Slide 26

Slide 26 text

$customerEmails = array_map(function ($customer) { return $customer->email; }, $customers);

Slide 27

Slide 27 text

$stockTotals = []; foreach ($inventoryItems as $item) { $stockTotals[] = [ 'product' => $item->productName, 'total_value' => $item->quantity * $item->price, ]; } return $stockTotals;

Slide 28

Slide 28 text

$stockTotals = []; foreach ($inventoryItems as $item) { $stockTotals[] = [ 'product' => $item->productName, 'total_value' => $item->quantity * $item->price, ]; } return $stockTotals;

Slide 29

Slide 29 text

$stockTotals = array_map(function ($inventoryItem) { return [ 'product' => $item->productName, 'total_value' => $item->quantity * $item->price, ]; }, $inventoryItems);

Slide 30

Slide 30 text

Another…

Slide 31

Slide 31 text

$outOfStockProducts = []; foreach ($products as $product) { if ($product->isOutOfStock()) { $outOfStockProducts[] = $product; } } return $outOfStockProducts;

Slide 32

Slide 32 text

$expensiveProducts = []; foreach ($products as $product) { if ($product->price > 100) { $expensiveProducts[] = $product; } } return $expensiveProducts;

Slide 33

Slide 33 text

$results = []; foreach ($items as $item) { if ($item->price > 100) { $results[] = $item; } } return $results;

Slide 34

Slide 34 text

$outOfStockProducts = []; foreach ($products as $product) { if ($product->isOutOfStock()) { $outOfStockProducts[] = $product; } } return $outOfStockProducts;

Slide 35

Slide 35 text

$results = []; foreach ($items as $item) { if ($item->isOutOfStock()) { $results[] = $item; } } return $results;

Slide 36

Slide 36 text

$results = []; foreach ($items as $item) { if ($item->isOutOfStock()) { $results[] = $item; } } return $results;

Slide 37

Slide 37 text

$results = []; foreach ($items as $item) { if ($item->price > 100) { $results[] = $item; } } return $results;

Slide 38

Slide 38 text

$callback = function ($item) { return $item->price > 100; }; $results = []; foreach ($items as $item) { if ($callback($item)) { $results[] = $item; } } return $results;

Slide 39

Slide 39 text

$results = []; foreach ($items as $item) { if ($item->isOutOfStock()) { $results[] = $item; } } return $results;

Slide 40

Slide 40 text

$callback = function ($item) { return $item->isOutOfStock(); }; $results = []; foreach ($items as $item) { if ($callback($item)) { $results[] = $item; } } return $results;

Slide 41

Slide 41 text

$callback = function ($item) { return $item->isOutOfStock(); }; $results = []; foreach ($items as $item) { if ($callback($item)) { $results[] = $item; } } return $results;

Slide 42

Slide 42 text

$callback = function ($item) { return $item->price > 100; }; $results = []; foreach ($items as $item) { if ($callback($item)) { $results[] = $item; } } return $results;

Slide 43

Slide 43 text

function array_filter($items, $callback) { $results = []; foreach ($items as $item) { if ($callback($item)) { $results[] = $item; } } return $results; } $results = []; foreach ($items as $item) { if ($callback($item)) { $results[] = $item; } } return $results;

Slide 44

Slide 44 text

$outOfStockProducts = []; foreach ($products as $product) { if ($product->isOutOfStock()) { $outOfStockProducts[] = $product; } } return $outOfStockProducts;

Slide 45

Slide 45 text

$outOfStockProducts = array_filter($products, function ($product) { return $product->isOutOfStock(); });

Slide 46

Slide 46 text

$expensiveProducts = []; foreach ($products as $product) { if ($product->price > 100) { $expensiveProducts[] = $product; } } return $expensiveProducts;

Slide 47

Slide 47 text

$expensiveProducts = array_filter($products, function ($product) { return $product->price > 100; });

Slide 48

Slide 48 text

function getSalesEmails($employees) { $emails = []; foreach ($employees as $employee) { if ($employee->department === 'Sales') { $emails[] = $employee->email; } } return $emails; }

Slide 49

Slide 49 text

function getSalesEmails($employees) { $emails = []; foreach ($employees as $employee) { if ($employee->department === 'Sales') { $emails[] = $employee->email; } } return $emails; } …map?

Slide 50

Slide 50 text

function getSalesEmails($employees) { $emails = []; foreach ($employees as $employee) { if ($employee->department === 'Sales') { $emails[] = $employee->email; } } return $emails; }

Slide 51

Slide 51 text

Thinking in STEPS

Slide 52

Slide 52 text

function getSalesEmails($employees) { $emails = []; foreach ($employees as $employee) { if ($employee->department === 'Sales') { $emails[] = $employee->email; } } return $emails; }

Slide 53

Slide 53 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); $emails = []; foreach ($employees as $employee) { if ($employee->department === 'Sales') { $emails[] = $employee->email; } } return $emails; }

Slide 54

Slide 54 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); $emails = []; foreach ($employees as $employee) { if ($employee->department === 'Sales') { $emails[] = $employee->email; } } return $emails; }

Slide 55

Slide 55 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); $emails = []; foreach ($employees as $employee) { if ($employee->department === 'Sales') { $emails[] = $employee->email; } } return $emails; }

Slide 56

Slide 56 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); $emails = []; foreach ($salesEmployees as $employee) { if ($employee->department === 'Sales') { $emails[] = $employee->email; } } return $emails; }

Slide 57

Slide 57 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); $emails = []; foreach ($salesEmployees as $employee) { if ($employee->department === 'Sales') { $emails[] = $employee->email; } } return $emails; }

Slide 58

Slide 58 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); $emails = []; foreach ($salesEmployees as $employee) { $emails[] = $employee->email; } return $emails; }

Slide 59

Slide 59 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); $emails = []; foreach ($salesEmployees as $employee) { $emails[] = $employee->email; } return $emails; }

Slide 60

Slide 60 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); $emails = []; foreach ($salesEmployees as $employee) { $emails[] = $employee->email; } return $emails; } Map!

Slide 61

Slide 61 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); $emails = []; foreach ($salesEmployees as $employee) { $emails[] = $employee->email; } return $emails; }

Slide 62

Slide 62 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); $emails = array_map(function ($employee) { return $employee->email; }, $salesEmployees); return $emails; }

Slide 63

Slide 63 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); $emails = array_map(function ($employee) { return $employee->email; }, $salesEmployees); return $emails; }

Slide 64

Slide 64 text

Things I HATE

Slide 65

Slide 65 text

#1. FOR LOOPS #2. CONDITIONALS #3. TEMP VARIABLES

Slide 66

Slide 66 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); $emails = array_map(function ($employee) { return $employee->email; }, $salesEmployees); return $emails; }

Slide 67

Slide 67 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); $emails = array_map(function ($employee) { return $employee->email; }, $salesEmployees); return $emails; }

Slide 68

Slide 68 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); $emails = array_map(function ($employee) { return $employee->email; }, $salesEmployees); return $emails; }

Slide 69

Slide 69 text

Inline TEMP

Slide 70

Slide 70 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); $emails = array_map(function ($employee) { return $employee->email; }, $salesEmployees); return $emails; }

Slide 71

Slide 71 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); return array_map(function ($employee) { return $employee->email; }, $salesEmployees); }

Slide 72

Slide 72 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); return array_map(function ($employee) { return $employee->email; }, $salesEmployees); }

Slide 73

Slide 73 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); return array_map(function ($employee) { return $employee->email; }, $salesEmployees); }

Slide 74

Slide 74 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); return array_map(function ($employee) { return $employee->email; }, $salesEmployees); }

Slide 75

Slide 75 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); return array_map(function ($employee) { return $employee->email; }, array_filter($employees, function ($employee) { return $employee->department == 'Sales'; })); }

Slide 76

Slide 76 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); return array_map(function ($employee) { return $employee->email; }, array_filter($employees, function ($employee) { return $employee->department == 'Sales'; })); }

Slide 77

Slide 77 text

function getSalesEmails($employees) { return array_map(function ($employee) { return $employee->email; }, array_filter($employees, function ($employee) { return $employee->department == 'Sales'; })); }

Slide 78

Slide 78 text

The Problem With PRIMITIVES

Slide 79

Slide 79 text

# => hello_there_laracon function toCamelCase($snakeString) { return lcfirst( str_replace(' ', '', ucwords(str_replace('_', ' ', $snakeString)) ) ); } # => helloThereLaracon

Slide 80

Slide 80 text

# => hello_there_laracon function toCamelCase($snakeString) { return $snakeString->replace('_', ' ') ->ucwords() ->replace(' ', '') ->lcfirst(); } # => helloThereLaracon

Slide 81

Slide 81 text

Introducing COLLECTIONS

Slide 82

Slide 82 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); $emails = array_map(function ($employee) { return $employee->email; }, $salesEmployees); return $emails; }

Slide 83

Slide 83 text

function getSalesEmails($employees) { $salesEmployees = array_filter($employees, function ($employee) { return $employee->department == 'Sales'; }); $emails = array_map(function ($employee) { return $employee->email; }, $salesEmployees); return $emails; }

Slide 84

Slide 84 text

function getSalesEmails($employees) { $salesEmployees = $employees->filter(function ($employee) { return $employee->department == 'Sales'; }); $emails = array_map(function ($employee) { return $employee->email; }, $salesEmployees); return $emails; }

Slide 85

Slide 85 text

function getSalesEmails($employees) { $salesEmployees = $employees->filter(function ($employee) { return $employee->department == 'Sales'; }); $emails = array_map(function ($employee) { return $employee->email; }, $salesEmployees); return $emails; }

Slide 86

Slide 86 text

function getSalesEmails($employees) { $salesEmployees = $employees->filter(function ($employee) { return $employee->department == 'Sales'; }); $emails = $salesEmployees->map(function ($employee) { return $employee->email; }); return $emails; }

Slide 87

Slide 87 text

function getSalesEmails($employees) { return $employees->filter(function ($employee) { return $employee->department == 'Sales'; })->map(function ($employee) { return $employee->email; }); }

Slide 88

Slide 88 text

Let’s write SOME CODE!

Slide 89

Slide 89 text

http://www.fullstackradio.com FULL RADIO STACK

Slide 90

Slide 90 text

@adamwathan adamwathan.me