Slide 1

Slide 1 text

Few things on Scala ͍ΒͪͷͨΊͷ4DBMBೖ໳ Image: typesafe.com Sorami Hisamoto 09 July 2013 @ PG

Slide 2

Slide 2 text

For me to digest, and to introduce some of the contents from l'VODUJPOBM1SPHSBNNJOH1SJODJQMFTJO4DBMBz a marvelous course by Martin Odersky (plus alpha).

Slide 3

Slide 3 text

For me to digest, and to introduce some of the contents from l'VODUJPOBM1SPHSBNNJOH1SJODJQMFTJO4DBMBz a marvelous course by Martin Odersky (plus alpha). https://www.coursera.org/course/progfun

Slide 4

Slide 4 text

For me to digest, and to introduce some of the contents from l'VODUJPOBM1SPHSBNNJOH1SJODJQMFTJO4DBMBz a marvelous course by Martin Odersky (plus alpha). https://www.coursera.org/course/progfun Disclaimar I’m just a novice. Watch the lectures by yourself!

Slide 5

Slide 5 text

“Scala is so cool that nobody knows it.” Image: - Cal Henderson http://www.youtube.com/watch?v=i6Fr65PFqfk

Slide 6

Slide 6 text

“I call Scala an old wine in a new bottle.” Image: - Venkat Subramaniam http://www.youtube.com/watch?v=LH75sJAR0hc

Slide 7

Slide 7 text

Image: Programming Paradigms

Slide 8

Slide 8 text

Image: Programming Paradigms ‣ Imperative ໋ྩܕ ‣ Functional ؔ਺ܕ ‣ Logic ࿦ཧϓϩάϥϛϯά


Slide 9

Slide 9 text

Image: Programming Paradigms ‣ Imperative ໋ྩܕ ‣ Functional ؔ਺ܕ ‣ Logic ࿦ཧϓϩάϥϛϯά
 Orthogonal to Object Oriented Programming.

Slide 10

Slide 10 text

Image: ij There's a strong correspondence between Imperative Programs and Computers. Memory cells, load/store instructions, etc...

Slide 11

Slide 11 text

Image: ij There's a strong correspondence between Imperative Programs and Computers. Memory cells, load/store instructions, etc... Pure imperative programming 
 is limited by the "Von Neumann" bottleneck.

Slide 12

Slide 12 text

Image: How can we avoid conceptualizing programs word by word?

Slide 13

Slide 13 text

Image: How can we avoid conceptualizing programs word by word? High-level abstractions: ideally, develop theories of collections, shapes, strings, ...

Slide 14

Slide 14 text

Image: How can we avoid conceptualizing programs word by word? High-level abstractions: ideally, develop theories of collections, shapes, strings, ... Let’s concentrate on defining theories for operators 
 expressed as functions.

Slide 15

Slide 15 text

Image: Theory: data types & operations. Normally, a theory does not describe mutations.

Slide 16

Slide 16 text

Image: Theory: data types & operations. Normally, a theory does not describe mutations. e.g. (a*x + b) + (c*x + d) = (a+c)*x + (b+d)

Slide 17

Slide 17 text

Image: Functional Programming Restricted sense: programming without mutable variables, assignments, loops, and other imperative control structures. Wider sense: Focusing on the functions.

Slide 18

Slide 18 text

Image: ‣ Assignment-less


Slide 19

Slide 19 text

Image: Don't need to worry about thread safety. ‣ Assignment-less


Slide 20

Slide 20 text

Image: Don't need to worry about thread safety. ‣ No side effects
 ‣ Assignment-less


Slide 21

Slide 21 text

Image: Don't need to worry about thread safety. ‣ No side effects
 Multiple calls. 
 Support concurrency. ‣ Assignment-less


Slide 22

Slide 22 text

Image: Don't need to worry about thread safety. ‣ No side effects
 ‣ Referential transparency
 Multiple calls. 
 Support concurrency. ‣ Assignment-less


Slide 23

Slide 23 text

Image: Don't need to worry about thread safety. ‣ No side effects
 ‣ Referential transparency
 Multiple calls. 
 Support concurrency. Can move code up n' down. 
 Can distribute to multicore. ‣ Assignment-less


Slide 24

Slide 24 text

Image: Don't need to worry about thread safety. ‣ No side effects
 ‣ Referential transparency
 Multiple calls. 
 Support concurrency. Can move code up n' down. 
 Can distribute to multicore. ‣ Functions are first-class, ... ‣ Assignment-less


Slide 25

Slide 25 text

Why it matters? Image:

Slide 26

Slide 26 text

Why it matters? Brevity. Easier to debug. Concurrency. Image:

Slide 27

Slide 27 text

Image: ‣ 1959 Lisp ‣ 1975-77 ML, FP, Scheme ‣ 1978 Smalltalk ‣ 1986 Standard ML ‣ 1990 Haskell, Earlang ‣ 1999 XSLT ‣ 2000 OCaml ‣ 2003 Scala, XQuery ‣ 2005 F# ‣ 2007 Clojure

Slide 28

Slide 28 text

Image: http://upload.wikimedia.org/wikipedia/commons/8/85/Scala_logo.png

Slide 29

Slide 29 text

‣ Includes full support of functional programming.
 Currying, pattern matching, lazy evaluation, tail recursion, immutability, etc. ‣ Static typing.
 “In Java, when they said typing they meant your fingers.”
 “You type less, because it is more typed. 
 In a language that is typed less, you type more.”
 - V. Subramaniam http://www.youtube.com/watch?v=LH75sJAR0hc Image: OO + FP

Slide 30

Slide 30 text

Image: Image: Used in Twitter, LinkedIn, Foursquare, Sony, Amazon, Facebook, NASA, ...

Slide 31

Slide 31 text

“ ... if someone had shown me the Programming in Scala book [...] back in 2003 I'd probably have never created Groovy. ” - James Strachan, the creator of Groovy
 http://macstrac.blogspot.jp/2009/04/
 scala-as-long-term-replacement-for.html

Slide 32

Slide 32 text

“ No other language on the JVM seems as capable of being a ‘replacement of Java’ as Scala, and the momentum behind Scala is now unquestionable. ” - Charles Nutter, the lead developer of JRuby http://blog.headius.com/2009/04/future-part-one.html

Slide 33

Slide 33 text

“ If I were to pick a language to use today other than Java, it would be Scala. ” - James Gosling, the father of Java Image:

Slide 34

Slide 34 text

Sounds good, but it is also ... Image:

Slide 35

Slide 35 text

Sounds good, but it is also ... a complex language. a ‘kitchen sink language’. Image:

Slide 36

Slide 36 text

“ The most depressing thing on Scala is [...] most people pick up Scala as ‘Java with more concise syntax’. ”
 - James Gosling https://twitter.com/akarazniewicz/status/11353944609521664

Slide 37

Slide 37 text

Anyway, let’s get started. Image:

Slide 38

Slide 38 text

Image: “Tool Setup” page in Odersky’s course page.
 https://class.coursera.org/progfun-002/wiki/view?page=ToolsSetup


Slide 39

Slide 39 text

On IDEs,“worksheet” might be helpful. Image:

Slide 40

Slide 40 text

On IDEs,“worksheet” might be helpful. Image:

Slide 41

Slide 41 text

giter8 A command line tool to apply templates defined on github. https://github.com/n8han/giter8 sbt A build tool. http://www.scala-sbt.org/ Image:

Slide 42

Slide 42 text

Image: Natural Language Processing & Machine Learning?

Slide 43

Slide 43 text

Image: Scalala - developed by Daniel Ramage, 
 who used to be under Chris Manning. Natural Language Processing & Machine Learning?

Slide 44

Slide 44 text

Image: Scalala - developed by Daniel Ramage, 
 who used to be under Chris Manning. Other libraries: Breeze, OptiML, FACTORIE, and more. Natural Language Processing & Machine Learning?

Slide 45

Slide 45 text

Image: Scalala - developed by Daniel Ramage, 
 who used to be under Chris Manning. Other libraries: Breeze, OptiML, FACTORIE, and more. “Scala and Machine Learning with Andrew McCallum”
 http://www.youtube.com/watch?v=DkRLTToCw-0 Natural Language Processing & Machine Learning?

Slide 46

Slide 46 text

Image: Scalala - developed by Daniel Ramage, 
 who used to be under Chris Manning. Other libraries: Breeze, OptiML, FACTORIE, and more. “Scala and Machine Learning with Andrew McCallum”
 http://www.youtube.com/watch?v=DkRLTToCw-0 NAACL2013 “Tagger in 2hrs” paper - implementation in Scala.
 https://github.com/dhgarrette/low-resource-pos-tagging-2013 Natural Language Processing & Machine Learning?

Slide 47

Slide 47 text

Some Examples Image:

Slide 48

Slide 48 text

First, some brevity. Image:

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

You can drop the things that are obvious.

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

ɾ ɾ ɾ

Slide 56

Slide 56 text

“for” is powerful in Scala. (For more usage, refer to “Programming in Scala” Chapter 7.) ɾ ɾ ɾ

Slide 57

Slide 57 text

Square roots with Newton's method Image:

Slide 58

Slide 58 text

‣ To compute sqrt(x), ‣ Start with initial estimate y (say, 1). ‣ Repeatedly improve the estimate 
 by taking the mean of y and x/y. Image:

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

Iterate until the guess is 
 good enough.

Slide 61

Slide 61 text

Iterate until the guess is 
 good enough. Improve the guess
 by taking mean of
 x and guess.

Slide 62

Slide 62 text

Image: Currying

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

Note that a and b are unchanged.

Slide 65

Slide 65 text

Note that a and b are unchanged.

Slide 66

Slide 66 text

Note that a and b are unchanged. These are special cases of for different values of f .

Slide 67

Slide 67 text

Note that a and b are unchanged. These are special cases of for different values of f . Can we factor out the common pattern?

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

higher order function

Slide 70

Slide 70 text

higher order function

Slide 71

Slide 71 text

higher order function Or even better ...

Slide 72

Slide 72 text

higher order function Or even better ...

Slide 73

Slide 73 text

higher order function Or even better ... anonymous functions

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

Function returning function.

Slide 76

Slide 76 text

Function returning function.

Slide 77

Slide 77 text

Function returning function.

Slide 78

Slide 78 text

Function returning function.

Slide 79

Slide 79 text

Function returning function.

Slide 80

Slide 80 text

Function returning function. Currying Transforming a function that takes multiple arguments in such a way that it can be called as a chain of functions, each with a single argument.

Slide 81

Slide 81 text

Finding Fixed Points Image:

Slide 82

Slide 82 text

Image: Number x is called a fixed point of a function f if f(x) = x.

Slide 83

Slide 83 text

Image: Number x is called a fixed point of a function f if f(x) = x. For some f, we can locate the fixed points by repetitive way: x, f(x), f(f(x)), f(f(f(x))), ... until the value does not vary anymore.

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

Image: Now, sqrt(x) = y such that y*y = x .

Slide 86

Slide 86 text

Image: Now, sqrt(x) = y such that y*y = x . Or, sqrt(x) = y such that y = x /y .

Slide 87

Slide 87 text

Image: Now, sqrt(x) = y such that y*y = x . Or, sqrt(x) = y such that y = x /y . Consequently, sqrt(x) is a fixed point of y → x /y .

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

Controls the oscillation.

Slide 90

Slide 90 text

Rational Numbers ༗ཧ਺ Image:

Slide 91

Slide 91 text

Class

Slide 92

Slide 92 text

Method

Slide 93

Slide 93 text

Method

Slide 94

Slide 94 text

... and other things

Slide 95

Slide 95 text

Operators

Slide 96

Slide 96 text

Operators Relaxed identifiers.

Slide 97

Slide 97 text

Operators Relaxed identifiers.

Slide 98

Slide 98 text

Operators Relaxed identifiers. Precedence rules, just like the maths.

Slide 99

Slide 99 text

Image: Binary Tree ೋ෼໦

Slide 100

Slide 100 text

Image: (Time’s up)

Slide 101

Slide 101 text

Image: (Time’s up) See Coursera week 3 https://class.coursera.org/progfun-002/lecture/index - Class hierarchies - Polymorphism, etc.

Slide 102

Slide 102 text

Some references for further study Image:

Slide 103

Slide 103 text

“Why Scala?” Epic. Just watch it.
 Venkat Subramaniam. www.youtube.com/watch?v=LH75sJAR0hc
 Image: “Functional Programming Principles in Scala” A bliss online course.
 Martin Odersky. www.coursera.org/course/progfun
 Programming in Scala” The Bible, by the creator himself.
 Martin Odersky, Lex Spoon, & Bill Venners.
 Programming Scala” An O’Reilly book. First part publicly available online.
 Dean Wampler & Alex Payne. shop.oreilly.com/product/9780596155964.do
 “Pragmatic Real-World Scala” A nice overview presentation by Typesafe CTO.
 Jonas Bonér. www.slideshare.net/jboner/

Slide 104

Slide 104 text

Image: END. Tanoshii desu kara