Functional Programming In PHP I

33 %
67 %
Information about Functional Programming In PHP I

Published on September 28, 2014

Author: UmutIIK



An introduction to functional programming in PHP

Functional Programming 1 In PHP Umut IŞIK

Table Of Contents 1. Introduction 2. The Benefits 3. Basics In PHP 1. Recursion 2. Map , Reduce And Filter 3. Lambda 4. Closure 4. Final Example 5. Resources

1. Introduction 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 Paradigma ▪ Avoid changing state ▪ Keep functions as shor as possible (even one line) ▪ Break problems down to smallest units ▪ Reusable function for smallest units ▪ Remove control statements (hardest part ☺ )

1. Introduction Simple Sample

2. The Benefits What can humble function do better? Functional programing is not just about using functions everywhere. It changes the paradigm.

Changing the paradimg? Let’s think about a simple problem «getting in the house». Special thanks to Alexander Steshenko. (Resources #4) 2. The Benefits

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 static values for tests ▪ FP pushes you to create reusable functions ▪ Higher level of abstractions

3. Basics In PHP 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 ▪ 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

4. Final Example A shopping cart implementation

5. Resources 1. 2. 3. 4. html

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Functional Programming in PHP - PHP: The Right Way

Functional Programming in PHP. PHP supports first-class functions, meaning that a function can be assigned to a variable. Both user-defined and built-in ...
Read more

PHP Master | Functional Programming in PHP

Challenge your procedural way of thinking with this article and learn the basic concepts of functional programming in PHP.
Read more

Functional Programming in PHP Book · Learn to program in ...

Learn advanced and powerful functional programming techniques with the Functional Programming in PHP Book.
Read more

Functional Programming in PHP - Learn How To Code by ...

Functional Programming Principles. In functional programming, you have no data represented by variables. In functional programming everything is a function.
Read more

Functional Programming in PHP - Liip Blog - Liip

Use functional programming in PHP to improve the readability and maintainability of your code. Gain confidence in your tests by using pure functions
Read more

Functional Programming – How to Write Functional Code in PHP

Article: Functional Programming - How to Write Functional Code in PHP, closures, currying, functional programming, functions, programming, recursion
Read more

Functional programming - Wikipedia, the free encyclopedia

... functional programming is a programming paradigm—a style of building the structure and ... This is also true of the PHP programming ...
Read more

The state of functional programming in PHP

With the rise of Javascript, and languages like Python and Ruby, functional programming is becoming more mainstream. Even Java seems to be getting closures ...
Read more

Functional programming in PHP - /usr/portage — Lars Strojny

Functional programming in PHP 16. PHP has traditionally been a simple, procedural language that took a lot of inspiration from C and Perl. Both syntax wise ...
Read more

Functional Programming in PHP « php[architect ...

Book Details Title Functional Programming in PHP ISBN 978-1940111056 Pages 118 Digital Formats PDF, ePub, Mobi Author Simon Holywell
Read more