Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
object IntroductionToFunctionalProgrammingWithScala {
!

val myName = "Daniel Cukier"
val twitter = "@danicuki"
val email ...
object IntroductionToFunctionalProgrammingWithScala {
!

val myName = "Daniel Cukier"
val twitter = "@danicuki"
val email ...
qsort:

@ Takes three parameters:
@
a:
Pointer to base of array a to be sorted (arrives in r0)
@
left: First of the range ...
qsort:

@ Takes three parameters:
@
a:
Pointer to base of array a to be sorted (arrives in r0)
@
left: First of the range ...
qsort:

@ Takes three parameters:
@
a:
Pointer to base of array a to be sorted (arrives in r0)
@
left: First of the range ...
void swap(int* a, int* b) {
int tmp;
tmp = *a;
* a = *b;
* b = tmp;
}
!

int partition(int vec[], int left, int right) {
i...
def sort(list: List[Int]): List[Int] = {
list match {
case Nil => list
case _ => sort(list.tail.filter(_ <= list.head)) ::...
sort :: (Ord a)
=> [a] -> [a]
sort []
= []
sort (pivot:rest) = (sort [y | y <- rest, y < pivot])
++ [pivot] ++
(sort [y | ...
def sort(list: List[Int]): List[Int] = {
list match {
case Nil => list
case _ => sort(list.tail.filter(_ <= list.head)) ::...
qsort:

asm

@ Takes three parameters:
@
a:
Pointer to base of array a to be sorted (arrives in r0)
@
left: First of the r...
Functional
Programming
object Benefits {
!
!
!

def FunctionalProgrammingBenefits =
Seq(LessCode,

Immutability,
ConcurrencyAndDistribution,
Patt...
object Lesscode {
!

def fact1(n: Int) = {
var i = n
var result = 1
while (i > 1) {
result = result * i
i -= 1
}
result
}
...
object ConcurrencyAndDistribution {
!

def factorial(n: Int) = Range(1, n+1).foldLeft(1D)(_ * _)
!

def main(args: Array[S...
object PatternMatching {
!

def extract = {
val tup = ("hello world", 42)
tup match {
case (s, i) =>
println("the string w...
object PatternMatching {
!

def valueAssignment {

!
!

!

val tup = (19, 73)
val (a, b) = tup
for ((a, b) <- tup) yield a...
object PatternMatching {
!
!

def checkCases {
val tup = (19, 73)

!
!

!

val result = tup match {
case (a, b) => a + b
c...
object HigherOrderAndFirstClassFunctions {
!

def add(i: Int, j: Int) = i + j



!
!

def addTen = add(_: Int, 10)
//addTe...
object HigherOrderAndFirstClassFunctions {



val root: PartialFunction[Double, Double] = {
case d if (d >= 0) => math.sqr...
Scala
http://www.scala-sbt.org/

$ mkdir hello
$ cd hello
$ echo 'object Hi { def main(args: Array[String]) =
println("Hi!") }' ...
object Benefits {
!

def ScalaBenefits =
Seq(FunctionalProgrammingBenefits,
ScalaPlusJava,
TypeInference,
Traits,
Actors)
...
object ScalaPlusJava {
!
!
!
!
!

case class Person(val name: String, val age: Int)
!

val people = List(Person("Dani", 34...
object TypeInference {
case class Person(val name: String, val age: Int)

!

def underagePeopleNames(persons: List[Person]...
object Traits {
abstract class Animal { def eat(): Unit }

!
!

!

trait HasWings {
def fly() {
goUp(); goDown()
}
}

!
!
...
object Actors {
class HelloActor extends Actor {
def act() {
loop {
receive {
case "hello" => println("hello back at you")...
C
F
C
Programação Funcional
C
G
C
É um negócio legal
G
F
C
Haskel, Erlang ou Scala
Am
Am/G
Não importa a linguagem
F
C
(G7...
object IntroductionToFunctionalProgrammingWithScala {
!

val myName = "Daniel Cukier"
val twitter = "@danicuki"
val email ...
Introduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
Upcoming SlideShare
Loading in …5
×

Introduction to Functional Programming with Scala

1,335 views

Published on

Scala é uma linguagem funcional que roda na JVM. Ela possui todas as vantagens da programação funcional, com a vantagem de ser facilmente integrada com Java, além de possuir uma stack completa de desenvolvimento e deploy. Nessa palestra, daremos os primeiros passos para quem quer trabalhar com Scala. Falaremos de imutabilidade, Traits, funções e expressões lambda entre outros tópicos

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Introduction to Functional Programming with Scala

  1. 1. object IntroductionToFunctionalProgrammingWithScala { ! val myName = "Daniel Cukier" val twitter = "@danicuki" val email = “danicuki@ime.usp.br" ! def main(args: Array[String]) { println(“Hello People!”) } ! }
  2. 2. object IntroductionToFunctionalProgrammingWithScala { ! val myName = "Daniel Cukier" val twitter = "@danicuki" val email = “danicuki@ime.usp.br" ! def main(args: Array[String]) { println(“Hello People!”) } ! }
  3. 3. qsort: @ Takes three parameters: @ a: Pointer to base of array a to be sorted (arrives in r0) @ left: First of the range of indexes to sort (arrives in r1) @ right: One past last of range of indexes to sort (arrives in r2) @ This function destroys: r1, r2, r3, r4, r5, r7 stmfd sp!, {r4, r6, lr} @ Save r4 and r6 for caller mov r6, r2 @ r6 <- right qsort_tailcall_entry: sub r7, r6, r1 @ If right - left <= 1 (already sorted), cmp r7, #1 ldmlefd sp!, {r1, r6, pc} @ Return, moving r4->r1, restoring r6 ldr r7, [r0, r1, asl #2] @ r7 <- a[left], gets pivot element add r2, r1, #1 @ l <- left + 1 mov r4, r6 @ r <- right partition_loop: ldr r3, [r0, r2, asl #2] @ r3 <- a[l] cmp r3, r7 @ If a[l] <= pivot_element, addle r2, r2, #1 @ ... increment l, and ble partition_test @ ... continue to next iteration. sub r4, r4, #1 @ Otherwise, decrement r, ldr r5, [r0, r4, asl #2] @ ... and swap a[l] and a[r]. str r5, [r0, r2, asl #2] str r3, [r0, r4, asl #2] partition_test: cmp r2, r4 @ If l < r, blt partition_loop @ ... continue iterating. partition_finish: sub r2, r2, #1 @ Decrement l ldr r3, [r0, r2, asl #2] @ Swap a[l] and pivot str r3, [r0, r1, asl #2] str r7, [r0, r2, asl #2] bl qsort @ Call self recursively on left part, @ with args a (r0), left (r1), r (r2), @ also preserves r6 and @ moves r4 (l) to 2nd arg register (r1) b qsort_tailcall_entry @ Tail-call self on right part, @ with args a (r0), l (r1), right (r6)
  4. 4. qsort: @ Takes three parameters: @ a: Pointer to base of array a to be sorted (arrives in r0) @ left: First of the range of indexes to sort (arrives in r1) @ right: One past last of range of indexes to sort (arrives in r2) @ This function destroys: r1, r2, r3, r4, r5, r7 stmfd sp!, {r4, r6, lr} @ Save r4 and r6 for caller mov r6, r2 @ r6 <- right qsort_tailcall_entry: sub r7, r6, r1 @ If right - left <= 1 (already sorted), cmp r7, #1 ldmlefd sp!, {r1, r6, pc} @ Return, moving r4->r1, restoring r6 ldr r7, [r0, r1, asl #2] @ r7 <- a[left], gets pivot element add r2, r1, #1 @ l <- left + 1 mov r4, r6 @ r <- right partition_loop: ldr r3, [r0, r2, asl #2] @ r3 <- a[l] cmp r3, r7 @ If a[l] <= pivot_element, addle r2, r2, #1 @ ... increment l, and ble partition_test @ ... continue to next iteration. sub r4, r4, #1 @ Otherwise, decrement r, ldr r5, [r0, r4, asl #2] @ ... and swap a[l] and a[r].
  5. 5. qsort: @ Takes three parameters: @ a: Pointer to base of array a to be sorted (arrives in r0) @ left: First of the range of indexes to sort (arrives in r1) @ right: One past last of range of indexes to sort (arrives in r2) @ This function destroys: r1, r2, r3, r4, r5, r7 stmfd sp!, {r4, r6, lr} @ Save r4 and r6 for caller mov r6, r2 @ r6 <- right qsort_tailcall_entry: sub r7, r6, r1 @ If right - left <= 1 (already sorted), cmp r7, #1 ldmlefd sp!, {r1, r6, pc} @ Return, moving r4->r1, restoring r6 ldr r7, [r0, r1, asl #2] @ r7 <- a[left], gets pivot element add r2, r1, #1 @ l <- left + 1 mov r4, r6 @ r <- right partition_loop: ldr r3, [r0, r2, asl #2] @ r3 <- a[l] cmp r3, r7 @ If a[l] <= pivot_element, addle r2, r2, #1 @ ... increment l, and ble partition_test @ ... continue to next iteration. sub r4, r4, #1 @ Otherwise, decrement r, ldr r5, [r0, r4, asl #2] @ ... and swap a[l] and a[r]. str r5, [r0, r2, asl #2] str r3, [r0, r4, asl #2] partition_test: cmp r2, r4 @ If l < r, blt partition_loop @ ... continue iterating. partition_finish: sub r2, r2, #1 @ Decrement l ldr r3, [r0, r2, asl #2] @ Swap a[l] and pivot str r3, [r0, r1, asl #2] str r7, [r0, r2, asl #2] bl qsort @ Call self recursively on left part, @ with args a (r0), left (r1), r (r2), @ also preserves r6 and @ moves r4 (l) to 2nd arg register (r1) b qsort_tailcall_entry @ Tail-call self on right part, @ with args a (r0), l (r1), right (r6) void swap(int* a, int* b) { int tmp; tmp = *a; * a = *b; * b = tmp; } ! int partition(int vec[], int left, int right) { int i, j; ! ! i = left; for (j = left + 1; j <= right; ++j) { if (vec[j] < vec[left]) { ++i; swap(&vec[i], &vec[j]); } } swap(&vec[left], &vec[i]); return i; } ! void quickSort(int vec[], int left, int right) { int r; ! } if (right > left) { r = partition(vec, left, right); quickSort(vec, left, r - 1); quickSort(vec, r + 1, right); }
  6. 6. void swap(int* a, int* b) { int tmp; tmp = *a; * a = *b; * b = tmp; } ! int partition(int vec[], int left, int right) { int i, j; ! i = left; for (j = left + 1; j <= right; ++j) { if (vec[j] < vec[left]) { ++i; swap(&vec[i], &vec[j]); } } swap(&vec[left], &vec[i]);
  7. 7. def sort(list: List[Int]): List[Int] = { list match { case Nil => list case _ => sort(list.tail.filter(_ <= list.head)) ::: list.head :: sort(list.tail.filter(_ > list.head)) } }
  8. 8. sort :: (Ord a) => [a] -> [a] sort [] = [] sort (pivot:rest) = (sort [y | y <- rest, y < pivot]) ++ [pivot] ++ (sort [y | y <- rest, y >=pivot])
  9. 9. def sort(list: List[Int]): List[Int] = { list match { case Nil => list case _ => sort(list.tail.filter(_ <= list.head)) ::: list.head :: sort(list.tail.filter(_ > list.head)) } } sort :: (Ord a) => [a] -> [a] sort [] = [] sort (pivot:rest) = (sort [y | y <- rest, y < pivot]) ++ [pivot] ++ (sort [y | y <- rest, y >=pivot])
  10. 10. qsort: asm @ Takes three parameters: @ a: Pointer to base of array a to be sorted (arrives in r0) @ left: First of the range of indexes to sort (arrives in r1) @ right: One past last of range of indexes to sort (arrives in r2) @ This function destroys: r1, r2, r3, r4, r5, r7 stmfd sp!, {r4, r6, lr} @ Save r4 and r6 for caller mov r6, r2 @ r6 <- right qsort_tailcall_entry: sub r7, r6, r1 @ If right - left <= 1 (already sorted), cmp r7, #1 ldmlefd sp!, {r1, r6, pc} @ Return, moving r4->r1, restoring r6 ldr r7, [r0, r1, asl #2] @ r7 <- a[left], gets pivot element add r2, r1, #1 @ l <- left + 1 mov r4, r6 @ r <- right partition_loop: ldr r3, [r0, r2, asl #2] @ r3 <- a[l] cmp r3, r7 @ If a[l] <= pivot_element, addle r2, r2, #1 @ ... increment l, and ble partition_test @ ... continue to next iteration. sub r4, r4, #1 @ Otherwise, decrement r, ldr r5, [r0, r4, asl #2] @ ... and swap a[l] and a[r]. str r5, [r0, r2, asl #2] str r3, [r0, r4, asl #2] partition_test: cmp r2, r4 @ If l < r, blt partition_loop @ ... continue iterating. partition_finish: sub r2, r2, #1 @ Decrement l ldr r3, [r0, r2, asl #2] @ Swap a[l] and pivot str r3, [r0, r1, asl #2] str r7, [r0, r2, asl #2] bl qsort @ Call self recursively on left part, @ with args a (r0), left (r1), r (r2), @ also preserves r6 and @ moves r4 (l) to 2nd arg register (r1) b qsort_tailcall_entry @ Tail-call self on right part, @ with args a (r0), l (r1), right (r6) void swap(int* a, int* b) { int tmp; tmp = *a; * a = *b; * b = tmp; } ! int partition(int vec[], int left, int right) { int i, j; ! C ! i = left; for (j = left + 1; j <= right; ++j) { if (vec[j] < vec[left]) { ++i; swap(&vec[i], &vec[j]); } } swap(&vec[left], &vec[i]); return i; } ! void quickSort(int vec[], int left, int right) { int r; ! if (right > left) { r = partition(vec, left, right); quickSort(vec, left, r - 1); quickSort(vec, r + 1, right); } } def sort(list: List[Int]): List[Int] = { list match { case Nil => list case _ => sort(list.tail.filter(_ <= list.head)) ::: list.head :: sort(list.tail.filter(_ > list.head)) } } Scala
  11. 11. Functional Programming
  12. 12. object Benefits { ! ! ! def FunctionalProgrammingBenefits = Seq(LessCode,
 Immutability, ConcurrencyAndDistribution, PatternMatching, HigherOrderFunctions ) ! ! }
  13. 13. object Lesscode { ! def fact1(n: Int) = { var i = n var result = 1 while (i > 1) { result = result * i i -= 1 } result } ! ! ! ! ! ! ! def fact2(n: Int) = (1 to n).foldLeft(1)(_ * _) ! ! ! } ! def fact3(n: Int): Int = if (n == 1) 1 else n * fact3(n - 1)
  14. 14. object ConcurrencyAndDistribution { ! def factorial(n: Int) = Range(1, n+1).foldLeft(1D)(_ * _) ! def main(args: Array[String]) { val x = future { factorial(100) } val y = future { factorial(200) } val z = for (a <- x; b <- y) yield a * b for (c <- z) println("Result: " + c) println("Meanwhile, the main thread goes on!") } ! }
  15. 15. object PatternMatching { ! def extract = { val tup = ("hello world", 42) tup match { case (s, i) => println("the string was " + s) println("the number was " + i) } ! ! ! ! ! ! val p = Person("John Doe", 42) p match { case Person(name, 42) => println(name) } ! ! ! } ! } http://goo.gl/bB1TiM
  16. 16. object PatternMatching { ! def valueAssignment { ! ! ! val tup = (19, 73) val (a, b) = tup for ((a, b) <- tup) yield a + b ! ! ! ! ! } ! }
  17. 17. object PatternMatching { ! ! def checkCases { val tup = (19, 73) ! ! ! val result = tup match { case (a, b) => a + b case (19, a) => a } ! ! ! ! ! ! } } //unreachable code
  18. 18. object HigherOrderAndFirstClassFunctions { ! def add(i: Int, j: Int) = i + j 
 ! ! def addTen = add(_: Int, 10) //addTen(20) = 30 : Int ! ! ! ! def powerTwo(x: Int => Int)(y: Int): Int = x(y) * x(y) def addTenPowerTwo(x: Int): Int = powerTwo(addTen)(x) ! ! ! val myFunc = PartialFunction(sum10power2) // myFunc(20) = addTenPowerTwo(20) = // (20 + 10) * (20 + 10) = // 30 * 30 = 900 : Int //vai dar nó no cérebro ! ! ! }
  19. 19. object HigherOrderAndFirstClassFunctions { 
 val root: PartialFunction[Double, Double] = { case d if (d >= 0) => math.sqrt(d) } ! ! ! ! ! ! ! ! root(3) //Double = 1.7320508075688772 List(0.5, -0.2, 4).collect(root) //List[Double] = List(0.7071067811865476, 2.0) ! ! } //vai dar nó no cérebro
  20. 20. Scala
  21. 21. http://www.scala-sbt.org/ $ mkdir hello $ cd hello $ echo 'object Hi { def main(args: Array[String]) = println("Hi!") }' > hw.scala $ sbt run Hi! ! $ mkdir project $ echo 'addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.4.0")' > project/plugins.sbt $ sbt eclipse [info] Successfully created Eclipse project files for project(s): hello
  22. 22. object Benefits { ! def ScalaBenefits = Seq(FunctionalProgrammingBenefits, ScalaPlusJava, TypeInference, Traits, Actors) }
  23. 23. object ScalaPlusJava { ! ! ! ! ! case class Person(val name: String, val age: Int) ! val people = List(Person("Dani", 34), Person("Matusalém", 912)) val (minors, adults) = people partition (_.age < 150) ! ! ! ! ! ! ! ! } Java List<Person> people = ………; List<Person> minors = new ArrayList<Person>(people.size()); List<Person> adults = new ArrayList<Person>(people.size()); public void populate() { for (Person person : people) { if (person.age() < 150) minors.add(person); else adults.add(person); } }
  24. 24. object TypeInference { case class Person(val name: String, val age: Int) ! def underagePeopleNames(persons: List[Person]) = { for (person <- persons; if person.age < 18) yield person.name } ! ! ! ! ! ! ! ! ! } ! def createRandomPeople() = { val names = List("Alice", "Bob", "Carol", “Dave”) for (name <- names) yield { val age = (Random.nextGaussian() * 8 + 20).toInt new Person(name, age) } } val people = createRandomPeople() //people: List[Person] = List(Alice (16),…)) underagePeopleNames(people) //res1: List[String] = List(Alice, Bob, Frank)
  25. 25. object Traits { abstract class Animal { def eat(): Unit } ! ! ! trait HasWings { def fly() { goUp(); goDown() } } ! ! ! ! ! trait Mammal { def suck() { ... } } ! ! ! ! } ! trait Bird extends Animal with HasWings class Own extends Bird class Bat extends Mammal with HasWings
  26. 26. object Actors { class HelloActor extends Actor { def act() { loop { receive { case "hello" => println("hello back at you") case _ => println("huh?") } } } } 
 ! ! ! ! ! ! ! ! ! ! } ! def main(args: Array[String]) { val helloActor = new Actors.HelloActor helloActor.start helloActor ! "hello" helloActor ! “good bye" }
  27. 27. C F C Programação Funcional C G C É um negócio legal G F C Haskel, Erlang ou Scala Am Am/G Não importa a linguagem F C (G7) Se for funcional C F C Muita recursividade C F C Com imutabilidade C G/B Am Como esse troço é difícil Am/G Am Mas também poderoso Am/G Am E um código limpo F C G7 É bem mais gostoso
 Bb C Sei programar tão bonito C Bb Que os irmãos vão ficar Bb C Com inveja de mim Bb C Programador de função C Bb Casador de padrão C F Eu uso modelo de ator
  28. 28. object IntroductionToFunctionalProgrammingWithScala { ! val myName = "Daniel Cukier" val twitter = "@danicuki" val email = “danicuki@ime.usp.br" ! val references = Seq( “https://github.com/danicuki/IntroductionToScala”, “https://www.slideshare.net/danicuki”) ! println(“Thanks!”) ! ! }

×