Top Banner
Rubyist Scala 2.0 in Kawasaki Ruby Kaigi @Peranikov
50

Rubyistを誘うScalaの世界 2.0

Jan 08, 2017

Download

Technology

Yuto Matsukubo
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Rubyistを誘うScalaの世界 2.0

Rubyist Scala 2.0

in Kawasaki Ruby Kaigi

@Peranikov

Page 2: Rubyistを誘うScalaの世界 2.0

• (Matsukubo Yuto)

• @Peranikov

• Kawasaki.rb

http://kawasakirb.github.io/

• Ruby

• )Socket Ruby Scala

• We’re hiring!

Page 3: Rubyistを誘うScalaの世界 2.0

T

Page 4: Rubyistを誘うScalaの世界 2.0
Page 5: Rubyistを誘うScalaの世界 2.0
Page 6: Rubyistを誘うScalaの世界 2.0

Rubyist Scala

Kawasaki.rb Ruby

Page 7: Rubyistを誘うScalaの世界 2.0

Scala ?✋

Page 8: Rubyistを誘うScalaの世界 2.0

Rubyist Scala

Page 9: Rubyistを誘うScalaの世界 2.0

• Ruby

Scala

(Rubyist )

Page 10: Rubyistを誘うScalaの世界 2.0

Scala

Page 11: Rubyistを誘うScalaの世界 2.0

Scala

• JVM ( .NET )

• Java BetterJava

Page 12: Rubyistを誘うScalaの世界 2.0

Ruby ?

Page 13: Rubyistを誘うScalaの世界 2.0

Ruby ( )

• Mix-In

• Open Class

• method_missing

Page 14: Rubyistを誘うScalaの世界 2.0

Ruby ( )

• Mix-In

• Open Class

• method_missing

Scala !

Page 15: Rubyistを誘うScalaの世界 2.0
Page 16: Rubyistを誘うScalaの世界 2.0

Ruby

1 + 2 # => 3

1.+(2) # => 3

Page 17: Rubyistを誘うScalaの世界 2.0

Scala

1 + 2 // => 3

1.+(2) // => 3

Page 18: Rubyistを誘うScalaの世界 2.0

Scalaclass MyClass { // ()

def smile: String = { " :-)" } //

def smile(str: String): String = { str + " :-)" }

// {}

def smile2(str: String) = str + " :-)"}

Page 19: Rubyistを誘うScalaの世界 2.0

Scalaval obj = new MyClass

// ()

//

obj.smile

//

obj.smile("Hi,")

// 1 ()

obj smile "Hi,"

Page 20: Rubyistを誘うScalaの世界 2.0
Page 21: Rubyistを誘うScalaの世界 2.0

Ruby

• Enumerable#map(collect)

• Enumerable#flat_map

• Enumerable#reduce(inject)

• Enumerable#select

• Enumerable#find

• etc…

Page 22: Rubyistを誘うScalaの世界 2.0

Scala

• Traversable#map

• Traversable#flatMap

• Traversable#reduceLeft

• Traversable#filter

• Traversable#find

• etc…

Page 23: Rubyistを誘うScalaの世界 2.0

: Ruby map

[1, 2, 3, 4].map { |i| i * 2 }# => [2, 4, 6, 8]

Page 24: Rubyistを誘うScalaの世界 2.0

Scala

List(1, 2, 3, 4).map { i => i * 2 }// => List(2, 4, 6, 8)

Ruby !

Page 25: Rubyistを誘うScalaの世界 2.0

Scala ( )

List(1, 2, 3, 4).map { _ * 2 }// => List(2, 4, 6, 8)

Page 26: Rubyistを誘うScalaの世界 2.0

Q. ? A. Ruby

• Scala map

List(1,2,3).map { i => i * 2 }

List(1,2,3).map( i => i * 2 )

Scala {}

Page 27: Rubyistを誘うScalaの世界 2.0

Ruby lambda# Rubyf = lambda { |i| i + 10 }f.call(20) # => 30

// Scalaval f = (i: Int) => i + 10f(20) // => 30

Page 28: Rubyistを誘うScalaの世界 2.0

:size or length?• List

• size length (size length )

Ruby

List(1,2,3).length // => 3List(1,2,3).size // => 3

Page 29: Rubyistを誘うScalaの世界 2.0

Mix-In

Page 30: Rubyistを誘うScalaの世界 2.0

Ruby module Mix-inmodule Monster def roar ' '

endend

class Godzilla include Monsterend

Godzilla.new.roar # => " "

Page 31: Rubyistを誘うScalaの世界 2.0

Scala Traittrait Monster { def roar = " "

}

class Godzilla extends Monster

(new Godzilla).roar // => " "

Page 32: Rubyistを誘うScalaの世界 2.0

trait Monster { def roar = " "

}

trait HasTail { def swing = " "

}

class Godzilla extends Monster with HasTail

Page 33: Rubyistを誘うScalaの世界 2.0

trait Monster { def roar: String}

class Godzilla extends Monster { def roar = " "

}

(new Godzilla).roar // => " "

Page 34: Rubyistを誘うScalaの世界 2.0

Open Class

Page 35: Rubyistを誘うScalaの世界 2.0

Ruby Open Class

class String def replace_to_scala self.gsub('ruby', 'scala') endend

"ruby is nice!".replace_to_scala# => "scala is nice!"

Page 36: Rubyistを誘うScalaの世界 2.0

Scala Open Class( )

implicit class MyString(val s: String) def replaceToScala = { s.replaceAll("ruby", "scala") }}

"ruby is nice!".replaceToScala// String = scala is nice!

Page 37: Rubyistを誘うScalaの世界 2.0

implicit class MyString(val s: String) def replaceToScala = { s.replaceAll("ruby", "scala") }}

"ruby is nice!".replaceToScala// String = scala is nice!

Page 38: Rubyistを誘うScalaの世界 2.0

implicit conversion

def concat(i: String, j: String): String = { i + j}

implicit def intToString(src: Int): String = { src.toString}

concat(100, 200) // => 100200

Page 39: Rubyistを誘うScalaの世界 2.0

method_missing

Page 40: Rubyistを誘うScalaの世界 2.0

Ruby method_missingclass MyClass def method_missing(name) "#{name} is missing!!" endend

MyClass.new.foo# => "foo is missing!!"

Page 41: Rubyistを誘うScalaの世界 2.0

Scala method_missing (Dynamic )

import scala.language.dynamics

class MyClass extends Dynamic { def selectDynamic(name: String): String = { s"${name} is missing!" }}

(new MyClass).foo // => foo is missing!

Page 42: Rubyistを誘うScalaの世界 2.0

Duck Typing

Page 43: Rubyistを誘うScalaの世界 2.0

Ruby Duck Typing

Page 44: Rubyistを誘うScalaの世界 2.0

Scala Duck Typing(Structural Subtyping)•

• : roar

Page 45: Rubyistを誘うScalaの世界 2.0

1.

type Roarabel = { def roar: String}

Page 46: Rubyistを誘うScalaの世界 2.0

2.

class KingGhidorah { def roar: String = " "

}

class Mothra { def roar: String = " "

}

Page 47: Rubyistを誘うScalaの世界 2.0

3.

def doRoar(target: Roarabel) = { target.roar}

doRoar(new KingGhidorah)// => “ ”

doRoar(new Mothra)// => “ ”

Page 48: Rubyistを誘うScalaの世界 2.0

Scala

• Future

• Scala.js … JS Scala

• Scala Native … Scala LLVM

• Dotty … Scala

Page 49: Rubyistを誘うScalaの世界 2.0

• Scala

• Ruby Scala

• Rubyist Scala !

Page 50: Rubyistを誘うScalaの世界 2.0