Embed presentation
Download as PDF, PPTX


![Let's
take
this
model
case class Category(
name: String,
parent: Option[Category])
case class Product(
name: String,
description: String,
category: Category)](https://image.slidesharecdn.com/scala-collections1-140805104441-phpapp02/85/Scala-Collections-API-3-320.jpg)


![Mapping
// Get their names in uppercase
val names: List[String] =
products.map(_.name.toUpperCase)](https://image.slidesharecdn.com/scala-collections1-140805104441-phpapp02/85/Scala-Collections-API-6-320.jpg)
![Extrac)ng
// Get category name set
val catNames : Set[String] =
products.map(_.category.name).toSet](https://image.slidesharecdn.com/scala-collections1-140805104441-phpapp02/85/Scala-Collections-API-7-320.jpg)
![Grouping
// Group by the initial of their names
val grouped : Map[Char, List[Product]] =
products.groupBy(_.name.head)](https://image.slidesharecdn.com/scala-collections1-140805104441-phpapp02/85/Scala-Collections-API-8-320.jpg)
![More
Mapping
// Create tuples of product and category
names
val pairs : Tuple2[String,String] =
for {
product <- products
category = product.category
} yield (product.name, category.name)](https://image.slidesharecdn.com/scala-collections1-140805104441-phpapp02/85/Scala-Collections-API-9-320.jpg)


The document discusses the Scala collections API, highlighting its rich library capabilities for manipulating various data structures like lists, sets, and hash maps. It provides a model case demonstrating how to sort a list of products, transform their names to uppercase, retrieve unique category names, group products by the initial letter of their names, and create tuples of product and category names. The document includes practical examples of using higher-order functions for these operations.


![Let's
take
this
model
case class Category(
name: String,
parent: Option[Category])
case class Product(
name: String,
description: String,
category: Category)](https://image.slidesharecdn.com/scala-collections1-140805104441-phpapp02/85/Scala-Collections-API-3-320.jpg)


![Mapping
// Get their names in uppercase
val names: List[String] =
products.map(_.name.toUpperCase)](https://image.slidesharecdn.com/scala-collections1-140805104441-phpapp02/85/Scala-Collections-API-6-320.jpg)
![Extrac)ng
// Get category name set
val catNames : Set[String] =
products.map(_.category.name).toSet](https://image.slidesharecdn.com/scala-collections1-140805104441-phpapp02/85/Scala-Collections-API-7-320.jpg)
![Grouping
// Group by the initial of their names
val grouped : Map[Char, List[Product]] =
products.groupBy(_.name.head)](https://image.slidesharecdn.com/scala-collections1-140805104441-phpapp02/85/Scala-Collections-API-8-320.jpg)
![More
Mapping
// Create tuples of product and category
names
val pairs : Tuple2[String,String] =
for {
product <- products
category = product.category
} yield (product.name, category.name)](https://image.slidesharecdn.com/scala-collections1-140805104441-phpapp02/85/Scala-Collections-API-9-320.jpg)
