質問

Let's say I have List(1,2,3,4,5) and I want to get List(3,5,7,9), that is, the sums of the element and the previous (1+2, 2+3,3+4,4+5)

I tried to do this by making two lists:

val list1 = List(1,2,3,4)
val list2 = (list1.tail ::: List(0))                   // 2,3,4,5,0
for (n0_ <- list1; n1th_ <- list2) yield (n0_ + n1_)

But that combines all the elements with each other like a cross product, and I only want to combine the elements pairwise. I'm new to functional programming and I thought I'd use map() but can't seem to do so.

役に立ちましたか?

解決

List(1, 2, 3, 4, 5).sliding(2).map(_.sum).to[List] does the job.

Docs:

def sliding(size: Int): Iterator[Seq[A]] 

Groups elements in fixed size blocks by passing a "sliding window" over them (as opposed to partitioning them, as is done in grouped.)

他のヒント

You can combine the lists with zip and use map to add the pairs.

val list1 = List(1,2,3,4,5)
list1.zip(list1.tail).map(x => x._1 + x._2)

res0: List[Int] = List(3, 5, 7, 9)

Personally I think using sliding as Infinity has is the clearest, but if you want to use a zip-based solution then you might want to use the zipped method:

( list1, list1.tail ).zipped map (_+_)

In addition to being arguably clearer than using zip, it is more efficient in that the intermediate data structure (the list of tuples) created by zip is not created with zipped. However, don't use it with infinite streams, or it will eat all of your memory.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top