Functional Programming In PHP I

Preview:

DESCRIPTION

An introduction to functional programming in PHP

Citation preview

Umut IŞIK

Functional Programming 1In PHP

1. Introduction

2. The Benefits

3. Basics In PHP1. Recursion

2. Map , Reduce And Filter

3. Lambda

4. Closure

4. Final Example

5. Resources

Table Of Contents

According to Wikipedia

… a style of building the structure and elements of computer programs, that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data.

1. Introduction

According to Haskellwiki

..programs are executed by evaluating expressions, … FP (Functional programming) typically avoids using mutable state…

…functions are first-class, which means that they are treated like any other values and can be passed as arguments to other functions or be returned as a result of a function.

1. Introduction

According to Simon Hoywell

Programming in functional way is essentially coding without any assigment of values.

Values can be passed from function to function as arguments and return values.

1. Introduction

Summary

Two main terms

▪Avoid value assignment

▪Use functions as parameters & return values

1. Introduction

Simple Sample

1. Introduction

History

▪Lisp for scientific computers in the late 1950s

▪Scheme and Dylan were later attempts to simplify and improve Lisp

▪ In the 1970s, ML was created

▪The Haskell language began with a consensus in 1987 to form an open standard for functional programming

1. Introduction

History (What happened?)

▪A significant change in the modern computing platform with multi-cores

▪Need of parallel processing

1. Introduction

What can humble function do better?

Functional programing is not just about using functions everywhere. It changes the paradigm.

2. The Benefits

Changing the paradigm?

▪Avoid changing state

▪Break problems down to smallest units

▪Reusable function for smallest units

▪Keep functions as short as possible (even one line)

▪Create too small functions that are not worth naming

▪Remove control statements (hardest part ☺ )

2. The Benefits

Changing the paradigm?

Let’s think about a simple problem «getting in the house».

Special thanks to Alexander Steshenko. (Resources #4)

2. The Benefits

Changing the paradigm?

Imperative getting into the house solution:

1. get the keys out of the pocket

2. pick the right key

3. open the door with the key

4. enter the house

2. The Benefits

Changing the paradigm?

Functional getting into the house solution:

▪enter the house

▪ through the door opened

▪with the right key

▪chosen from all the keys you get out of the pocket

2. The Benefits

Avoid value assignment

▪No state on runtime

▪ Immutable state

▪Code correctness is of special importance

▪More time for algorythm

▪No mocking for global state (so testing is easy ☺ )

2. The Benefits

Functions as parameters and return values

▪Substituting functions with values for tests (Referential transparency)

▪FP pushes you to create reusable functions

▪Higher level of abstractions

2. The Benefits

Performance

▪Concurrency/Paralel processing

▪Order of execution is not important (side effect free functions ☺ )

2. The Benefits

PHP is inherently imperative but

▪ It supports basics of FP

▪ It has lambda function

▪You can protect your code from changing state

▪There are many libraries for FP

3. Basics In PHP

Recursion

▪To avoid using control statements (loops)

▪Be carefull about stop condition

▪Be carefull about variable assignments in recursive function

3. Basics In PHP

Recursion

Total of price of a shopping cart

3. Basics In PHP

Map , Reduce and Filter

▪Map function processes a key/value pair to generate a set of intermediate key/value pairs

▪Reduce function merges all intermediate values associated with the same intermediate key

▪Filter function creates a subset by applying a callback function

3. Basics In PHP

Map , Reduce and Filter

▪array_map() for map technique

▪array_reduce() for reduce technique

▪array_filter() for reduce technique

3. Basics In PHP

Map , Reduce and Filter

Map technique for applying discount to shopping cart

3. Basics In PHP

Map , Reduce and Filter

Reduce technique for calculating total price of shopping cart

3. Basics In PHP

Map , Reduce and Filter

Filter technique for filtering products according to region from shopping cart

3. Basics In PHP

Lambda

A function without a formal identifier or name

3. Basics In PHP

Closure

“An object is data with functions. A closure is a function with data.” — John D. Cook

3. Basics In PHP

Closure

▪Similar role in FP as objects perform in OOP

▪ In PHP a closure is an instance of internal Closure class

▪Very similar to Lambda

▪«use» clause passes variables or closures/functions into closure

3. Basics In PHP

Closure

Calculate product price by applying personal discount

3. Basics In PHP

A shopping cart implementation

4. Final Example

Recommended