scala> val nums = Array(-2,1,-3,4,6,-1,2,1,1,-5,4)
nums: Array[Int] = Array(-2, 1, -3, 4, 6, -1, 2, 1, 1, -5, 4)
scala> nums.foldLeft(List[Int]()){
case (init :+ last, num) if last > 0 && num > 0 => init :+ (last + num)
case (res, num) => res :+ num
}
res0: List[Int] = List(-2, 1, -3, 10, -1, 4, -5, 4)
Или:
scala> def reduce(nums: Seq[Int]): Seq[Int] = nums match {
| case x1+:x2+:xs => if(x1>0 && x2>0) reduce((x1+x2)+:xs) else x1+:reduce(x2+:xs)
| case ns => ns
| }
def reduce(nums: Seq[Int]): Seq[Int]
scala> reduce(nums)
val res1: Seq[Int] = ArraySeq(-2, 1, -3, 10, -1, 4, -5, 4)