Sorami Hisamoto
July 09, 2013
160

# Few Things on Scala

Original presentation at Computational Linguistics Lab, Nara Institute of Science and Technology.

July 09, 2013

## Transcript

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

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

3. 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

4. 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!

5. “Scala is so cool that
nobody knows it.”
Image:
- Cal Henderson

6. “I call Scala an old wine in a new bottle.”
Image:
- Venkat Subramaniam

7. Image:

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

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

10. Image:
ij
There's a strong correspondence between
Imperative Programs and Computers.

11. Image:
ij
There's a strong correspondence between
Imperative Programs and Computers.
Pure imperative programming
is limited by the
"Von Neumann" bottleneck.

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

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

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

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

16. 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)

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

18. Image:
‣ Assignment-less

19. Image:
‣ Assignment-less

20. Image:
‣ No side effects
‣ Assignment-less

21. Image:
‣ No side effects  Multiple calls.
Support concurrency.
‣ Assignment-less

22. Image:
‣ No side effects
‣ Referential transparency
Multiple calls.
Support concurrency.
‣ Assignment-less

23. Image:
‣ No side effects
‣ Referential transparency
Multiple calls.
Support concurrency.
Can move code up n' down.
Can distribute to multicore.
‣ Assignment-less

24. Image:
‣ No side effects
‣ Referential transparency
Multiple calls.
Support concurrency.
Can move code up n' down.
Can distribute to multicore.
‣ Functions are ﬁrst-class, ...
‣ Assignment-less

25. Why it matters?
Image:

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

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

28. Image:

29. ‣ 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 ﬁngers.”
“You type less, because it is more typed.
In a language that is typed less, you type more.”
Image:
OO + FP

30. Image:
Image:

31. ... 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

32. 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

33. If I were to pick a language to
use today other than Java,
it would be Scala.

- James Gosling, the father of Java
Image:

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

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

36. The most depressing thing on Scala is [...]
most people pick up Scala as
‘Java with more concise syntax’.
”
- James Gosling

37. Anyway, let’s get started.
Image:

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

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

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

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

42. Image:
Natural Language Processing
& Machine Learning?

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

44. 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?

45. 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”
Natural Language Processing
& Machine Learning?

46. 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”
NAACL2013 “Tagger in 2hrs” paper - implementation in Scala.
https://github.com/dhgarrette/low-resource-pos-tagging-2013
Natural Language Processing
& Machine Learning?

47. Some Examples
Image:

48. First, some brevity.
Image:

49. You can drop the things that are obvious.

50. ɾ
ɾ
ɾ

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

52. Square roots with
Newton's method
Image:

53. ‣ To compute sqrt(x),
‣ Repeatedly improve the estimate
by taking the mean of y and x/y.
Image:

54. Iterate until
the guess is
good enough.

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

56. Image:
Currying

57. Note that
a and b are
unchanged.

58. Note that
a and b are
unchanged.

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

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

61. higher order
function

62. higher order
function

63. higher order
function
Or even better ...

64. higher order
function
Or even better ...

65. higher order
function
Or even better ...
anonymous
functions

66. Function
returning
function.

67. Function
returning
function.

68. Function
returning
function.

69. Function
returning
function.

70. Function
returning
function.

71. 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.

72. Finding
Fixed Points
Image:

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

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

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

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

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

78. Controls the oscillation.

79. Rational Numbers
༗ཧ਺
Image:

80. Class

81. Method

82. Method

83. ... and other things

84. Operators

85. Operators
Relaxed identiﬁers.

86. Operators
Relaxed identiﬁers.

87. Operators
Relaxed identiﬁers.
Precedence rules,
just like the maths.

88. Image:
Binary Tree
ೋ෼໦

89. Image:
(Time’s up)

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

91. Some references
for further study
Image:

92. “Why Scala?” Epic. Just watch it.
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/

93. Image:
END.
Tanoshii
desu kara