Logo

Programming-Idioms

History of Idiom 7 > diff from v45 to v46

Edit summary for version 46 by :
New Scala implementation by user [meshelton]

Version 45

2016-02-16, 16:46:54

Version 46

2016-02-16, 21:21:42

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Code
val items = List("a", "b", "c")
items.zipWithIndex.foreach{ case (item, index) => 
  println(s"$index => $item")
}
Comments bubble
zipWithIndex takes each item in a collection and replaces it with a tuple of (item, index)