D26 Scala增强

Scala重点语法整理
①定义函数
   
   
  1. def functionName ([list of parameters]):[return type]={
  2.  
  3. function body{}
  4.  
  5. return[expr]
  6. }
示例
    
    
  1. def fun1(a:String,b:Int):Unit={
  2.  
  3. println("nima")
  4.  
  5. }
②函数传入方法
   
   
  1. def time()={
  2. println("Getting time in nano seconds")
  3. System.nanoTime
  4. }
  5.  
  6. def delayed( t:=>Long)={
  7. println("In delayed method")
  8. println("Param: "+ t)
  9. }
  10.  
  11. def main(args:Array[String]):Unit={
  12. delayed(time()) //先执行delayed方法体,执行到t时才执行内部传入的time()方法
  13. }
  14. }
  15. /*
  16. In delayed method
  17. Getting time in nano seconds
  18. Param: 1117398067174855
  19. */
③递归函数,for循环中反复调用自身
④String* 是该字符串可变长度的
⑤函数的参数是默认的情况
   
   
  1. def addInt( a:Int=5, b:Int=7):Int={
  2. var sum:Int=0
  3.  
  4. sum = a + b
  5. return sum //执行时,缺省参数时,按照默认参数计算!!
  6. }
⑥柯里函数: 柯里转换函数接受多个参数成一条链的函数,每次取一个参数。柯里函数是具有多个参数列表定义
   
   
  1. def main(args:Array[String]){
  2. val str1:String="Hello, "
  3. val str2:String="Scala!"
  4. println("str1 + str2 = "+ strcat(str1)(str2))
  5. }
  6. def strcat(s1:String)(s2:String)={
  7. s1 + s2
  8. }
⑦元组的使用
    
    
  1. def main(args:Array[String]){
  2. val t =(4,3,2,1)
  3. val sum =t._1 + t._2 + t._3 + t._4
  4. println("Sum of elements: "+ sum )
  5. }

单机用Scala的actor来写word count

   
   
  1. package cn.itcast.actor
  2. import scala.actors.{Actor,Future}
  3. import scala.collection.mutable.{HashSet,ListBuffer}
  4. import scala.io.Source
  5. /**
  6. * Created by root on 2016/5/11.
  7. */
  8. classTaskextendsActor{
  9. override def act():Unit={
  10. loop {
  11. react {
  12. caseSubmitTask(filename)=>{
  13. //局部统计, 结果是Map[String, Int]
  14. val result =Source.fromFile(filename).getLines().flatMap(_.split(" ")).map((_,1)).toList.groupBy(_._1).mapValues(_.size)
  15. sender !ResultTask(result)//发送ResultTask, 用它来包装result
  16. }
  17. caseStopTask=>{
  18. exit()
  19. }
  20. }
  21. }
  22. }
  23. }
  24. caseclassSubmitTask(filename:String)
  25. caseclassResultTask(result :Map[String,Int])
  26. case object StopTask
  27. object ActorWordCount{
  28. def main(args:Array[String]){
  29. val replySet =newHashSet[Future[Any]]()
  30. val resultList =newListBuffer[ResultTask]()//存储actor读取的内容
  31. val files =Array[String]("c://words.txt","c://a.txt")
  32. for(f <- files){
  33. val actor =newTask
  34. val reply = actor.start()!!SubmitTask(f)//启动, 并发送消息,返回Future

    !

    发送异步消息,没有返回值。

    !?

    发送同步消息,等待返回值。

    !!

    发送异步消息,返回值是 Future[Any]

  35. replySet += reply //把这些Future放到集合中
  36. }
  37. while(replySet.size >0){
  38. val toCompute = replySet.filter(_.isSet)//取出有效的结果, 待处理的数据
  39. for(f <- toCompute){
  40. val result = f().asInstanceOf[ResultTask]//获取实例, 注意f后要加(), 调用apply(), 否则会报转换异常
  41. resultList += result
  42. replySet -= f
  43. }
  44. Thread.sleep(100)
  45. }
  46. //汇总的功能
  47. //List((hello, 5), (tom,3), (helllo, 2), (jerry, 2))
  48. val fr = resultList.flatMap(_.result).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2))
  49. println(fr)
  50. }
  51. }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值