Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Few Things on Scala

Few Things on Scala

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

Sorami Hisamoto

July 09, 2013
Tweet

More Decks by Sorami Hisamoto

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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!

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  7. Image:
    Programming Paradigms

    View full-size slide

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


    View full-size slide

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

    Orthogonal to
    Object Oriented
    Programming.

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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
    defining theories for operators 

    expressed as functions.

    View full-size slide

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

    View full-size slide

  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)

    View full-size slide

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

    View full-size slide

  18. Image:
    ‣ Assignment-less


    View full-size slide

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


    View full-size slide

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

    ‣ Assignment-less


    View full-size slide

  21. Image:
    Don't need to worry about
    thread safety.
    ‣ No side effects
 Multiple calls. 

    Support concurrency.
    ‣ Assignment-less


    View full-size slide

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

    ‣ Referential transparency

    Multiple calls. 

    Support concurrency.
    ‣ Assignment-less


    View full-size slide

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


    View full-size slide

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


    View full-size slide

  25. Why it matters?
    Image:

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide


  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

    View full-size slide


  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
    http://blog.headius.com/2009/04/future-part-one.html

    View full-size slide


  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:

    View full-size slide

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

    View full-size slide

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

    View full-size slide


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

    View full-size slide

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

    View full-size slide

  38. Image:
    “Tool Setup” page in Odersky’s course page.

    https://class.coursera.org/progfun-002/wiki/view?page=ToolsSetup


    View full-size slide

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

    View full-size slide

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

    View full-size slide

  41. 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:

    View full-size slide

  42. Image:
    Natural Language Processing
    & Machine Learning?

    View full-size slide

  43. Image:
    Scalala - developed by Daniel Ramage, 

    who used to be under Chris Manning.
    Natural Language Processing
    & Machine Learning?

    View full-size slide

  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?

    View full-size slide

  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”

    http://www.youtube.com/watch?v=DkRLTToCw-0
    Natural Language Processing
    & Machine Learning?

    View full-size slide

  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”

    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?

    View full-size slide

  47. Some Examples
    Image:

    View full-size slide

  48. First, some brevity.
    Image:

    View full-size slide

  49. You can drop the things that are obvious.

    View full-size slide

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

    View full-size slide

  51. Square roots with
    Newton's method
    Image:

    View full-size slide

  52. ‣ 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:

    View full-size slide

  53. Iterate until
    the guess is 

    good enough.

    View full-size slide

  54. Iterate until
    the guess is 

    good enough.
    Improve the guess

    by taking mean of

    x and guess.

    View full-size slide

  55. Image:
    Currying

    View full-size slide

  56. Note that
    a and b are
    unchanged.

    View full-size slide

  57. Note that
    a and b are
    unchanged.

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  60. higher order
    function

    View full-size slide

  61. higher order
    function

    View full-size slide

  62. higher order
    function
    Or even better ...

    View full-size slide

  63. higher order
    function
    Or even better ...

    View full-size slide

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

    View full-size slide

  65. Function
    returning
    function.

    View full-size slide

  66. Function
    returning
    function.

    View full-size slide

  67. Function
    returning
    function.

    View full-size slide

  68. Function
    returning
    function.

    View full-size slide

  69. Function
    returning
    function.

    View full-size slide

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

    View full-size slide

  71. Finding
    Fixed Points
    Image:

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  77. Controls the oscillation.

    View full-size slide

  78. Rational Numbers
    ༗ཧ਺
    Image:

    View full-size slide

  79. ... and other things

    View full-size slide

  80. Operators
    Relaxed identifiers.

    View full-size slide

  81. Operators
    Relaxed identifiers.

    View full-size slide

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

    View full-size slide

  83. Image:
    Binary Tree
    ೋ෼໦

    View full-size slide

  84. Image:
    (Time’s up)

    View full-size slide

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

    View full-size slide

  86. Some references
    for further study
    Image:

    View full-size slide

  87. “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/

    View full-size slide

  88. Image:
    END.
    Tanoshii
    desu kara

    View full-size slide