Scala:.take(1)for for-understanding?

时间:2012-11-14 09:52:02

标签: scala for-comprehension

val SumABC = 1000
val Max = 468
val Min = 32

val p9 = for {
  a <- Max to 250 by -1
  b <- Min+(Max-a) to 249
  if a*a+b*b == (SumABC-a-b)*(SumABC-a-b)
} yield a*b*(SumABC-a-b)

我可以。(1)在这里吗? (我试图把它翻译成平面图,过滤器等,但是因为我失败了,我猜它不会那么可读......)

1 个答案:

答案 0 :(得分:2)

如果我理解你的隐秘任务,你想做的是以下

val p9 = (for {
  a <- Max to 250 by -1
  b <- Min+(Max-a) to 249
  if a*a+b*b == (SumABC-a-b)*(SumABC-a-b)
} yield a*b*(SumABC-a-b)).take(1)

只需在yield之前和之后添加括号,以确保在for块的结果上调用take方法