Functional programming scala_mod

468 views

Published on

Functional Programming using Scala. A beginner guide for scala enthusiast which covers Higher order functions, Pattern Matching and need for functional Paradigm

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
468
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
8
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Functional programming scala_mod

  1. 1. Functional Programming K Kishore
  2. 2. Functional Paradigm > Immutable vs Mutable > Functions and Higher Order Functions > Pattern Matching > In This Talk
  3. 3. 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
  4. 4. 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 ?)
  5. 5. 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
  6. 6. 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
  7. 7. Scala (Scalable Language)
  8. 8. Prof. Martin Odersky
  9. 9. 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
  10. 10. val list = List(1,2,3) (or) (1 :: 2 :: 3 :: Nil) > Immutable List 1 2 3 Nil
  11. 11. 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
  12. 12. 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
  13. 13. 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
  14. 14. 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
  15. 15. def add(x: Int, y: Int): Int = x+y > Functions Name Parameters Return Type Body
  16. 16. 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
  17. 17. 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
  18. 18. 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
  19. 19. 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
  20. 20. Problem : To Test Yes/No > def test(number: Int): String = number match { case 0 => "No" case 1 => "Yes" case _ => "Error" } Pattern Matching
  21. 21. 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
  22. 22. 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
  23. 23. 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
  24. 24. Define head, tail, init and last methods for list > Problems 1 2 3 4 5 12 21 56 45 head last tail init
  25. 25. twitter.github.com/scala_school > twitter.github.io/effectivescala/ > Coursera Scala course (50K students last year) > Scala made easy > Programming Scala > How to Learn
  26. 26. 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

×