This presentation describes basic issues related to functional programming with PHP and solution for most of problems served by the library called PhpSlang.
5. â WHY?
â What can functional programming bring to the table?
â WHAT?
â When language can be described as functional?
â PROBLEMS?
â Top sins of PHP
â SOLUTION?
Table of content
9. WHY?
Pros Cons In result
Efficiency Efficiency Scalability / Quality
Entry threshold Entry threshold Letâs get the party
started
Mathematical
description of
reality
Mathematical
description of
reality
Shorter and more
descriptive code
10. WHY?
Pros Cons In result
Efficiency Efficiency Scalability / Quality
Entry threshold Entry threshold Letâs get the party
started
Mathematical
description of
reality
Mathematical
description of
reality
Shorter and more
descriptive code
INVEST AND WAIT
FOR A DIVIDEND
14. WHAT?
â Function is a first-class citizen
â Lambda Calculus
â Immutability
â No side effects
15. First-class citizen
â Can be stored in variable and data structures
â Can be passed as a parameter to procedure/functions
â Can be returned by procedures/functions
â Can be instantiated inline
â Has itâs own identity (name independent)
35. class Building
{
protected $floors;
public function __construct(int $floors)
{
$this->setFloors($floors);
}
public function getFloors() : int
{
return $this->floors;
}
public function setFloors(int $floors) : Building
{
$this->floors = $floors;
return $this;
}
}
36. use PhpSlangUtilCopy;
class Building
{
use Copy;
protected $floors;
public function __construct(int $floors)
{
$this->floors = $floors;
}
public function getFloors() : int
{
return $this->floors;
}
public function withFloors(int $floors) : Building
{
return $this->copy('floors', $floors);
}
}
80. public function maybeSomething(string $email) : Option<string>
{
...
}
â Version: 0.4.0
â Date: 2016-01-06
â Author:
â Ben Scholzen 'DASPRiD' mail@dasprids.de,
â Rasmus Schultz rasmus@mindplay.dk
â Status: Draft
â First Published at: http://wiki.php.net/rfc/generics
:(
81. public function maybeSomething(string $email) : Option<string>
{
...
}
WANT TO EARN EXTRA $1020 ?
https://www.bountysource.com/issues/20553561-add-generics-support
:~|
113. use PhpSlangCollectionListCollection;
...
public function beautifulMultiplyBy(array $input, float $multiplication) : ListCollection
{
return (new ListCollection($input))
->map(function ($number) use ($multiplication) {
return $number * $multiplication;
});
}
114. use PhpSlangCollectionParallelListCollection;
...
public function beautifulMultiplyBy(array $input, float $multiplication) : ParallelListCollection
{
return (new ParallelListCollection($input))
->map(function ($number) use ($multiplication) {
return $number * $multiplication;
});
}
115. use PhpSlangCollectionListCollection;
...
public function asyncChainedComputationExample(array $input) : ListCollection
{
return (new ListCollection($input))
->map($this->transformationOne());
}
use PhpSlangCollectionParallelListCollection;
...
public function asyncChainedComputationExample(array $input) : ParallelListCollection
{
return (new ParallelListCollection($input))
->map($this->transformationOne());
}
116. use PhpSlangCollectionListCollection;
...
public function asyncChainedComputationExample(array $input) : ListCollection
{
return (new ListCollection($input))
->map($this->transformationOne());
}
use PhpSlangCollectionParallelListCollection;
...
public function asyncChainedComputationExample(array $input) : ParallelListCollection
{
return (new ParallelListCollection($input))
->map($this->transformationOne());
}
118. use PhpSlangCollectionParallelListCollection;
...
public function asyncChainedComputationExample(array $input) : ParallelListCollection
{
return (new ParallelListCollection($input))
->map($this->transformationOne())
->map($this->transformationTwo())
->map($this->transformationThree();
}
119. use PhpSlangCollectionParallelListCollection;
...
public function asyncChainedComputationExample(array $input) : ParallelListCollection
{
return (new ParallelListCollection($input))
->map(function ($number) {
return new Some($number)
->map($this->transformationOne())
->map($this->transformationTwo())
->map($this->transformationThree()
->get();
});
}
120. use PhpSlangCollectionParallelListCollection;
âŠ
public function asyncChainedComputationExample(array $input) : ParallelListCollection
{
return (new ParallelListCollection($input))
->map(function ($number) {
return new Some($number)
->map($this->transformationOne())
->map($this->transformationTwo())
->map($this->transformationThree()
->get();
});
}
121. What do I miss in PHP7 that Scala
luckily has?
127. def fibonacci(index: Int): Int = {
var a = 0
var b = 1
var i = 0
while (i < index) {
val c = a + b
a = b
b = c
i = i + 1
}
return a
}
function fibonacci(int $index) : int
{
$a = 0;
$b = 1;
$i = 0;
while ($i < $index) {
$c = $a + $b;
$a = $b;
$b = $c;
$i += 1;
}
return $a;
}
128. def recursiveFibonacci(n: Int, a:Int, b:Int): Int =
n match {
case 0 => a
case _ => recursiveFibonacci( n-1, b, a+b )
}
def fibonacci( n : Int) : Int =
recursiveFibonacci( n, 0, 1)
function recursiveFibonacci(int $n, int $a, int $b) {
return ($n == 0)
? $a
: recursiveFibonacci($n - 1, $b, $a + $b);
}
function fibonacci(int $n) : int
{
return recursiveFibonacci($n, 0, 1);
}
129. def fibonacci(index : Int) : Int =
index match {
case 0 | 1 => index
case _ => fibonacci(index - 1 ) + fibonacci(index - 2)
}
function fibonacci(int $index) : int
{
return in_array($index, [0, 1])
? $index
: fibonacci($index - 1) + fibonacci($index - 2);
}
130. @tailrec
def recursiveFibonacci(n: Int, a:Int, b:Int): Int =
n match {
case 0 => a
case _ => recursiveFibonacci( n-1, b, a+b )
}
def fibonacci( n : Int) : Int =
recursiveFibonacci( n, 0, 1)
function recursiveFibonacci(int $n, int $a, int $b) {
return ($n == 0)
? $a
: recursiveFibonacci($n - 1, $b, $a + $b);
}
function fibonacci(int $n) : int
{
return recursiveFibonacci($n, 0, 1);
}
:(
:)