Skip to content

Latest commit

 

History

History
20 lines (17 loc) · 647 Bytes

1-collections-ops.md

File metadata and controls

20 lines (17 loc) · 647 Bytes
optionId scastieLink codeTitle description
collections-ops
Functional programming with immutable collections
High-level operations avoid the need for complex and error-prone loops.
val fruits =
  List("apple", "banana", "avocado", "papaya")

val countsToFruits = // count how many 'a' in each fruit
  fruits.groupBy(fruit => fruit.count(_ == 'a'))

for (count, fruits) <- countsToFruits do
  println(s"with 'a' × $count = $fruits")
// prints: with 'a' × 1 = List(apple)
// prints: with 'a' × 2 = List(avocado)
// prints: with 'a' × 3 = List(banana, papaya)