学习Scala已经有不少时间了,对FP还是不甚了了。现在决定把自己的点滴的进步都记录下来。
今天看一个写信并等待回复的场景。
Jilen写了一封信给Yision(是我的一个大学同学),现在Jilen把信投递到邮递员Jerry(另外一个大学同学),邮递员把信送给Yision
Yision收到后回复了一封信给Jilen,这封信同样给邮递员Jerry
现在看看我们的类型系统
类型系统分成两类对象,人和消息(信件和回复)
先来看看我们的消息系统
sealed trait Message {
val from: Person
val to: Person
val content: String
}
case class Letter(val from: Person, val to: Person, val content: String) extends Message
case class Reply(val from: Person, val to: Person, val content: String) extends Message
Letter是一封信,Reply是一封回信(这里继承体系也许不那么合理)
再来看看我们的邮递员
/**
* 邮递员
*/
class Postmen(name: String) extends Person(name) {
def act = {
loop {
reactWithin(1000) {
case x: Message =>
println("[postmen] a message from %s to %s".format(x.from.name, x.to.name))
x.to ! x
case TIMEOUT =>
println("[postmen] have no message within 1000 ms, i am now going to have a rest")
exit()
}
}
}
start()
}
邮递员这里做两件事,
- 不停查看有没有收到消息,如果有就说有一封从谁写给谁的信需要投递
- 如果1000ms内没有任何信件,就下班了
Jerry是这里唯一的邮递员
object Jerry extends Postmen("jerry")
再看看Jilen(我)是怎么写信的,
Jilen是独一无二的,所以他是一个object
/* it is me
*/
object Jilen extends Person("jilen") {
def act {
loop {
react {
case (toWhom: Person, content: String) =>
//把信件给邮递员
Jerry ! Letter(this, toWhom, content)
receive {
case Reply(from, to, content) =>
//收到回复了
println("[Jilen] a reply from " + from.name + " received, writes \"" + content + "\"")
exit()
}
}
}
}
/**
* 还不知到这是给谁的信
*/
class NoReceiver(val content: String) {
/**
* 指定信给谁,然后投递给邮递员
*/
def to(whom: Person) = Jilen ! (whom, content)
}
def write(content: String) = {
new NoReceiver(content)
}
start()
}
Jilen有个write方法可以在信纸上写上要说的话,返回一个NoReceiver,可以理解成信封上还没写收信人
/**
* 还不知到这是给谁的信
*/
class NoReceiver(val content: String) {
/**
* 指定信给谁,然后投递给邮递员
*/
def to(whom: Person) = Jilen ! (whom, content)
}
to 方法填好收信人并告诉自己把信投递出去, 投递之后Jilen开始一直等待回复(下面的receive)
case (toWhom: Person, content: String) =>
//把信件给邮递员
Jerry ! Letter(this, toWhom, content)
receive {
case Reply(from, to, content) =>
//收到回复了
println("[Jilen] a reply from " + from.name + " received, writes \"" + content + "\"")
exit()
Yision同样是一个object,他收到了Jilen的信并回复
object Yision extends Person("yision") {
def act = {
loop {
react {
//收到信并回复
case Letter(from, to, content) =>
println("[Yision] receive letter from %s, writes %s".format(from.name, to.name))
Jerry ! Reply(this, from, "i have received a letter from you, and will reply to you soon")
exit()
}
}
}
start()
}
等等!整个程序还差一个入口。
object App {
def main(args: Array[String]) {
Jilen write "hello" to Yision //
}
}
main方法只有一行,Jilen写了一封信给Yision,这就像一个导火线点燃了后面Jerry和Yision的所有动作。
- 大小: 37.4 KB
分享到:
相关推荐
scala-actors-2.10 jar包,可以解决 scala-actors-2.10以上版本带来的不兼容问题
A lot of example code to demonstrate Scala programming language, by the inventor of the language.
Scala By Example(2009_5) ,scala语言的最新学习资料,英文的,慢慢看吧
Scala by example by martin odersky draft 2011
Scala’s actors library, and is directlyinvolved in the developmentof actors in Scala’s standard library. Our goal is that byreading this book, you can learn everything you need to buildefficient, ...
使用实例讲解Scala编程。是Scala官方的2014年6月的最新文档。
Actors in Scala 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
Actors in Scala
scala入门文档。从示例代码中学习scala语言的各种特性,帮助初学者快速入门。
scala by example, learn scala by example
一个使用bazel构建scala的最小示例,包括一个库,二进制文件和测试。 bazel test example-lib:test bazel run example-bin 还包括使用从maven-dependencies.yaml生成可传递的Maven依赖关系到3rdparty的示例 bazel ...
Gradle_Scala_Example Gradle Build包含以intellij想法构建并从控制台运行的示例Scala项目
Actors In Scala .面向对象和函数式编程语言. JVM .concurrency
scala.actors - Concurrency framework inspired by Erlang. scala.io - Input and output. scala.math - Basic math functions and additional numeric types. scala.sys - Interaction with other processes and ...
scala编程 33章 中文pdf Scala编程实战 目录 第1章字符串. 11 第2章数值39 第3章控制结构.60 第4章类和属性.103 第5章方法147 第6章对象170 第7章包和导入.190 第8章特质200 第9章函数式编程214 第10 章集合242 第...
Android Scala示例 带有sbt 0.13.8,Scala 2.11.7和android-sdk-plugin 1.4.4的Android项目要求Java 1.7以上Android SDK 您也可以使用脚本来安装Android SDK在Mac上 $ ./mac_install_sdk.sh在Linux上 $ ./linux_...
By presenting the emerging best practices and designs from the Scala community, it guides you through dozens of powerful techniques example by example.About the Book Scala is a powerful JVM language ...
scala-js-example-app, 使用 Scala.js 构建的示例应用程序 用 Scala.js 编写的应用这是用 Scala.js 编写的应用程序的实例示例。开始要开始,在这个示例项目中打开 sbt,并执行任务 fastOptJS 。 这将创建文件 target...
Working with higher-order functions and the powerful Scala collections library * Leveraging Scala's powerful pattern matching and case classes * Creating concurrent programs with Scala actors * ...
Actors in Scala is the authoritative guide to programming with the actors framework of Scala's standard library, co-written by the creator and lead maintainer, Philipp Haller. The book provides a ...