Help us understand the problem. What is going on with this article?

Scala: Listで前後の値を参照した処理の記述

More than 5 years have passed since last update.

ScalaでList(1, 2, 3, 4, 5)というリストがあったときに、(1, 2), (2, 3), (3, 4), (4, 5)と前後2つペアにして、List(3, 5, 7, 9)のような足し算の処理になるような式の書き方。


val list = List(1, 2, 3, 4, 5)

(list.init zip list.tail).map{ case (a, b) => a + b }


Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away