これまでのScala これからのScala (20100904 Scala座#01 )

Click here to load reader

  • date post

    11-Jul-2015
  • Category

    Documents

  • view

    4.030
  • download

    7

Embed Size (px)

Transcript of これまでのScala これからのScala (20100904 Scala座#01 )

  • ScalaScala

    12010/09/04Scala #01

    201094

  • ID:yuroyoro

    2

    201094

  • Scala

    CodeZineScala

    3

    201094

  • Scala? Scala Way

    2.8kwsk Beyond 2.8

    4

    201094

  • 5

    201094

  • Scala?

    201094

  • JVM +

    by Maartin Odersky

    201094

  • +

    8

    201094

  • 2

    9

    201094

  • JVM

    JVMscalaclass

    .scala.scala

    .scala.class

    war.scala.class

    scalaLibrary.jar

    201094

  • Java?

    201094

  • Less typing

    201094

  • scala> val list = List("foo","bar","baz")list: List[java.lang.String] = List(foo, bar, baz)

    201094

  • scala> list map{ s => s * 2 }

    res6: List[String] = List(foofoo, barbar, bazbaz)

    201094

  • scala> list.headOption match{ case Some( s ) => s * 2 case None => "" }res9: String = foofoo

    201094

  • traitMix-in

    scala> case class Person(name:String,age:Int) extends Ordered[Person]{ def compare(that:Person) = if(name.compare(that.name) == 0) age.compare(that.age) else name.compare(that.name)defined class Person

    scala> Person("ozaki",18) > Person("ozaki",20)res4: Boolean = false

    201094

  • Read-Eval-Print Loop

    17

    201094

  • 18

    201094

  • REPL

    201094

  • 20

    201094

  • (ry

    21

    201094

  • REPL

    LL

    201094

  • Specs

    "A full stack"->-(fullStack) should { behave like "A non-empty stack below full capacity"

    "throw an exception when sent #push" in {

    stack.push(11) must throwAn[Error] }

    }

    201094

  • TDD

    24by @t_wada201094

  • ScalaLL!!(

    201094

  • Scala

    201094

  • 27

    Hashtagimport scala.xml._import scala.io.Source

    object HashTagSearch { def main( args:Array[String] ){ val url = "http://search.twitter.com/search.atom?q=%s".format( args.head ) XML.load( new java.net.URL( url ) ) \\ "entry" map{ e => "%s:%s" format( e \\ "author" \\ "name" text , e \\ "content" text) } foreach{ println } }}

    201094

  • 28

    OneLiner

    scala.xml.XML.load(new java.net.URL

    ("http://search.twitter.com/

    search.atom?q=scalaza01")) \\ "entry"

    map{e => "%s:%s" format(e \\ "author"

    \\ "name" text,e \\ "content" text)}

    foreach{ println }

    201094

  • Scala Way

    201094

  • 30

    201094

  • valvar

    31

    201094

  • immutablemutable

    32

    201094

  • for/while

    33

    201094

  • if/else

    34

    201094

  • Optionnull

    35

    201094

  • 36

    201094

  • 01

    Char

    37

    201094

  • def countChar(s:String,c:Char) = { var cnt = 0 for( x

  • 39

    201094

  • 40

    201094

  • 41

    201094

  • for

    var

    42

    201094

  • 43

    201094

  • def countChar(s:String,c:Char) = s filter{ c == } size

    201094

  • 45

    = Char = char

    201094

  • 46

    201094

  • 02

    cat

    47

    201094

  • def cat(name:String) = { val f = new JFile(name)

    if(f.exists && f.isFile)

    Source.fromFile(f).mkString else

    "No such file : %s" format(name)

    }

    201094

  • 49

    201094

  • Option

    50

    201094

  • abstract class Pathcase class File(name:String,file:JFile) extends Path

    case class Dir(name:String,file:JFile) extends Path

    201094

  • Option

    def file(name:String) = new JFile(name) match { case f if f.exists && f.isFile => Some(File(name,f))

    case f if f.exists && f.isDirectory => Some(Dir(name,f))

    case _ => None}

    201094

  • def cat(name:String) = path(name) collect { case File(name,f) => Source.fromFile(f).mkString

    case Dir(name,f) => "%s is directory" format(name)

    } getOrElse("No such file : %s" format(name))

    201094

  • ?

    54

    201094

  • WARNING:

    55

    201094

  • 56

    201094

  • grep

    57

    201094

  • 03

    grep

    58

    201094

  • def grep(name:String,pattern:String) =

    201094

  • trait

    60

    201094

  • traittrait Text { val content:String

    val name:String

    def formatLine( n:Int,s:String) =

    "%s:%3d:%s" format (name, n+1, s)

    def withLineNumber = content.lines.zipWithIndex

    def formatWithLineNumber = withLineNumber.map{ case (s,n) => formatLine(n,s)}

    }

    201094

  • cattrait

    trait Cat extends Text{ def cat = formatWithLineNumber foreach{ println }

    }

    201094

  • greptraittrait Grep extends Text { def grep(pattern:String) = {

    val r = pattern.r

    withLineNumber collect{ case (s,n) if r.findAllIn(s).nonEmpty =>

    formatLine(n,s)

    } foreach{ println }

    }}

    201094

  • traitabstract class Path { def cat:Unit def grep(pattern:String):Unit}case class File(name:String,file:JFile) extends Path with Cat with Grep{ val content = Source.fromFile(file).mkString}case class Dir(name:String,file:JFile) extends Path { def cat = file.list.foreach{ println } def grep(pattern:String) = file.list.map{ s => path(name+/+s) }. toSeq.flatten.foreach{ f => f.grep(pattern) }}

    201094

  • WARNING:

    65

    201094

  • URLcat/grep

    66

    201094

  • case class URL(name:String) extends Path with Cat with Grep{

    val content =

    Source.fromURL(new java.net.URL(name)).mkString}

    201094

  • 04

    until

    68

    201094

  • def until( p: => Boolean)(f: => Unit) = while( !p ) f

    p : f :

    201094

  • scala> var cnt = 0cnt: Int = 0

    scala> until(cnt > 5 ){ println(cnt);cnt = cnt + 1 } 0

    1

    2

    34

    5

    201094

  • 05

    #librahack

    71

    201094

  • import com.yuroyoro.util.net._

    object LibraHack {

    def crawl( keyword:Sting) = {

    val qs = "hidKensakuF=1&Page=1&lang=jpn&sel1=1&key1=%s&sel2=2&key2=&sel3=3&key3=&sel4=4&key4=&andor=0&SortKubun=1&isbn=&bookz=1&taiz=1&year-from=&year-to=&GengoKubun=no" format( encodeURL(keyword) )

    val res = new HttpConnection( "http://www.library.okazaki.aichi.jp/tosho/asp/Book_Kensaku_g.asp").

    method(HttpPost).

    param( "Accept" -> "application/xml, application/xhtml+xml, text/html;q=0.9, text/plain;q=0.8, image/png, */*;q=0.5").

    param( "User-Agent" -> "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; ja-jp) AppleWebKit/533.17.8 (KHTML, like Gecko) Version/5.0.1 Safari/533.17.8").

    param( "Content-Length" -> qs.length.toString ).

    body( qs ).

    asSource

    }

    }

    201094

  • 73

    201094

  • 201094

  • ?

    201094

  • TwitterFourSquareLinkedInNovel Pulselectricit de France TradingXebiaXMPie(Xerox)Sony Pictures Imageworks

    SiemensGridGainAppJetReaktorTrientalSygnecaHD HoldingsSAIC Mimesis RepublicWattsOn

    201094

  • 201094

  • Scala

    201094

  • Scala

    201094

  • Kestrel memcache

    Gizzard DBsharding framework

    201094

  • Twitter Streaming API Architecture http://www.slideshare.net/jkalucki/chirp-2010streamingapiarchpost

    Streaming APIbackend

    201094

  • 201094

  • 201094

  • 84

    201094

  • Lift Web Framework

    201094

  • ajax/comet view first

    201094

  • PHPScala/Lift3

    201094

  • CodeZine

    201094

  • 89

    201094

  • 2.8kwsk

    201094

  • 2010/07/15Scala 2.8.0 final

    201094

  • package object

    /

    @specialize

    201094

  • break/continueScalaSwingblushupREPLimplicitXML,Actoretc

    201094

  • /

    scala> def foo( s:String = "hoge" , n:Int = 1 ) =

    println("%s:%2d" format(s,n))

    foo: (s: String,n: Int)Unit

    scala> foo()

    hoge: 1

    scala> foo("aaa")

    aaa: 1

    scala> foo(n = 3 )

    hoge: 3

    scala> foo(n = 5 , s = "bar")

    bar: 5

    2

    201094

  • scala> case class (firstName:String,lastName:String,age:Int)

    defined class

    scala> val nene = (" ","",18)

    nene: = (,,18)

    scala> val = nene.copy( firstName = "")

    : = (,,18)

    copy (2.8)

    201094

  • APItraitTraversable,Iterable mapcollect/scanLeftAPI

    201094

  • scala> Map("a" -> 1, "b" -> 2 ,"c" -> 3).map{ case (k,v) => (k*2,v*2) }

    res1: Iterable[(String, Int)] = ArrayBuffer((aa,2), (bb,4), (cc,6))

    2.7

    scala> Map("a" -> 1, "b" -> 2 ,"c" -> 3).map{ case (k,v) => (k*2,v*2) }

    res12: scala.collection.immutable.Map[String,Int] = Map((aa,2), (bb,4), (cc,6))

    2.8

    201094

  • scala> val l = Seq("foo","bar","baz")

    l: Seq[java.lang.String] = List(foo, bar, ba