KEMBAR78
Functional programming scala_mod | PDF
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

Functional programming scala_mod