SlideShare a Scribd company logo
Functional	Programming		
		
		
		
	K	Kishore
Functional	Paradigm	>
Immutable	vs	Mutable	>
Functions	and	Higher	Order	Functions	>
Pattern	Matching	>
In	This	Talk
What	is	Functional	Programming	?	>
Functional	Programming	language	is	one	which	does	not	have	mutable	
variables,	assignments,	control	structures.	It	enables	the	construction	of	elegant	
programs	that	focus	on	functions.	(FP	has	been	around	for	50	years).	
	
Languages	:	
Lisp,	XQuery,	FP,	Haskell,	Scala,	Clojure,	etc	
Functional	Programming	is	becoming	increasingly	popular	because
it	offers	an	attractive	method	for	exploiting	parallelism	for	multicore
and	cloud	computing.
	
Functional	Paradigm
Need	a	catalyzer,	something	that	sparks	initial	adoption	until	the
other	advantages	become	clear	to	everyone.	
>
Multicore	(=	parallel	programming)	-
Cloud	computing	(=	distributed	programming)	-
Functional	Paradigm	(	Why	?)
Non‑determinism	=	Parallel	processing	+	Mutable	state
	
>
Mutable	State	>
var	age	=	20		-
age	=	age+1	-
To	get	deterministic	processing,	avoid	the	mutable	state	!	
Avoiding	mutable	state	means	programming	functionally.	
>
The	root	of	the	Problem
But	what	about	Objects?	>
So,	should	we	forget	OO	and	all	program	in	functional		programming	
languages?		
-
We	need	to	combine	OO	and	FP			-
New	Object	>
Previously:
	“Objects	are	characterized	by	state,	identity,	and	behavior.”	(Booch)	
-
Now:
Eliminate	or	reduce	mutable	state.
	
-
Objects
Scala	(Scalable	Language)
Prof.		Martin		Odersky
What	is	Immutable	?	>
Examples:	-
val	age	=	18	//	In	Scala	-
final	int	age	=	18	//	Java	-
const	int	age		=	18	//	C	-
What	is	Mutable	>
Examples:	-
var	age	=	20	//In	Scala	-
age	=	18	//Java,	C,	C++	-
Immutable	and	Mutable
val	list	=	List(1,2,3)	(or)	(1	::	2	::	3	::	Nil)	>
Immutable	List	
1	
	2	
3	
Nil
To	Double	the	values	in	the	list	.	i.e.	for	a	list	(1,	2,	3,	4,	5)	the	result	should	be	(2,	4,	
6,	8,	10).	
>
	
Let's	do	in	Java	-
int[	]	numbers	=	new	int[	]	{1,2,3,4,5};		
List<Integer>	doubled	=	new	ArrayList<>();
								for(int	elem	:		numbers)	{
												doubled.add(elem	*	2);
								}
								System.out.println(doubled);		
Immutable	and	Mutable	
Primitive	
Obsession
To	Double	the	values	in	the	list	.	i.e.	for	a	list	(1,2,	3,	4,	5)	the	
result	should	be	(2,	4,	6,	8,	10).	
>
‑	Let's	do	in	Scala		
val	numbers	=	List(1,	2,	3,	4,	5,	6)	
val	result	=	numbers.map(elem	=>	elem	*2)	
val	result	=	numbers.map((elem:	Int)	=>	elem	*2)	
val	result	=	numbers.map(	_	*2)
	
Immutable	and	Mutable
Another	Solution	:	>
val	numbers	=	List(1,2,3,4,5,6)
for(elem									numbers){
	println(elem	*	2)
}	
-
Problem	:	>
If	we	list	all	the	natural	numbers	below	10	that	are	multiples	of	3	or	5,	
we	get	3,	5,	6	and	9.	The	sum	of	these	multiples	is	23.
Find	the	sum	of	all	the	multiples	of	3	or	5	below	1000.	
-
(	1	until	1000).filter(elem	=>	elem	%	3	==0	||	elem	%	5	==0	).sum	-
Immutable	and	Mutable
Functions	are	first	class	Citizens.	A	function	Consists	of	4	
things.	
>
Name	-
Parameters	-
Body	-
Return	Type	-
	
Example:		
		
def	add(x:	Int,	y:	Int):	Int	=	x+y	
	
Functions	
	Function	Keyword	
		
	Name	
		
	Parameters	
	
	Body	
	
	Return	Type
def	add(x:	Int,	y:	Int):	Int	=	x+y	>
Functions	
	
	
	
Name	
Parameters	
Return	
Type	
	Body
A	Function	Without	a	Name	>
Example	:	-
val	sum	=	(x:	Int,		y:	Int)=>x+y	-
println(sum(5,	1))	-
	
	Write	a	Square	function	in	Scala.	i.e.	For	Input	4	the	result	is	16	(4*4)	
Solutions	:	
‑		def	square(x:	Int)	=	x*x	
‑		def	squares(x:	Int):	Int	=	x*x	
‑		val	result	=	(x:	Int)=>	x*x	
	
Anonymous	Function
Funtions	that	takes	other	functions	has	parameters	or	returning	a	function	
is	called	Higher	Order	Function.	
>
Example	:	>
					val	priceList	=	List(5,	10,	15,	17,	20,	25,	28,	30,		35,	40)		
						Goal	:	To	sum	all	the	prices	
								def	sumPrices(priceList:	List[Int]):	Int	=	{
						var	sum	=	0
													for(price									priceList)	{		sum	+=price	}			
												sum	
}	
Higher	Order	Functions
Goal	:	To	sum	all	the	prices	Under	25	>
def	sumPricesUnder25(prices:	List[Int]):	Int	=	{
	var	sum	=	0
								for(price									prices)	{
		if(price<25)	sum+=price
	}	
	sum
}		
Goal	:	To	sum	all	the	prices	Over	25,	Under	30,	etc	>
	How	will	You	Solve	this	Problem	?	
	Key	:	We	need	generic	function	i.e.	Single	function	to	compute	sum	of	all	
prices	Over	25,	Under	30,	etc	
Higher	Order	Functions
Solution	:	>
def	sumPrices(prices:	List[Int],	cond:	Int	=>	Boolean):	Int	=	{
		var	sum	=	0
				for(price									prices)	{
						if(cond(price))	sum+=price
				}
		sum
}				
Alterantive	Solution:	>
def	sumPrices(prices:	List[Int],	cond:	Int	=>	Boolean):	Int	=	
prices.filter(cond).sum
	
Higher	Order	Functions
Problem	:	To	Test	Yes/No	>
def	test(number:	Int):	String	=	number	match	{	
case	0	=>	"No"		
case	1	=>	"Yes"		
case	_	=>	"Error"	
}	
Pattern	Matching
To	sum	all	the	values	in	the	list.	i.e	List(1,	2,	3,	4,	5)	=>	15	>
def	sum(list:	List[Int]):	Int	=	list	match	{	
case	Nil	=>	0	
case	x	::	xs	=>	x	+	sum(xs)	
}	
	
	
	
	
Pattern	Matching	
	1 	2 	3 	4 	5 	Nil
x 	xs
To	Print	Hello	World	N	times	>
	
def	ntimes(n:	Int)	=	for(i						n)	println("Hello	World")		
	
Sum	of	Odd	elements	in	a	List	>
	
def	oddSum(list:	List[Int]):	Int	=	list.filter(elem	=>	elem%2!=0).sum	
Problems
Find	length	of	the	list	without	using	length	function	>
	
def	length(list:	List[Int]):	Int	=	list.map(elem	=>	1).sum	
	
def	length(list:	List[Int]):	Int	=	list	match	{	
case	Nil	=>	0	
case	x	::	xs	=>	1	+	length(xs)	
}	
Problems
Define	head,	tail,	init	and	last		methods	for	list		>
	
	
Problems	
1	 	2	 3	 4	 5	 	12
	
	21
	
56
	
45
	
head	 last	
tail	
	init
twitter.github.com/scala_school		>
twitter.github.io/effectivescala/	>
Coursera	Scala	course	(50K	students	last	year)	>
Scala	made	easy	>
Programming	Scala	>
How	to	Learn
https://www.coursera.org/	>
http://www.udacity.com/	>
https://www.edx.org/	>
http://www.learnstreet.com/	>
http://teamtreehouse.com	>
http://nptel.iitm.ac.in/	>
Resources
Functional programming scala_mod

More Related Content

Viewers also liked (12)

PDF
The neverendingstory adtech_final
Maura Tuohy
 
PPTX
Slideshare 5 marketing trends not to ignore.young bloods
Maura Tuohy
 
PDF
Freedom rf 2007_english
LUISA BLANES
 
PPTX
Comparative adjectives
LUISA BLANES
 
PDF
The romans
LUISA BLANES
 
PDF
0113 cm crochet_bags_relaunch_01
LUISA BLANES
 
PDF
Ivo Devijver | Brandveiligheid brussel
Ivo Devijver
 
PDF
11 baby crochet cocoon patterns
LUISA BLANES
 
PDF
8 adorable crochet amigurumi patterns
LUISA BLANES
 
PDF
Nuevo testamento en quechua
LUISA BLANES
 
PDF
A.communicative.grammar.of.english
LUISA BLANES
 
PDF
Trash to treasure 28 recycled crafts
LUISA BLANES
 
The neverendingstory adtech_final
Maura Tuohy
 
Slideshare 5 marketing trends not to ignore.young bloods
Maura Tuohy
 
Freedom rf 2007_english
LUISA BLANES
 
Comparative adjectives
LUISA BLANES
 
The romans
LUISA BLANES
 
0113 cm crochet_bags_relaunch_01
LUISA BLANES
 
Ivo Devijver | Brandveiligheid brussel
Ivo Devijver
 
11 baby crochet cocoon patterns
LUISA BLANES
 
8 adorable crochet amigurumi patterns
LUISA BLANES
 
Nuevo testamento en quechua
LUISA BLANES
 
A.communicative.grammar.of.english
LUISA BLANES
 
Trash to treasure 28 recycled crafts
LUISA BLANES
 

Similar to Functional programming scala_mod (20)

PPTX
Introduction to Functional programming
Ny Fanilo Andrianjafy, B.Eng.
 
PPTX
When life gives you functions make functional programs!
Aaron Levin
 
PDF
Functional programming & immutable data.
Luiz Henrique Estacio
 
PDF
Functional programming 101
Marcle Rodrigues
 
PDF
Introduction to functional programming
Konrad Szydlo
 
PDF
Intro to functional programming - Confoo
felixtrepanier
 
PDF
Functional Programming and Java8
Ender Aydin Orak
 
PPTX
Functional programming f#
DouYutao
 
PPTX
History fp
VeerapatBoonvanich1
 
PPTX
From Imperative to Functional Programming (for Absolute Beginners)
Alex Bunardzic
 
PDF
Teach Yourself some Functional Programming with Scala
Damian Jureczko
 
PPTX
Functional Programming Introduction
Roberto Lopez
 
PDF
Introduction to functional programming (In Arabic)
Omar Abdelhafith
 
PPTX
Why functional programming in C# & F#
Riccardo Terrell
 
PPTX
Break Free with Managed Functional Programming: An Introduction to F#
Dave Fancher
 
PPTX
Break Free with Managed Functional Programming: An Introduction to F#
IndyMobileNetDev
 
PDF
Forget what you think you know: Redefining functional programming for Scala
Kelley Robinson
 
PPTX
F# Ignite - DNAD2010
Rodrigo Vidal
 
PPTX
Functional programming
PiumiPerera7
 
PPTX
Introduction to functional programming with JavaScript
Farzaneh Orak
 
Introduction to Functional programming
Ny Fanilo Andrianjafy, B.Eng.
 
When life gives you functions make functional programs!
Aaron Levin
 
Functional programming & immutable data.
Luiz Henrique Estacio
 
Functional programming 101
Marcle Rodrigues
 
Introduction to functional programming
Konrad Szydlo
 
Intro to functional programming - Confoo
felixtrepanier
 
Functional Programming and Java8
Ender Aydin Orak
 
Functional programming f#
DouYutao
 
From Imperative to Functional Programming (for Absolute Beginners)
Alex Bunardzic
 
Teach Yourself some Functional Programming with Scala
Damian Jureczko
 
Functional Programming Introduction
Roberto Lopez
 
Introduction to functional programming (In Arabic)
Omar Abdelhafith
 
Why functional programming in C# & F#
Riccardo Terrell
 
Break Free with Managed Functional Programming: An Introduction to F#
Dave Fancher
 
Break Free with Managed Functional Programming: An Introduction to F#
IndyMobileNetDev
 
Forget what you think you know: Redefining functional programming for Scala
Kelley Robinson
 
F# Ignite - DNAD2010
Rodrigo Vidal
 
Functional programming
PiumiPerera7
 
Introduction to functional programming with JavaScript
Farzaneh Orak
 
Ad

Recently uploaded (20)

PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Ad

Functional programming scala_mod