FP in OOP
Lessons learned from Functional Programming applied
to Object Oriented Programming
Functional Programming: Why?
OOP was the great paradigm shift of the 90s
FP will be the next great paradigm shift
Excels at parallel and distributed computing
Background: Church-Turing Thesis
Anything that could possibly be computed, can
be computed using any of the following:
● A Turing machine
● The Lambda Calculus
● Recursion
See Google for more details!
/* Factorial in PHP */function factorial($n){
$result = 1;
for ($c = 1; $c <= $n; $c++)$result = $result * $c;
return $result;}
The Imperative Way
The Functional Way
-- Factorial in Haskellfactorial 0 = 1factorial n = n * factorial (n - 1)
Value Objects
Value objects
Immutable
Safe to compose
Easy to test
Use to express as much logic as possible
Value Objects
class Date {private $day, $month, $year;public function __construct($day, $month, $year) { ... }public function isAfter(Date $other) { ... }public function isBefore(Date $other) { ... }public function isSame(Date $other) { ... }
}class DateRange {
private $start, $end;public function __construct(Date $start, Date $end) {
if ($start->isAfter($end)) {throw new InvalidArgumentException(); }
}public function contains(Date $day) {
return $day->isAfter($this->start) &&$day->isBefore($this->end);
}}
Command/Query
Separation
Command/query separation
Isolates state-modifying logic
Queries are safe to reorder, easier to
optimize
Easier to test
Provides opportunity to use different
models for read and write operations
(CQRS)
Command/query separation
class ShoppingCart {
private $total = 0;
private $products = array();
public function calculateSubtotal() {
$subtotal = 0;
foreach ($this->products as $product) {
$subtotal += $product->getPrice();
}
return $subtotal;
}
public function calculateTax() {
return $this->total * 0.075;
}
public function addProduct($product) {
$this->products[] = $product;
}
public function updateTotal($amount) { $this->total = $amount;
}
Command/query separation
$product1 = new Product(25.00);
$product2 = new Product(10.00);
$cart = new ShoppingCart;
$cart->addProduct($product1);
$cart->addProduct($product2);
assert($cart->calculateSubtotal() == 35.00);
$cart->updateTotal($cart->calculateSubtotal());
assert($cart->getTotal() + $cart->calculateTax() ==
37.625);
assert($cart->calculateTax() + $cart->getTotal() ==
37.625);
$cart->updateTotal($cart->getTotal() + $cart-
>calculateTax());
assert($cart->getTotal() == 37.625);
Higher Order Functions
Higher-order functions
Let you parameterize behavior or
designate new behavior at runtime
OOP design patterns:
● Strategy pattern
● Visitor pattern
Strategy Example - PHP 1
abstract class PricingStrategy {private $customer;public function __construct($customer) { ... }abstract public function calculatePrice($prodcut);
}class GoldClubPricing extends PricingStrategy {
public function calculatePrice($prodcut) { ... }}class HighRiskPricing extends PricingStrategy {
public function calculatePrice($prodcut) { ... }}class LoyalCustomerPricing extends PricingStrategy {
public function calculatePrice($prodcut) { ... }}
Strategy Example - PHP 2
class ShoppingCart {private $products;private $customer;private function getPricingStrategy($customer) { ... }
public function calculateSubtotal() {$subtotal = 0;$pricingStrategy = $this->getPricingStrategy($this->customer);
foreach ($this->products as $product) {$subtotal += $pricingStrategy->calculatePrice($product);
}return $subtotal;
}}
Strategy Example - Functional
goldClubPricing product = ...highRiskPricing product = ...loyalCustomerPricing product = ...
getPricingStrategy customer = ...
calculateSubtotal customer products = sum (map pricingStrategy products)where pricingStrategy = getPricingStrategy customer
Other Techniques
Other techniques
Lazy evaluation
Map/Reduce (Folds)
Message passing concurrency and the Actor
model
Thank you!
Further Reading
Patterns of Enterprise Application Architecture -
Martin Fowler
Domain Driven Design - Eric Evans
A Functional Pattern System for Object-Oriented
Design - Thomas Kuhne
The Power of Value - Dan Johnssonhttp://www.infoq.com/presentations/Value-Objects-Dan-Bergh-Johnsson
Objects, Anomalies, and Actors: The Next Revolutionhttp://www.infoq.com/presentations/Objects-Anomalies-and-Actors-The-Next-Revolution
Actor Model Concurrency - Jeff Darcyhttp://pl.atyp.us/wordpress/index.php/2009/07/actor-model-concurrency