functional programming in php i

31
Umut IŞIK Functional Programming 1 In PHP

Upload: umut-isik

Post on 30-Jun-2015

332 views

Category:

Technology


1 download

DESCRIPTION

An introduction to functional programming in PHP

TRANSCRIPT

Page 1: Functional Programming In PHP I

Umut IŞIK

Functional Programming 1In PHP

Page 2: Functional Programming In PHP I

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

Page 3: Functional Programming In PHP I

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

Page 4: Functional Programming In PHP I

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

Page 5: Functional Programming In PHP I

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

Page 6: Functional Programming In PHP I

Summary

Two main terms

▪Avoid value assignment

▪Use functions as parameters & return values

1. Introduction

Page 7: Functional Programming In PHP I

Simple Sample

1. Introduction

Page 8: Functional Programming In PHP I

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

Page 9: Functional Programming In PHP I

History (What happened?)

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

▪Need of parallel processing

1. Introduction

Page 10: Functional Programming In PHP I

What can humble function do better?

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

2. The Benefits

Page 11: Functional Programming In PHP I

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

Page 12: Functional Programming In PHP I

Changing the paradigm?

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

Special thanks to Alexander Steshenko. (Resources #4)

2. The Benefits

Page 13: Functional Programming In PHP I

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

Page 14: Functional Programming In PHP I

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

Page 15: Functional Programming In PHP I

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

Page 16: Functional Programming In PHP I

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

Page 17: Functional Programming In PHP I

Performance

▪Concurrency/Paralel processing

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

2. The Benefits

Page 18: Functional Programming In PHP I

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

Page 19: Functional Programming In PHP I

Recursion

▪To avoid using control statements (loops)

▪Be carefull about stop condition

▪Be carefull about variable assignments in recursive function

3. Basics In PHP

Page 20: Functional Programming In PHP I

Recursion

Total of price of a shopping cart

3. Basics In PHP

Page 21: Functional Programming In PHP I

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

Page 22: Functional Programming In PHP I

Map , Reduce and Filter

▪array_map() for map technique

▪array_reduce() for reduce technique

▪array_filter() for reduce technique

3. Basics In PHP

Page 23: Functional Programming In PHP I

Map , Reduce and Filter

Map technique for applying discount to shopping cart

3. Basics In PHP

Page 24: Functional Programming In PHP I

Map , Reduce and Filter

Reduce technique for calculating total price of shopping cart

3. Basics In PHP

Page 25: Functional Programming In PHP I

Map , Reduce and Filter

Filter technique for filtering products according to region from shopping cart

3. Basics In PHP

Page 26: Functional Programming In PHP I

Lambda

A function without a formal identifier or name

3. Basics In PHP

Page 27: Functional Programming In PHP I

Closure

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

3. Basics In PHP

Page 28: Functional Programming In PHP I

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

Page 29: Functional Programming In PHP I

Closure

Calculate product price by applying personal discount

3. Basics In PHP

Page 30: Functional Programming In PHP I

A shopping cart implementation

4. Final Example