SlideShare a Scribd company logo
Umut IŞIK
Functional Programming 1
In PHP
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
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
1. http://en.wikipedia.org/wiki/Functional_programming
2. http://www.haskell.org/haskellwiki/Functional_programming
3. http://www.functionalphp.com/
4. http://blog.lcf.name/2011/12/functional-programming-in-php.
html
5. Resources

More Related Content

PPTX
Functional programming
PPTX
Dev Concepts: Functional Programming
PDF
Some basic FP concepts
PPTX
PHP = PHunctional Programming
PPTX
An Introduction to Functional Programming with Javascript
PPTX
User defined functions
PPTX
Function & Recursion
PPTX
User defined functions in C
Functional programming
Dev Concepts: Functional Programming
Some basic FP concepts
PHP = PHunctional Programming
An Introduction to Functional Programming with Javascript
User defined functions
Function & Recursion
User defined functions in C

What's hot (20)

PDF
Programming in c by pkv
PPT
user defined function
PPT
User defined functions in C programmig
PPTX
predefined and user defined functions
PPT
User Defined Functions
PPTX
structured programming
PDF
Functions and tasks in verilog
PPTX
Procedure n functions
PPTX
Presentation on Function in C Programming
PPT
C FUNCTIONS
PPTX
Function and types
PPTX
Built in function
PPT
Function in C Language
PPTX
Modular programming
PPT
Functions
PPTX
PPT
Verilog Tasks and functions
PDF
Functional Python Webinar from October 22nd, 2014
PPTX
Function in C Programming
Programming in c by pkv
user defined function
User defined functions in C programmig
predefined and user defined functions
User Defined Functions
structured programming
Functions and tasks in verilog
Procedure n functions
Presentation on Function in C Programming
C FUNCTIONS
Function and types
Built in function
Function in C Language
Modular programming
Functions
Verilog Tasks and functions
Functional Python Webinar from October 22nd, 2014
Function in C Programming
Ad

Viewers also liked (20)

PDF
Using mySQL in PHP
PDF
JQuery-Tutorial
PPT
Financial intelligent for start ups
PPTX
Fcp lecture 01
PDF
Presentation & Pitching tips
PPT
Microsoft excel beginner
PPT
Intro to php
PPTX
How to Use Publicity to Grow Your Startup
PPTX
Why Learn PHP Programming?
PDF
Intro to PHP for Beginners
PPT
Computer Programming- Lecture 10
PPTX
Excel training for beginners
PDF
phpTutorial1
PPTX
Beating the decline of the Facebook Organic Reach - KRDS singapore
PPT
Computer Programming- Lecture 9
PPTX
How to present your business plan to investors
PPTX
Comp 107 cep ii
PPTX
Comp 107chp 1
PPTX
9 Tips For Building An Internal Social Media Team
PPT
Excel for beginners class 1
Using mySQL in PHP
JQuery-Tutorial
Financial intelligent for start ups
Fcp lecture 01
Presentation & Pitching tips
Microsoft excel beginner
Intro to php
How to Use Publicity to Grow Your Startup
Why Learn PHP Programming?
Intro to PHP for Beginners
Computer Programming- Lecture 10
Excel training for beginners
phpTutorial1
Beating the decline of the Facebook Organic Reach - KRDS singapore
Computer Programming- Lecture 9
How to present your business plan to investors
Comp 107 cep ii
Comp 107chp 1
9 Tips For Building An Internal Social Media Team
Excel for beginners class 1
Ad

Similar to Functional Programming In PHP I (20)

PPTX
Functional Programming.pptx
PDF
Booting into functional programming
PDF
Python functional programming
PDF
Introduction to functional programming
PDF
(3) cpp procedural programming
PPTX
The joy of functional programming
PPTX
PLSQL-OO [SOUG 2022].pptx
PPTX
Functional programming in python
PDF
Functional programming in python
PPTX
Lecture 1_Functions in C.pptx
PDF
Twins: Object Oriented Programming and Functional Programming
PPTX
Funtional Programming
PPTX
_Python_ Functions _and_ Libraries_.pptx
PDF
Functional Programming in Ruby
PPTX
Functional Paradigm.pptx
PPT
Savitch Ch 04
PPT
Savitch Ch 04
PDF
Intro to JavaScript - Week 2: Function
PPT
Basics of cpp
PPTX
Functional Programming in JavaScript & ESNext
Functional Programming.pptx
Booting into functional programming
Python functional programming
Introduction to functional programming
(3) cpp procedural programming
The joy of functional programming
PLSQL-OO [SOUG 2022].pptx
Functional programming in python
Functional programming in python
Lecture 1_Functions in C.pptx
Twins: Object Oriented Programming and Functional Programming
Funtional Programming
_Python_ Functions _and_ Libraries_.pptx
Functional Programming in Ruby
Functional Paradigm.pptx
Savitch Ch 04
Savitch Ch 04
Intro to JavaScript - Week 2: Function
Basics of cpp
Functional Programming in JavaScript & ESNext

More from Umut IŞIK (20)

PDF
Açık Kaynağa Nasıl Katkı Yapabiliriz?
PDF
Php projelerinde ci_uygulama
PDF
Açık Kaynağa Nasıl Katkı Yapabiliriz?
PDF
A Ci Experience
PDF
BBS Flyers 08/2016
PDF
BBS Flyers 07/2016
PDF
BBS Flyers 05/2016
PDF
BBS Flyers 03/2016
PDF
BBS Flyers 02/2016
PDF
BBS Flyers 01/2016
PDF
BBS Flyers 12/2015
PDF
BBS Flyers 11/2015
PDF
BBS Flyers 10/2015
PDF
BBS Flyers 09/2015
PPTX
Ionic
PDF
BBS Flyers 08/2015
PDF
BBS Flyers-07-2015
PDF
BBS Flyers 06/2015
PDF
BBS Flyers 05/2015
PDF
BBS Flyers 04/2015
Açık Kaynağa Nasıl Katkı Yapabiliriz?
Php projelerinde ci_uygulama
Açık Kaynağa Nasıl Katkı Yapabiliriz?
A Ci Experience
BBS Flyers 08/2016
BBS Flyers 07/2016
BBS Flyers 05/2016
BBS Flyers 03/2016
BBS Flyers 02/2016
BBS Flyers 01/2016
BBS Flyers 12/2015
BBS Flyers 11/2015
BBS Flyers 10/2015
BBS Flyers 09/2015
Ionic
BBS Flyers 08/2015
BBS Flyers-07-2015
BBS Flyers 06/2015
BBS Flyers 05/2015
BBS Flyers 04/2015

Recently uploaded (20)

PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Machine learning based COVID-19 study performance prediction
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Empathic Computing: Creating Shared Understanding
PDF
Electronic commerce courselecture one. Pdf
PPTX
Cloud computing and distributed systems.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
cuic standard and advanced reporting.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Review of recent advances in non-invasive hemoglobin estimation
Machine learning based COVID-19 study performance prediction
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The Rise and Fall of 3GPP – Time for a Sabbatical?
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
The AUB Centre for AI in Media Proposal.docx
Advanced methodologies resolving dimensionality complications for autism neur...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Empathic Computing: Creating Shared Understanding
Electronic commerce courselecture one. Pdf
Cloud computing and distributed systems.
Diabetes mellitus diagnosis method based random forest with bat algorithm
Agricultural_Statistics_at_a_Glance_2022_0.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
cuic standard and advanced reporting.pdf

Functional Programming In PHP I

  • 2. 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 Table Of Contents
  • 3. 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
  • 4. 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
  • 5. 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
  • 6. Summary Two main terms ▪Avoid value assignment ▪Use functions as parameters & return values 1. Introduction
  • 8. 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
  • 9. History (What happened?) ▪A significant change in the modern computing platform with multi-cores ▪Need of parallel processing 1. Introduction
  • 10. What can humble function do better? Functional programing is not just about using functions everywhere. It changes the paradigm. 2. The Benefits
  • 11. 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
  • 12. Changing the paradigm? Let’s think about a simple problem «getting in the house». Special thanks to Alexander Steshenko. (Resources #4) 2. The Benefits
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. Performance ▪Concurrency/Paralel processing ▪Order of execution is not important (side effect free functions ☺ ) 2. The Benefits
  • 18. 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
  • 19. Recursion ▪To avoid using control statements (loops) ▪Be carefull about stop condition ▪Be carefull about variable assignments in recursive function 3. Basics In PHP
  • 20. Recursion Total of price of a shopping cart 3. Basics In PHP
  • 21. 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
  • 22. Map , Reduce and Filter ▪array_map() for map technique ▪array_reduce() for reduce technique ▪array_filter() for reduce technique 3. Basics In PHP
  • 23. Map , Reduce and Filter Map technique for applying discount to shopping cart 3. Basics In PHP
  • 24. Map , Reduce and Filter Reduce technique for calculating total price of shopping cart 3. Basics In PHP
  • 25. Map , Reduce and Filter Filter technique for filtering products according to region from shopping cart 3. Basics In PHP
  • 26. Lambda A function without a formal identifier or name 3. Basics In PHP
  • 27. Closure “An object is data with functions. A closure is a function with data.” — John D. Cook 3. Basics In PHP
  • 28. 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
  • 29. Closure Calculate product price by applying personal discount 3. Basics In PHP
  • 30. A shopping cart implementation 4. Final Example
  • 31. 1. http://en.wikipedia.org/wiki/Functional_programming 2. http://www.haskell.org/haskellwiki/Functional_programming 3. http://www.functionalphp.com/ 4. http://blog.lcf.name/2011/12/functional-programming-in-php. html 5. Resources