Functional Programming in Scala in a Nutshell 1
Why Scala? 2
Java is too Verbose // construct a empty list List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); // introduce a counting index, i for (int i = 0; i<list.size(); i++) { Integer element = list.get(i); System.out.println(element); } 3
4
Scala is Concise (1 :: 2 :: 3 :: Nil).foreach(println) 5
Railway Programming 6
... in Elegant Functional Programming Way val openerO = Some(Opener) val wineO = Some(Wine(vintage = 1997)) val contentsO = for { opener ← openerO wine ← wineO } yield opener.open(wine) // contentsO: Option[Contents] = Some(contentsOfWine) // no null, no NullPointerException 7
... in Elegant Functional Programming Way val openerO = Some(Opener) val wineO = None val contentsO = for { opener ← openerO wine ← wineO } yield opener.open(wine) // contentsO: Option[Contents] = None // no null, no NullPointerException 8
Scala supports OOP, too // OOP polymorphism val newOpener: Opener = new NormalOpener() val oldOpener: Opener = new BrokenOpener() val wine = new Wine() println(newOpener.open(wine)) // contentsOfWine println(oldOpener.open(wine)) // Exception occurs! 9
Scala is Compatible with Java Compatible with Hadoop and Spark → Official language for the bigdata community // joda time based on java import org.joda.time.DateTime, java.util.Date val jodaDt: DateTime = new DateTime(new Date()) val month = month = dt.getMonthOfYear() 10
… and Vice Versa // scala code object Person { val MALE = "m"; } // java code public class App { public static void main(String argv[]) { Person$ person = Person$.MODULE$; System.out.println(person.MALE()); } } 11
What is Functional Programming? 12
It’s NOT map and reduce & It’s NOT lambda functions 13
By Definition, A function is called pure if all its inputs are declared as inputs - none of them are hidden - and likewise all its outputs are declared as outputs. 1 — Kris Jenkins 1 It is not a concrete definition but easy and intuitive. 14
Immutability // value, not variable val a = 1 a = 2 // error: reassignment to val // list val ints1: List[Int] = 1 :: 2 :: Nil val ints2: List[Int] = ints1 :+ 3 println(ints1) // List(1, 2) println(ints2) // List(1, 2, 3) println(ints2 == 1 :: 2 :: 3 :: Nil) // true 15
Immutability def fibonacci(n : Int): Int = { // while loop requires temporary varables var (a, b, i) = (0, 1, 0) while( i < n ) { val c = a + b a = b b = c i = i + 1 } return a } 16
Immutability // recursion doesn't requires temporary varables def fibonacci(n : Int): Int = n match { case 0 | 1 => n case _ => fibonacci(n - 1) + fibonacci(n - 2) } 17
Side Effect, by Definition A function is said to have a side effect if it modifies some state outside its scope or has an observable interaction with its calling functions or the outside world. — Side effect (computer science), Wikipedia 18
Side Effect // this def has a side effect def currentProgram(guide: TVGuide, channel: Int): Program = { val schedule = guide.getSchedule(channel) schedule.programAt(new Date()) } 19
Side Effect // now it has no more side effects // and it is immutable def program(guide: TVGuide, channel: Int, date: Date): Program = { val schedule = guide.getSchedule(channel) schedule.programAt(date) } 20
Let's Start at the Beginning Again A Program is functional iff it has no side effects.2 2 cf. Definition using Referential Transparency 21
Why Functional Programming? 4 ! Type declaration exposes its action 4 " Short code & Fewer bugs 4 # Extremely easy to write unit test 4 $ Easy to maintain 22
When should you use Functional Programming? 4 ! If you want to develop a product that you need to manage in the long run 4 " If you want to write an elegant code 4 # If you want to build up your project quickly 23
When shoud not you use Functional Programming? 4 ! If you want to write a script to use once 4 ⏳ If your app requires nano-fast performance 4 # If you do not know about functional programming, and you need to develop your app in two weeks 4 $ If you want to live by maintaining a program for whole lifetime 24
For Android Developers 25
For Javascript Developers 26
Further Readings 27
Grammars 4 스칼라 학교! 4 스칼라 공식 도큐먼트 입문 4 Effective Scala by Twitter 4 Effective Scala by Heejong Lee 4 Hacker Rank for Tutorials 4 Exercism 28
What is Functional Programming? 4 함수형 프로그래밍이란 무엇인가? 4 어떤 프로그래밍 언어들이 함수형인가? 29
Textbooks & References 4 ! 스칼라로 배우는 함수형 프로그래밍 4 Functional Programming Principles in Scala on Coursera 4 Big Data Analysis with Scala and Spark on Coursera 4 Scala Excercises 30
Popular Scala Libraries 4 ! Scalaz: Scala library for functional programming 4 Cats: Lightweight, modular, and extensible library for functional programming 4 Monix: Asynchronous Programming for Scala 4 Circe: A JSON library for Scala powered by Cats 31
Haskell 4 Eta Programming Language, Haskell on the JVM 4 (가장 쉬운) 하스켈 책 : 느긋하지만 우아하고 세련된 함수형 언어 32
Resources 4 Scastie, an interactive playground for Scala 33

Functional Programming in Scala in a Nutshell

  • 1.
  • 2.
  • 3.
    Java is tooVerbose // construct a empty list List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); // introduce a counting index, i for (int i = 0; i<list.size(); i++) { Integer element = list.get(i); System.out.println(element); } 3
  • 4.
  • 5.
    Scala is Concise (1:: 2 :: 3 :: Nil).foreach(println) 5
  • 6.
  • 7.
    ... in ElegantFunctional Programming Way val openerO = Some(Opener) val wineO = Some(Wine(vintage = 1997)) val contentsO = for { opener ← openerO wine ← wineO } yield opener.open(wine) // contentsO: Option[Contents] = Some(contentsOfWine) // no null, no NullPointerException 7
  • 8.
    ... in ElegantFunctional Programming Way val openerO = Some(Opener) val wineO = None val contentsO = for { opener ← openerO wine ← wineO } yield opener.open(wine) // contentsO: Option[Contents] = None // no null, no NullPointerException 8
  • 9.
    Scala supports OOP,too // OOP polymorphism val newOpener: Opener = new NormalOpener() val oldOpener: Opener = new BrokenOpener() val wine = new Wine() println(newOpener.open(wine)) // contentsOfWine println(oldOpener.open(wine)) // Exception occurs! 9
  • 10.
    Scala is Compatiblewith Java Compatible with Hadoop and Spark → Official language for the bigdata community // joda time based on java import org.joda.time.DateTime, java.util.Date val jodaDt: DateTime = new DateTime(new Date()) val month = month = dt.getMonthOfYear() 10
  • 11.
    … and ViceVersa // scala code object Person { val MALE = "m"; } // java code public class App { public static void main(String argv[]) { Person$ person = Person$.MODULE$; System.out.println(person.MALE()); } } 11
  • 12.
  • 13.
    It’s NOT mapand reduce & It’s NOT lambda functions 13
  • 14.
    By Definition, A functionis called pure if all its inputs are declared as inputs - none of them are hidden - and likewise all its outputs are declared as outputs. 1 — Kris Jenkins 1 It is not a concrete definition but easy and intuitive. 14
  • 15.
    Immutability // value, notvariable val a = 1 a = 2 // error: reassignment to val // list val ints1: List[Int] = 1 :: 2 :: Nil val ints2: List[Int] = ints1 :+ 3 println(ints1) // List(1, 2) println(ints2) // List(1, 2, 3) println(ints2 == 1 :: 2 :: 3 :: Nil) // true 15
  • 16.
    Immutability def fibonacci(n :Int): Int = { // while loop requires temporary varables var (a, b, i) = (0, 1, 0) while( i < n ) { val c = a + b a = b b = c i = i + 1 } return a } 16
  • 17.
    Immutability // recursion doesn'trequires temporary varables def fibonacci(n : Int): Int = n match { case 0 | 1 => n case _ => fibonacci(n - 1) + fibonacci(n - 2) } 17
  • 18.
    Side Effect, byDefinition A function is said to have a side effect if it modifies some state outside its scope or has an observable interaction with its calling functions or the outside world. — Side effect (computer science), Wikipedia 18
  • 19.
    Side Effect // thisdef has a side effect def currentProgram(guide: TVGuide, channel: Int): Program = { val schedule = guide.getSchedule(channel) schedule.programAt(new Date()) } 19
  • 20.
    Side Effect // nowit has no more side effects // and it is immutable def program(guide: TVGuide, channel: Int, date: Date): Program = { val schedule = guide.getSchedule(channel) schedule.programAt(date) } 20
  • 21.
    Let's Start atthe Beginning Again A Program is functional iff it has no side effects.2 2 cf. Definition using Referential Transparency 21
  • 22.
    Why Functional Programming? 4 ! Typedeclaration exposes its action 4 " Short code & Fewer bugs 4 # Extremely easy to write unit test 4 $ Easy to maintain 22
  • 23.
    When should youuse Functional Programming? 4 ! If you want to develop a product that you need to manage in the long run 4 " If you want to write an elegant code 4 # If you want to build up your project quickly 23
  • 24.
    When shoud notyou use Functional Programming? 4 ! If you want to write a script to use once 4 ⏳ If your app requires nano-fast performance 4 # If you do not know about functional programming, and you need to develop your app in two weeks 4 $ If you want to live by maintaining a program for whole lifetime 24
  • 25.
  • 26.
  • 27.
  • 28.
    Grammars 4 스칼라 학교! 4스칼라 공식 도큐먼트 입문 4 Effective Scala by Twitter 4 Effective Scala by Heejong Lee 4 Hacker Rank for Tutorials 4 Exercism 28
  • 29.
    What is FunctionalProgramming? 4 함수형 프로그래밍이란 무엇인가? 4 어떤 프로그래밍 언어들이 함수형인가? 29
  • 30.
    Textbooks & References 4 ! 스칼라로배우는 함수형 프로그래밍 4 Functional Programming Principles in Scala on Coursera 4 Big Data Analysis with Scala and Spark on Coursera 4 Scala Excercises 30
  • 31.
    Popular Scala Libraries 4 ! Scalaz:Scala library for functional programming 4 Cats: Lightweight, modular, and extensible library for functional programming 4 Monix: Asynchronous Programming for Scala 4 Circe: A JSON library for Scala powered by Cats 31
  • 32.
    Haskell 4 Eta ProgrammingLanguage, Haskell on the JVM 4 (가장 쉬운) 하스켈 책 : 느긋하지만 우아하고 세련된 함수형 언어 32
  • 33.
    Resources 4 Scastie, aninteractive playground for Scala 33