26
learn in half an hour http://jeffkit.info 11年1月23日星期日

Scala jeff

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Scala jeff

learn in half an hourhttp://jeffkit.info

11年1月23日星期日

Page 2: Scala jeff

Scalable language事件驱动的并发编程模型 (actor)

既 指令型 又 函数式

任何东西都是 对象

构建于JVM之上,also .net

强大的表现能力(DSL)

动静两相宜

11年1月23日星期日

Page 3: Scala jeff

Scala的八卦

11年1月23日星期日

Page 4: Scala jeff

tiobe 2010/1

11年1月23日星期日

Page 5: Scala jeff

Scala!

11年1月23日星期日

Page 6: Scala jeff

twitter 与 scala

11年1月23日星期日

Page 7: Scala jeff

foursquare 开源 rogue

Venue where (_.mayor eqs 1234) and (_.categories contains “thai”) orderAsc (_.popularity)fetch (10)

A Lift/MongoDB Query DSL

11年1月23日星期日

Page 8: Scala jeff

欧盟&Scala

11年1月23日星期日

Page 9: Scala jeff

Scala在社区

11年1月23日星期日

Page 10: Scala jeff

Code Time

11年1月23日星期日

Page 11: Scala jeff

println (“hello Scala”)

包定义

程序入口

函数的定义

常用标准输出函数

11年1月23日星期日

Page 12: Scala jeff

package info.jeffkit

object HelloScala { def main(args : Array[String]) : Unit = { println("hello world") }}

1

2 3

1

2

3

包定义

主程序入口,object -> main

函数的定义

4

4 标准输出11年1月23日星期日

Page 13: Scala jeff

techparty say hello

类的定义及使用

变量声明

DSL雏形

常用标准输出函数

11年1月23日星期日

Page 14: Scala jeff

package info.jeffkit

class Group(val name:String){ def say(msg:String):Unit = { println ("The Group " + this.name + " say : " + msg) }}

object HelloScala { def main(args : Array[String]) : Unit = { println("hello scala") val techparty = new Group("techparty") techparty.say("hello scala") techparty say "i'm the magic of DSL" }}

1

类定义与构造函数

变量声明与创建类

调用类方法

DSL风格的调用

23

4

1

2

3

4

11年1月23日星期日

Page 15: Scala jeff

来自工厂的 hello

11年1月23日星期日

Page 16: Scala jeff

class Group private (val name:String){ def say(msg:String):Unit = { println ("The Group " + this.name + " say : " + msg) }}

object Group{ private val groups = Map( "techparty" -> new Group("techparty"), "barcamp" -> new Group("barcamp"), "openparty" -> new Group("openparty") ) def getGroup(name:String) = { if (groups.contains(name)) groups(name) else null }}

私有构造函数

同名伴生对象

创建Map

工厂方法

1

2

3

4

1

23

4

11年1月23日星期日

Page 17: Scala jeff

集体活动吧(闭包)

11年1月23日星期日

Page 18: Scala jeff

函数值的声明,接收一个函数

类的副构造函数

List的foreach,接受一个闭包

初始化List

1

2

3

4

1

2

3

4

def play(game:String,members:Array[Member],perform:Member => Unit) = { println ("The group is playing a game : " + game ) members.foreach{ member => perform(member) } }

class Member(val name:String,val topic:String){ def this(name:String){ this(name,null) }}

val members = Array( new Member("laiyonghao","male",true,"2010,My choices"), new Member("jeff","male",true,"learn scala in haft an hour"), new Member("iceberg") )

11年1月23日星期日

Page 19: Scala jeff

lambda式闭包

以函数为参数

偏应用函数

函数的curry化调用方式

1

2

3

4

1

2

3

4

//lambda techparty.play("introduce your self", members, member => println ("My name is " + member.name)) //传递函数 def present(member:Member) = { if (member.topic != null) println (member.name + " is presenting.") } techparty.play("present", members, present) //偏应用函数,新函数比原函数多了一些业务性质 val potluck = techparty.play("potluck", members, _:Member => Unit) potluck(_.name + " is eating") // ============ curry化 =============== techparty.play_curry("dismiss", members){ member => member.name + " is leaving and going home" }

11年1月23日星期日

Page 20: Scala jeff

有python fans吗(mixin)

11年1月23日星期日

Page 21: Scala jeff

特性声明,trait

特性扩展,把trait当另一种class即可

实时mixin特性(with)

使用mixin的特性

1

2

3

4

1

2

3

4

trait Fan{ def like() = "techparty"}

trait JavaFan extends Fan{ override def like() = "java " + super.like()}

trait PythonFan extends Fan{ override def like() = "python " + super.like()}

val member = new Member("jeff","learn scala in haft an hour") with JavaFan with PythonFan

member.like()

11年1月23日星期日

Page 22: Scala jeff

沙龙提供各种咨询服务(模式匹配)

11年1月23日星期日

Page 23: Scala jeff

接受任意类型、数量的参数

开始进行参数匹配

各式匹配表达式

处理未匹配情况

1

2

3

4

1

2

3

4

def search(condition:Any):String = { condition match { case "sleepy" => println("you look sleepy,sleep 1 second") Thread.sleep(1000) case q:String => println("search by a keyword :" + q) case ("name",name:String) => println("search by name : " + name) case (year:Int,month:Int) => println("search by year " + year + " and " + month) case _ => println("can't handle your search");return "fail" } "success" }

11年1月23日星期日

Page 24: Scala jeff

来咨询的同学多了去(并发编程)

11年1月23日星期日

Page 25: Scala jeff

actor,创建新线程

向接收者发送消息

创建actor,并保持引用

for 循环,这里要接收两次

1

2

3

4

12

3

4

val receiver = self actor { receiver ! techparty.search("jeff",2010) } val at = actor { receiver ! (self,techparty.search("sleepy")) } println("waiting for actor to finish") for(i <- 1 to 2){ receive { case (at,status:String) => println("hey,at is finish his job,and the status is: " + status) case "success" => println("search finish") case "fail" => println("search fail") } }

5

接收消息,又见case411年1月23日星期日

Page 26: Scala jeff

Thank you!

11年1月23日星期日