注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

阿弥陀佛

街树飘影未见尘 潭月潜水了无声 般若观照心空静...

 
 
 

日志

 
 
关于我

一直从事气象预报、服务建模实践应用。 注重气象物理场、实况场、地理信息、本体知识库、分布式气象内容管理系统建立。 对Barnes客观分析, 小波,计算神经网络、信任传播、贝叶斯推理、专家系统、网络本体语言有一定体会。 一直使用Java、Delphi、Prolog、SQL编程。

网易考拉推荐

Scala - Using reduceLeft to get the max or min value from a collection  

2014-04-02 23:09:54|  分类: Scala |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Scala - Using reduceLeft to get the max or min value from a collection

Scala collections FAQ: Can you share an example that shows how to get the max or min value from a collection using the Scala reduceLeft collections method?

I don't have much time today, but sure, here's a quick example. The following code will determine which student has the max (top/high) score:

package collections.reduce
 
case class Student(name: String, score: Int)
 
object ReduceLeftTest extends App {
   
  val alex = Student("Alex", 83)
  val david = Student("David", 80)
  val frank = Student("Frank", 85)
  val julia = Student("Julia", 90)
  val kim = Student("Kim", 95)
   
  val students = Seq(alex, david, frank, julia, kim)
   
  def max(s1: Student, s2: Student): Student = if (s1.score > s2.score) s1 else s2
 
  val topStudent = students.reduceLeft(max)
  println(s"${topStudent.name} had the highest score: ${topStudent.score}")
 
}

When this code is run it will print the following output:

Kim had the highest score: 95

reduceLeft

How does reduceLeft work? If you change the max method as follows, you can see how reduceLeft works. Here's the new max method:

def max(s1: Student, s2: Student): Student = {
  println(s"comparing ${s1.name} to ${s2.name}")
  if (s1.score > s2.score) s1 else s2
}

When our test object (ReduceLeftTest) is run with this new max method, it prints the following output:

comparing Alex to David
comparing Alex to Frank
comparing Frank to Julia
comparing Julia to Kim
Kim had the highest score: 95

If you're not familiar how the reduceLeft method works, I hope this provides a really big hint.

Okay, here's a little more help: The reduceLeft method marches through the entire collection, beginning at the first element in the collection, and compares two items to each other, using the max method we passed into reduceLeft. For the first test it compares elements 0 and 1, and gets the max value between those two elements. Then it takes that max, compares it to element 2, and again determines the max between those elements. It takes that max, compares it to element 3, and so on, marching through the entire collection.

I can provide a longer, more detailed discussion in the future (if anyone wants it), but until then, I hope this has been a decent little Scala reduceLeft example.

  评论这张
 
阅读(581)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017