Functional programming scala_mod
-
Upload
kishore-nair -
Category
Technology
-
view
201 -
download
0
description
Transcript of Functional programming scala_mod
![Page 1: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/1.jpg)
FunctionalProgramming
KKishore
![Page 2: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/2.jpg)
FunctionalParadigm>
ImmutablevsMutable>
FunctionsandHigherOrderFunctions>
PatternMatching>
InThisTalk
![Page 3: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/3.jpg)
WhatisFunctionalProgramming?>FunctionalProgramminglanguageisonewhichdoesnothavemutablevariables,assignments,controlstructures.Itenablestheconstructionofelegantprogramsthatfocusonfunctions.(FPhasbeenaroundfor50years).Languages:
Lisp,XQuery,FP,Haskell,Scala,Clojure,etcFunctionalProgrammingisbecomingincreasinglypopularbecauseitoffersanattractivemethodforexploitingparallelismformulticoreandcloudcomputing.
FunctionalParadigm
![Page 4: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/4.jpg)
Needacatalyzer,somethingthatsparksinitialadoptionuntiltheotheradvantagesbecomecleartoeveryone.
>
Multicore(=parallelprogramming)-Cloudcomputing(=distributedprogramming)-
FunctionalParadigm(Why?)
![Page 5: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/5.jpg)
Non‑‑determinism=Parallelprocessing+Mutablestate
>
MutableState>varage=20-age=age+1-
Togetdeterministicprocessing,avoidthemutablestate!Avoidingmutablestatemeansprogrammingfunctionally.
>
TherootoftheProblem
![Page 6: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/6.jpg)
ButwhataboutObjects?>So,shouldweforgetOOandallprograminfunctionalprogramminglanguages?
-
WeneedtocombineOOandFP-
NewObject>Previously:“Objectsarecharacterizedbystate,identity,andbehavior.”(Booch)
-
Now:Eliminateorreducemutablestate.
-
Objects
![Page 7: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/7.jpg)
Scala(ScalableLanguage)
![Page 8: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/8.jpg)
Prof.MartinOdersky
![Page 9: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/9.jpg)
WhatisImmutable?>Examples:-
valage=18//InScala-finalintage=18//Java-constintage=18//C-
WhatisMutable>Examples:-
varage=20//InScala-age=18//Java,C,C++-
ImmutableandMutable
![Page 10: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/10.jpg)
vallist=List(1,2,3)(or)(1::2::3::Nil)>
ImmutableList
1
2
3
Nil
![Page 11: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/11.jpg)
ToDoublethevaluesinthelist.i.e.foralist(1,2,3,4,5)theresultshouldbe(2,4,6,8,10).
>
Let'sdoinJava-
int[]numbers=newint[]{1,2,3,4,5};List<Integer>doubled=newArrayList<>();for(intelem:numbers){doubled.add(elem*2);}System.out.println(doubled);
ImmutableandMutable
PrimitiveObsession
![Page 12: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/12.jpg)
ToDoublethevaluesinthelist.i.e.foralist(1,2,3,4,5)theresultshouldbe(2,4,6,8,10).
>
‑‑ Let'sdoinScalavalnumbers=List(1,2,3,4,5,6)valresult=numbers.map(elem=>elem*2)valresult=numbers.map((elem:Int)=>elem*2)valresult=numbers.map(_*2)
ImmutableandMutable
![Page 13: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/13.jpg)
AnotherSolution:>valnumbers=List(1,2,3,4,5,6)for(elemnumbers){println(elem*2)}
-
Problem:>Ifwelistallthenaturalnumbersbelow10thataremultiplesof3or5,weget3,5,6and9.Thesumofthesemultiplesis23.
Findthesumofallthemultiplesof3or5below1000.
-
(1until1000).filter(elem=>elem%3==0||elem%5==0).sum-
ImmutableandMutable
![Page 14: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/14.jpg)
FunctionsarefirstclassCitizens.AfunctionConsistsof4things.
>
Name-Parameters-Body-ReturnType-
Example:defadd(x:Int,y:Int):Int=x+y
Functions
FunctionKeywordNameParametersBodyReturnType
![Page 15: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/15.jpg)
defadd(x:Int,y:Int):Int=x+y>
Functions
Name
Parameters
ReturnType
Body
![Page 16: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/16.jpg)
AFunctionWithoutaName>Example:-valsum=(x:Int,y:Int)=>x+y-println(sum(5,1))-
WriteaSquarefunctioninScala.i.e.ForInput4theresultis16(4*4)Solutions:
‑‑ defsquare(x:Int)=x*x‑‑ defsquares(x:Int):Int=x*x‑‑ valresult=(x:Int)=>x*x
AnonymousFunction
![Page 17: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/17.jpg)
FuntionsthattakesotherfunctionshasparametersorreturningafunctioniscalledHigherOrderFunction.
>
Example:>
valpriceList=List(5,10,15,17,20,25,28,30,35,40)
Goal:TosumallthepricesdefsumPrices(priceList:List[Int]):Int={varsum=0for(pricepriceList){sum+=price}sum}
HigherOrderFunctions
![Page 18: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/18.jpg)
Goal:TosumallthepricesUnder25>defsumPricesUnder25(prices:List[Int]):Int={varsum=0for(priceprices){if(price<25)sum+=price}sum}
Goal:TosumallthepricesOver25,Under30,etc>
HowwillYouSolvethisProblem?
Key:Weneedgenericfunctioni.e.SinglefunctiontocomputesumofallpricesOver25,Under30,etc
HigherOrderFunctions
![Page 19: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/19.jpg)
Solution:>defsumPrices(prices:List[Int],cond:Int=>Boolean):Int={varsum=0for(priceprices){if(cond(price))sum+=price}sum}
AlterantiveSolution:>defsumPrices(prices:List[Int],cond:Int=>Boolean):Int=prices.filter(cond).sum
HigherOrderFunctions
![Page 20: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/20.jpg)
Problem:ToTestYes/No> deftest(number:Int):String=numbermatch{case0=>"No"case1=>"Yes"case_=>"Error"
}
PatternMatching
![Page 21: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/21.jpg)
Tosumallthevaluesinthelist.i.eList(1,2,3,4,5)=>15> defsum(list:List[Int]):Int=listmatch{
caseNil=>0casex::xs=>x+sum(xs)
}
PatternMatching
1 2 3 4 5 Nil
x xs
![Page 22: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/22.jpg)
ToPrintHelloWorldNtimes>
defntimes(n:Int)=for(in)println("HelloWorld")
SumofOddelementsinaList>
defoddSum(list:List[Int]):Int=list.filter(elem=>elem%2!=0).sum
Problems
![Page 23: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/23.jpg)
Findlengthofthelistwithoutusinglengthfunction>deflength(list:List[Int]):Int=list.map(elem=>1).sumdeflength(list:List[Int]):Int=listmatch{
caseNil=>0casex::xs=>1+length(xs)
}
Problems
![Page 24: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/24.jpg)
Definehead,tail,initandlastmethodsforlist>
Problems
1 2 3 4 5 12
21
56
45
head last
tail
init
![Page 25: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/25.jpg)
twitter.github.com/scala_school>
twitter.github.io/effectivescala/>
CourseraScalacourse(50Kstudentslastyear)>
Scalamadeeasy>
ProgrammingScala>
HowtoLearn
![Page 26: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/26.jpg)
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
![Page 27: Functional programming scala_mod](https://reader034.fdocuments.us/reader034/viewer/2022052301/554ebe36b4c9053c4b8b47f2/html5/thumbnails/27.jpg)