SlideShare a Scribd company logo
1 of 15
Dev Concepts
What is
Functional
Programming?
Pure Functions,
Lambda Functions,
Higher Order
2
 Functional programming (FP)
 Programming by composing pure functions, avoiding
shared state, mutable data, and side-effects
 Declarative programing approach (not imperative)
 Program state flows through pure functions
 Pure function == function, which returns value only
determined by its input, without side effects
 Examples: sqrt(x), sort(list)  sorted list
 Pure function == consistent result
Functional Programming
Functional
Languages
Pure Functional and
Multi-Paradigm Languages
4
 Purely functional languages are unpractical and rarely used
 The program is pure function without side effects, e.g. Haskell
 Impure functional languages
 Emphasize functional style, but allow side effects, e.g. Clojure
 Multi-paradigm languages
 Combine multiple programing paradigms:
functional, structured, object-oriented, …
 Examples: JavaScript, C#, Python, Java
Functional Programming Languages
The
Functional
Paradigm in
Action
Example of Functional
Programming Style
6
 Read several numbers and find the biggest of them (in C#)
Functional Programming – Examples
Console.WriteLine(
Console.ReadLine()
.Split(" ")
.Select(int.Parse)
.Max()
);
 Functional style
var input = Console.ReadLine();
var items = input.Split(" ");
var nums = items.Select(int.Parse);
var maxNum = nums.Max();
Console.WriteLine(maxNum);
 Imperative style
Live Demo
Functional
Programming (FP)
https://repl.it/@nakov/functional-max-num-cs
https://repl.it/@nakov/imperative-max-num-cs
Lambda
Functions
Lambda and
First-Class
9
 Lambda functions: anonymous function (formula)
 JS, Python, C# and Java and support first-class functions
(functions can be stored in variables and passed as arguments)
Lambda and First-Class Functions
x => 2 * x C# x => 2 * x JS
Func<int, int> twice =
x => 2 * x;
var d = twice(5); // 10
C#
let twice = x => 2 * x;
let d = twice(5); // 10
JS
lambda x: 2 * x Python
twice = lambda x: 2 * x
d = twice(5) # 10
Python
Function<Integer, Integer>
twice = x -> 2 * x;
var d = twice.apply(5); Java
Live Demo
First-Class Functions
https://repl.it/@nakov/first-class-function-js
Higher-Order
Functions
Functions as
Arguments to
Other Functions
12
 Higher-order functions take other functions as arguments
Higher Order Functions
function aggregate(start, end, func) {
for (var result = start, i = start+1; i <= end; i++)
result = func(result, i);
return result;
}
aggregate(1, 10, (a, b) => a + b) // 55
aggregate(1, 10, (a, b) => a * b) // 3628800
aggregate(1, 10, (a, b) => '' + a + b) // "12345678910"
Live Demo
Higher-Order
Functions
https://repl.it/@nakov/higher-order-functions-js
 …
 …
 …
Next Steps
 Join the SoftUni "Learn To Code" Community
 Access the Free Dev Lessons
 Get Help from the Mentors
 Meet the Other Learners
https://softuni.org
 …
 …
 …
Join the SoftUni Community
softuni.o
rg

More Related Content

What's hot

Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overridingRajab Ali
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)Ritika Sharma
 
Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Omar Abdelhafith
 
Functional Programming with JavaScript
Functional Programming with JavaScriptFunctional Programming with JavaScript
Functional Programming with JavaScriptAung Baw
 
Introduction to functional programming
Introduction to functional programmingIntroduction to functional programming
Introduction to functional programmingKonrad Szydlo
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript FundamentalsSrdjan Strbanovic
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional ProgrammingGeison Goes
 
Python functional programming
Python functional programmingPython functional programming
Python functional programmingGeison Goes
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Kamlesh Makvana
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++Learn By Watch
 
Introduction To Functional Programming
Introduction To Functional ProgrammingIntroduction To Functional Programming
Introduction To Functional Programmingnewmedio
 
Functional programming in JavaScript
Functional programming in JavaScriptFunctional programming in JavaScript
Functional programming in JavaScriptJoseph Smith
 
Intro to functional programming
Intro to functional programmingIntro to functional programming
Intro to functional programmingAssaf Gannon
 
Functional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smartFunctional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smartChen Fisher
 
Functional programming and ruby in functional style
Functional programming and ruby in functional styleFunctional programming and ruby in functional style
Functional programming and ruby in functional styleNiranjan Sarade
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScripttmont
 

What's hot (20)

Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 
Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)
 
Functional Programming with JavaScript
Functional Programming with JavaScriptFunctional Programming with JavaScript
Functional Programming with JavaScript
 
Introduction to functional programming
Introduction to functional programmingIntroduction to functional programming
Introduction to functional programming
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript Fundamentals
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional Programming
 
Scala functions
Scala functionsScala functions
Scala functions
 
Function
FunctionFunction
Function
 
Python functional programming
Python functional programmingPython functional programming
Python functional programming
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
 
3 Function Overloading
3 Function Overloading3 Function Overloading
3 Function Overloading
 
Introduction To Functional Programming
Introduction To Functional ProgrammingIntroduction To Functional Programming
Introduction To Functional Programming
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Functional programming in JavaScript
Functional programming in JavaScriptFunctional programming in JavaScript
Functional programming in JavaScript
 
Intro to functional programming
Intro to functional programmingIntro to functional programming
Intro to functional programming
 
Functional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smartFunctional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smart
 
Functional programming and ruby in functional style
Functional programming and ruby in functional styleFunctional programming and ruby in functional style
Functional programming and ruby in functional style
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
 

Similar to Dev Concepts: Functional Programming

Advance python programming
Advance python programming Advance python programming
Advance python programming Jagdish Chavan
 
Functional Programming.pptx
Functional Programming.pptxFunctional Programming.pptx
Functional Programming.pptxKarthickT28
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAdam Getchell
 
SeneJug java_8_prez_122015
SeneJug java_8_prez_122015SeneJug java_8_prez_122015
SeneJug java_8_prez_122015senejug
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpuDhaval Jalalpara
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scaladatamantra
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxKhurramKhan173
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1YOGESH SINGH
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 
Intro f# functional_programming
Intro f# functional_programmingIntro f# functional_programming
Intro f# functional_programmingMauro Ghiani
 
Introduction to python & its applications.ppt
Introduction to python & its applications.pptIntroduction to python & its applications.ppt
Introduction to python & its applications.pptPradeepNB2
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextUnfold UI
 

Similar to Dev Concepts: Functional Programming (20)

Advance python programming
Advance python programming Advance python programming
Advance python programming
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Functional Programming.pptx
Functional Programming.pptxFunctional Programming.pptx
Functional Programming.pptx
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 
SeneJug java_8_prez_122015
SeneJug java_8_prez_122015SeneJug java_8_prez_122015
SeneJug java_8_prez_122015
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C# programming
C# programming C# programming
C# programming
 
Intro f# functional_programming
Intro f# functional_programmingIntro f# functional_programming
Intro f# functional_programming
 
Functions
FunctionsFunctions
Functions
 
Introduction to python & its applications.ppt
Introduction to python & its applications.pptIntroduction to python & its applications.ppt
Introduction to python & its applications.ppt
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNext
 
Functional programming 101
Functional programming 101Functional programming 101
Functional programming 101
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 

More from Svetlin Nakov

BG-IT-Edu: отворено учебно съдържание за ИТ учители
BG-IT-Edu: отворено учебно съдържание за ИТ учителиBG-IT-Edu: отворено учебно съдържание за ИТ учители
BG-IT-Edu: отворено учебно съдържание за ИТ учителиSvetlin Nakov
 
Programming World in 2024
Programming World in 2024Programming World in 2024
Programming World in 2024Svetlin Nakov
 
AI Tools for Business and Startups
AI Tools for Business and StartupsAI Tools for Business and Startups
AI Tools for Business and StartupsSvetlin Nakov
 
AI Tools for Scientists - Nakov (Oct 2023)
AI Tools for Scientists - Nakov (Oct 2023)AI Tools for Scientists - Nakov (Oct 2023)
AI Tools for Scientists - Nakov (Oct 2023)Svetlin Nakov
 
AI Tools for Entrepreneurs
AI Tools for EntrepreneursAI Tools for Entrepreneurs
AI Tools for EntrepreneursSvetlin Nakov
 
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023Svetlin Nakov
 
AI Tools for Business and Personal Life
AI Tools for Business and Personal LifeAI Tools for Business and Personal Life
AI Tools for Business and Personal LifeSvetlin Nakov
 
Дипломна работа: учебно съдържание по ООП - Светлин Наков
Дипломна работа: учебно съдържание по ООП - Светлин НаковДипломна работа: учебно съдържание по ООП - Светлин Наков
Дипломна работа: учебно съдържание по ООП - Светлин НаковSvetlin Nakov
 
Дипломна работа: учебно съдържание по ООП
Дипломна работа: учебно съдържание по ООПДипломна работа: учебно съдържание по ООП
Дипломна работа: учебно съдържание по ООПSvetlin Nakov
 
Свободно ИТ учебно съдържание за учители по програмиране и ИТ
Свободно ИТ учебно съдържание за учители по програмиране и ИТСвободно ИТ учебно съдържание за учители по програмиране и ИТ
Свободно ИТ учебно съдържание за учители по програмиране и ИТSvetlin Nakov
 
AI and the Professions of the Future
AI and the Professions of the FutureAI and the Professions of the Future
AI and the Professions of the FutureSvetlin Nakov
 
Programming Languages Trends for 2023
Programming Languages Trends for 2023Programming Languages Trends for 2023
Programming Languages Trends for 2023Svetlin Nakov
 
IT Professions and How to Become a Developer
IT Professions and How to Become a DeveloperIT Professions and How to Become a Developer
IT Professions and How to Become a DeveloperSvetlin Nakov
 
GitHub Actions (Nakov at RuseConf, Sept 2022)
GitHub Actions (Nakov at RuseConf, Sept 2022)GitHub Actions (Nakov at RuseConf, Sept 2022)
GitHub Actions (Nakov at RuseConf, Sept 2022)Svetlin Nakov
 
IT Professions and Their Future
IT Professions and Their FutureIT Professions and Their Future
IT Professions and Their FutureSvetlin Nakov
 
How to Become a QA Engineer and Start a Job
How to Become a QA Engineer and Start a JobHow to Become a QA Engineer and Start a Job
How to Become a QA Engineer and Start a JobSvetlin Nakov
 
Призвание и цели: моята рецепта
Призвание и цели: моята рецептаПризвание и цели: моята рецепта
Призвание и цели: моята рецептаSvetlin Nakov
 
What Mongolian IT Industry Can Learn from Bulgaria?
What Mongolian IT Industry Can Learn from Bulgaria?What Mongolian IT Industry Can Learn from Bulgaria?
What Mongolian IT Industry Can Learn from Bulgaria?Svetlin Nakov
 
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)How to Become a Software Developer - Nakov in Mongolia (Oct 2022)
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)Svetlin Nakov
 
Blockchain and DeFi Overview (Nakov, Sept 2021)
Blockchain and DeFi Overview (Nakov, Sept 2021)Blockchain and DeFi Overview (Nakov, Sept 2021)
Blockchain and DeFi Overview (Nakov, Sept 2021)Svetlin Nakov
 

More from Svetlin Nakov (20)

BG-IT-Edu: отворено учебно съдържание за ИТ учители
BG-IT-Edu: отворено учебно съдържание за ИТ учителиBG-IT-Edu: отворено учебно съдържание за ИТ учители
BG-IT-Edu: отворено учебно съдържание за ИТ учители
 
Programming World in 2024
Programming World in 2024Programming World in 2024
Programming World in 2024
 
AI Tools for Business and Startups
AI Tools for Business and StartupsAI Tools for Business and Startups
AI Tools for Business and Startups
 
AI Tools for Scientists - Nakov (Oct 2023)
AI Tools for Scientists - Nakov (Oct 2023)AI Tools for Scientists - Nakov (Oct 2023)
AI Tools for Scientists - Nakov (Oct 2023)
 
AI Tools for Entrepreneurs
AI Tools for EntrepreneursAI Tools for Entrepreneurs
AI Tools for Entrepreneurs
 
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
 
AI Tools for Business and Personal Life
AI Tools for Business and Personal LifeAI Tools for Business and Personal Life
AI Tools for Business and Personal Life
 
Дипломна работа: учебно съдържание по ООП - Светлин Наков
Дипломна работа: учебно съдържание по ООП - Светлин НаковДипломна работа: учебно съдържание по ООП - Светлин Наков
Дипломна работа: учебно съдържание по ООП - Светлин Наков
 
Дипломна работа: учебно съдържание по ООП
Дипломна работа: учебно съдържание по ООПДипломна работа: учебно съдържание по ООП
Дипломна работа: учебно съдържание по ООП
 
Свободно ИТ учебно съдържание за учители по програмиране и ИТ
Свободно ИТ учебно съдържание за учители по програмиране и ИТСвободно ИТ учебно съдържание за учители по програмиране и ИТ
Свободно ИТ учебно съдържание за учители по програмиране и ИТ
 
AI and the Professions of the Future
AI and the Professions of the FutureAI and the Professions of the Future
AI and the Professions of the Future
 
Programming Languages Trends for 2023
Programming Languages Trends for 2023Programming Languages Trends for 2023
Programming Languages Trends for 2023
 
IT Professions and How to Become a Developer
IT Professions and How to Become a DeveloperIT Professions and How to Become a Developer
IT Professions and How to Become a Developer
 
GitHub Actions (Nakov at RuseConf, Sept 2022)
GitHub Actions (Nakov at RuseConf, Sept 2022)GitHub Actions (Nakov at RuseConf, Sept 2022)
GitHub Actions (Nakov at RuseConf, Sept 2022)
 
IT Professions and Their Future
IT Professions and Their FutureIT Professions and Their Future
IT Professions and Their Future
 
How to Become a QA Engineer and Start a Job
How to Become a QA Engineer and Start a JobHow to Become a QA Engineer and Start a Job
How to Become a QA Engineer and Start a Job
 
Призвание и цели: моята рецепта
Призвание и цели: моята рецептаПризвание и цели: моята рецепта
Призвание и цели: моята рецепта
 
What Mongolian IT Industry Can Learn from Bulgaria?
What Mongolian IT Industry Can Learn from Bulgaria?What Mongolian IT Industry Can Learn from Bulgaria?
What Mongolian IT Industry Can Learn from Bulgaria?
 
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)How to Become a Software Developer - Nakov in Mongolia (Oct 2022)
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)
 
Blockchain and DeFi Overview (Nakov, Sept 2021)
Blockchain and DeFi Overview (Nakov, Sept 2021)Blockchain and DeFi Overview (Nakov, Sept 2021)
Blockchain and DeFi Overview (Nakov, Sept 2021)
 

Recently uploaded

Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 

Recently uploaded (20)

Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 

Dev Concepts: Functional Programming

Editor's Notes

  1. Hello, I am Svetlin Nakov from SoftUni and I am here for the next episode from my "Dev Concepts" series. In this lesson I will explain the functional programming paradigm. Functional programming (FP) is a style of programming based on composing pure functions, while avoiding shared state, mutable data, and side-effects. It's important, because most modern mainstream programming languages (like JavaScript, Python, Java and C#) support the functional paradigm with lambda functions and higher-order functions. In this lesson I will explain and demonstrate through live coding examples in JavaScript, Python, Java and C# the concepts of functional programming, pure functions, first-class functions, lambda functions, higher-order functions, and how to combine these concepts to implement filtering, mapping, ordering and other transformations over sequences of elements. Are you ready? Let's dive into the functional programming paradigm.
  2. Let's continue with another important paradigm in modern programming: functional programming. Functional programming (FP) is programming based on composing pure functions, while avoiding shared state, mutable data, and side-effects. Functional programs are sequences of transformations of data through functions. In pure functional programming functions and programs don't have state, which means that functions do not hold shared data. They only access their input arguments and return an output. I will give you examples later. Functional programming is declarative programing approach (not imperative), which means that instead of describing an algorithm how to do something step by step, functional developers describe the result by functions and compositions of functions. The program state flows through pure functions, where one function passes its output data as input to other function. I will illustrate how this happens with examples later. What is a "pure function"? It is a function, which returns value only determined by its input, without side effects. Printing something at the console or storing something in a database are examples of side effects. Therefore, using pure functional programming is often impractical. Modern languages use elements of functional-style programming and are not purely functional. Examples of pure functions are: the square root function: "sqrt of x", which takes a number as input and returns another number as output, and the function "sort of list", which takes a list as input and returns a new list as output. Both functions have no side effects: they don't change anything; they don't read or write external data; and they do not use state. They are pure functions. Pure functions are the heart of the functional programming. Using "pure functions" means maintaining "consistent results". If you invoke a pure function many times with the same input data, it will have the same consistent behavior and will return the same result, because it have no state and no interaction with the external data or components. Pure functions produce predictable results and behavior and sometimes their correctness can be mathematically proven. This is the most important principle in functional programming: to build programs by composition of stateless pure functions without side effects.
  3. Purely functional languages (like Haskell) are unpractical for most real-world projects and are rarely used. Modern programming languages are much more practical, so they implement the functional paradigm, along with other paradigms like structured programming and object-oriented programming. In this section I will explain the ideas behind pure functional languages, impure functional languages and multi-paradigm languages.
  4. There are many functional programming languages and languages that incorporate functional paradigms into modern software development. Most modern languages are not functional, but support concepts for functional programming. Purely functional languages are unpractical and rarely used, because it is more complicated to program without maintaining a state. Purely functional developers need to switch their thinking style from the traditional "algorithmic thinking" to "functional thinking". The program in the purely functional languages is a pure function (which calls other pure functions) without side effects. An example of purely functional language is Haskell, which is not widely used in practice, but it has a great value in learning the functional programming paradigms. Impure functional languages are used more often because they allow exceptions from the concept of "pure functions" and simplify the work of developers. These languages emphasize the functional style but sometimes allow side effects. An example of impure functional language is Clojure. It is not very popular in practical software development. Multi-paradigm languages combine the strengths of both the functional and the algorithmic (or imperative) world. Most of today's widely used general-purpose programming languages are multi-paradigm. They combine multiple programing paradigms: functional programming, declarative programming, structured programming, imperative programming, object-oriented programming, component-based programming, event-driven programming, asynchronous programming, and many others. Examples of popular general-purpose multi-paradigm programming languages are: JavaScript, C#, Python, Java, PHP, C++, Go, Swift and TypeScript. All these languages combine multiple concepts and paradigms for structuring the program to simplify the work of developers and improve their efficiency and performance.
  5. Now it's time to show you some code. I will demonstrate the functional style of programming through a sample C# program, written in a classical procedural style and the same code re-written in a functional style.
  6. This is an example, which demonstrates the functional style of programming, compared to the traditional imperative (or structured) programming style. We want to write a C# program to read several numbers, find the biggest of them and print it. To solve this problem in a functional style, we can write the following code: We print the result, which will be calculated by a function, using another function: Console.WriteLine. We read the input text from the console, by invoking a function: Console.ReadLine. Then we split the result from the previous function (the input text) into space-separated elements (which are strings). Then we parse each of these input strings to integer numbers. We use the mapping function in C# (Select). It takes as an input a sequence of strings and maps it through another function (int.Parse). The result from this function is a sequence of integers. Finally, we find the biggest number from the list of numbers, using the Max function. Each subsequent step takes as input the result of the previous step and transforms it into another result. This is the power of functional programming: to use a composition of functions to process certain data and obtain certain results. This functional style of writing expressions to build or compute something is very common in modern programming and can be seen in many programming languages, frameworks and libraries. Now, let's see the equivalent imperative style for the same program, again in C#. This is a piece of code, written in a structured programming style, or a procedural style. It consists of sequence of commands and each command takes its input from a variable, calculates a new result and stores it in a variable. This is the first command: reading the input text. The next command splits the input text into space-separated elements. The next command converts the sequence of input numbers from text to integers. The next command finds the maximal number from the integers. The last command prints the result. The imperative style solution is very clear. It consists of very simple sequence of steps, which execute one after another. It is easy to read and understand. The functional (or declarative) style solution uses nested functions and is more complex to read and understand. Some developers prefer the functional style. Others prefer the procedural style. Developers need to know both styles well and choose the best for their current task.
  7. We can see the code from the previous examples in action. Click the first link. Wait for it to load. This is the functional-style code. Click the [Run] button. Now enter some numbers, for example "20 30 5". The biggest number is 30. Now click the second link. Wait for it to load. This is the procedural-style code. Click the [Run] button. And again enter some numbers: "20 30 5". The biggest number is 30.
  8. In programming, lambda functions are short expressions that transform an input into an output. Lambda functions are typically used as arguments to other functions, and this is supported by most modern programming languages. For example, a sorting function may accept as a parameter a lambda function, which implements the comparison between two objects. For better understanding this concept, let's see some examples of lambda functions in JavaScript, Java, Python and C#.
  9. In programming, lambda functions are expressions, which transform some input to some output. They look like a mathematical formula, or like an anonymous (unnamed) function. Here are some examples. The first example is a lambda function in C#. The next example is a lambda function in JavaScript. The next example is a lambda function in Python. All these examples define the same lambda function in different languages. It takes an input number x and returns twice this number: 2 x. JavaScript, Python, C# and Java support "first-class functions". This concept means that functions can be stored in variables and can be passed as arguments to other functions. These languages support expressions of type "function", which hold programming logic to transform some input into some output. Lambda functions are the simplest way to write an expression of type "function". Once we have a variable, which holds a function, we can invoke it. Let's review some examples. This example in JavaScript shows how to define a variable, which holds a lambda function as its value. We define the variable "twice", which holds the function "multiply by 2". We can invoke the function "twice" with argument 5 and it returns 10. The next example is the same thing in Python. It declares a variable "twice" and stores a lambda function in it, a function which takes as input x and returns two times x. Then we invoke the function "twice" with argument 5 and it returns 10. This is the same code in C#. Again, we define a variable and assign a lambda function to it. Then we invoke the lambda function through the variable. The same code can be written in Java, and it looks very similar. Declaration of variable, holding a lambda function and invocation of the lambda function. First-class functions are an important concept in programming. They allow functions and methods to take other functions as arguments. And this is very powerful tool for many situations. We shall learn more about functional programming and first-class functions in the advanced programming modules and courses at SoftUni.
  10. The following live demonstration illustrates how to use first-class functions in JavaScript. We open the live example at repl.it. Wait for it to load. Then we click [Run]. And we can see the result: the lambda function from the variable "twice" is invoked and its result is printed.
  11. Higher-order functions take other functions as arguments and thus they can implement abstract behavior. For example, an aggregation function may accept as an argument the aggregation operator (which is a lambda function). Let's see how this happens in action.
  12. Higher-order functions take other functions as arguments. This is powerful concept in functional programming. Using functions, which take other functions as argument, we can implement abstract behavior. We can create functions, which calculate something, but what exactly depends on their arguments. Let's take an example: the function "aggregate". It takes 3 parameters: start, end and func. The function aggregates the result from the given function over the numbers from start to end. For example, it can multiply the numbers from 1 to 10. Or it can sum the numbers from 20 to 30. In the body of the "aggregate" function we apply the function "func" many times in a loop, for the numbers from "start" to "end". The start number, end number and the aggregation function come as arguments. This function is a good example, which illustrates the power of "higher-order functions" in functional programming. The function "aggregate" accepts as parameter another function, which combines two values during the aggregation process. This is an example how we can invoke this higher-order function: We sum the numbers from 1 to 10. The start argument is 1. The end argument is 10. The aggregation function is "a + b". It sums its arguments. This is an example how we can invoke the same higher-order function to multiply the numbers from 1 to 10. It is very similar to the previous example. The last example demonstrates how we can append together the numbers from 1 to 10, using the same higher-order function: The aggregation function here is different: it appends its arguments as text.
  13. This live demonstration illustrates how to use higher-order functions in JavaScript. We open the live example at repl.it. And we wait for it to load. It needs some time. This is the same example from the previous slide. The function "aggregate" and 3 different scenarios of aggregation. We run it and we see the result. And it is as expected.
  14. Did you like this lesson? Do you want more? Join the learners' community at softuni.org. Subscribe to my YouTube channel to get more free video tutorials on coding, dev concepts and software development. Access more free dev lessons and learning resources for developers. Get free help from mentors and meet other learners. And it's all free!
  15. Join now! SOFTUNI.ORG