Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

# Saving this for later?

### Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Standard text messaging rates apply

# Functional programming with_scala

1,224
views

Published on

Functional Programming with Scala

Functional Programming with Scala

Published in: Technology

7 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
1,224
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
37
0
Likes
7
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. -Functionalλ Programming with Scala ©2013 Raymond Tay
• 2. About m(e) I write code. I write books too !
• 3. https://github.com/raygit/Introduction_to_Scala
• 4. The Whats and Whos
• 5. Is Scala a fad? This is when i first heard of Scala
• 6. Mutability val x = 3 var y = 3
• 7. Mutability?scala> val x = mutable.HashMap[String,String]() x: scala.collection.mutable.HashMap[String,String] = Map() scala> x += ("a" "a")→ res0: x.type = Map(a -> a) ------ scala> val y = immutable.HashMap[String,String]() y: scala.collection.immutable.HashMap[String,String] = Map() scala> y += ("a" "a")→ <console>:9: error: reassignment to val y += ("a" "a")→
• 8. Free of side effects • Code reuse • Make better building blocks • Easier to reason about, optimize and test
• 9. Functions are First-classdef multiplyFour : Int Int = (4 * )⇒ def addTwo: Int Int = (2 + )⇒ def º[A,B,C](f:A B, g : B C ) = f andThen g // Parametric-⇒ ⇒ polymorphism def f = º(multiplyFour , addTwo) // We’ll make it look more ‘natural’ in the section: Typeclasses f(4) res6: Int = 18 (addTwo compose multiplyFour)(4) res4: Int = 18
• 10. Closure val x = 3 // what if its `var x = 3`? def = (y: Int) x + yλ ⇒ Be careful what you `close` over i.e. context- sensitive val xval x λλ33 var xvar x λλ33 77
• 11. Lambdas def g( : Int Int) =λ ⇒ λ g((x:Int) x * 2)⇒ OK g( (x:Int) (y:Int) x + y )⇒ ⇒ FAIL g( ((x: Int) (y: Int) x + y)(4) )⇒ ⇒ OK
• 12. Matching // simulate a binary tree sealed trait Tree case class Branch(ele: Int, left:Tree: right:Tree) extends Tree case object Leaf extends Tree // inOrder aka Depth-First Traversal def inOrder(t:Tree) : List[Int] = t match { case Branch(ele, l, r) inOrder(l):::List(ele):::inOrder(r)⇒ case Leaf Nil⇒ }
• 13. Recursion def string2spaces(ss: List[Char]) = ss match { case Nil Nil⇒ case h :: tail ‘ ‘ :: string2spaces(tail)⇒ } import scala.annotation.tailrec @tailrec def string2spaces(ss: List[Char], acc: List[Char]): List[Char] = ss match { case Nil acc⇒ case h :: tail string2spaces(tail,‘ ‘ +: acc)⇒ }
• 14. Lazy vs Eager Eval def IamEager[A](value:A) def IamLazy[A](value: ⇒ A)
• 15. TypeclassesWhat I really want to write is (addTwo ∘ multiplyFour)(4) and not (addTwo compose multiplyFour)(4) typeclasses - create higher kinded types! e.g. List[Int Int]⇒
• 16. Typeclasses in Scala trait Fn extends (Int Int) {⇒ def apply(x: Int) : Int def º(f: Fn) = f andThen this } def addTwo = new Fn { def apply(x: Int) = 2 + x } def multiplyFour = new Fn { def apply(x: Int) = 4 * x } multiplyFour º addTwo res0: Int => Int = <function1> (addTwo º multiplyFour)(4) res1: Int = 18
• 17. Typeclasses in Scala sealed trait MList[+A] case object Nil extends MList[Nothing] case class ::[+A](head:A, tail: MList[A]) extends MList[A] object MList { def apply[A](xs:A*) : MList[A] = if (xs.isEmpty) Nil else ::(xs.head, apply(xs.tail: _*)) }
• 18. Typeclasses in Scala object Main extends App { val x = MList(1,2,3,4,5) match { case ::(x, ::(2, ::(4, _))) => x case Nil => 42 case ::(x, ::(y, ::(3, ::(4, _)))) => x + y case ::(h, t) => h case _ => 101 } println(s"value of \${x}") }
• 19. Adhoc Polymorphism scala> (1,2,3) map { 1 + _ } <console>:8: error: value map is not a member of (Int, Int, Int) (1,2,3) map { 1 + _ } scala> implicit def giveMeMap[A](t : Tuple3[A,A,A]) = new Tuple3[A,A,A](t._1, t._2, t._3) { def map[B](f: A => B) = new Tuple3(f(_1), f(_2), f(_3)) } scala> (1,2,3) map { 1 + _ }res1: (Int, Int, Int) = (2,3,4)