A sighting of traverse_ function in Practical FP in Scala

Philip Schwarz
Philip SchwarzSoftware development is what I am passionate about
traverse_
a sighting of
in
@philip_schwarz
slides by http://fpilluminated.com/
by
trait ShoppingCart[F[_]] {
def add(userId: UserId, itemId: ItemId, quantity: Quantity): F[Unit]
def get(userId: UserId): F[CartTotal]
def delete(userId: UserId): F[Unit]
def removeItem(userId: UserId, itemId: ItemId): F[Unit]
def update(userId: UserId, cart: Cart): F[Unit]
}
object ShoppingCart {
def make[F[_]: GenUUID: MonadThrow](
items: Items[F],
redis: RedisCommands[F, String, String],
exp: ShoppingCartExpiration
): ShoppingCart[F] = new ShoppingCart[F] {
…
override def update(userId: UserId, cart: Cart): F[Unit] =
redis.hGetAll(userId.show).flatMap { itemIdToQuantityMap =>
itemIdToQuantityMap.toList.traverse_ { case (itemId, _) =>
ID.read[F, ItemId](itemId).flatMap { id =>
cart.items.get(id).traverse_ { quantity =>
redis.hSet(userId.show, itemId, quantity.show)
}
}
}
} *> redis.expire(userId.show, exp.value).void
…
}
}
with some minor renaming, to ease comprehension
for anyone lacking context – see repo for original
override def update(userId: UserId, cart: Cart): F[Unit] =
redis.hGetAll(userId.show).flatMap { itemIdToQuantityMap =>
itemIdToQuantityMap.toList.traverse_ { case (itemId, _) =>
ID.read[F, ItemId](itemId).flatMap { id =>
cart.items.get(id).traverse_ { quantity =>
redis.hSet(userId.show, itemId, quantity.show)
}
}
}
} *> redis.expire(userId.show, exp.value).void
with some minor renaming, to ease comprehension
for anyone lacking context – see repo for original
override def update(userId: UserId, cart: Cart): F[Unit] =
redis.hGetAll(userId.show).flatMap { itemIdToQuantityMap =>
itemIdToQuantityMap.toList.traverse_ { case (itemId, _) =>
ID.read[F, ItemId](itemId).flatMap { id =>
cart.items.get(id).traverse_ { quantity =>
redis.hSet(userId.show, itemId, quantity.show)
}
}
}
} *> redis.expire(userId.show, exp.value).void
^⇧P Type Info
@typeclass(excludeParents = List("FoldableNFunctions"))
trait Foldable[F[_]] extends UnorderedFoldable[F] with FoldableNFunctions[F] {
…
Traverse F[A] using Applicative[G]. A values will be mapped into G[B] and combined using Applicative#map2.
This method is primarily useful when G[_] represents an action or effect, and the specific A aspect of
G[A] is not otherwise needed.
def traverse_[G[_], A, B](fa: F[A])(f: A => G[B])(implicit G: Applicative[G]): G[Unit] =
override def update(userId: UserId, cart: Cart): F[Unit] =
redis.hGetAll(userId.show).flatMap { itemIdToQuantityMap =>
itemIdToQuantityMap.toList.traverse_ { case (itemId, _) =>
ID.read[F, ItemId](itemId).flatMap { id =>
cart.items.get(id).traverse_ { quantity =>
redis.hSet(userId.show, itemId, quantity.show)
}
}
}
} *> redis.expire(userId.show, exp.value).void
override def update(userId: UserId, cart: Cart): F[Unit] =
redis.hGetAll(userId.show).flatMap { itemIdToQuantityMap =>
itemIdToQuantityMap.toList.traverse_ { case (itemId, _) =>
ID.read[F, ItemId](itemId).flatMap { id =>
cart.items.get(id).traverse_ { quantity =>
redis.hSet(userId.show, itemId, quantity.show)
}
}
}
} *> redis.expire(userId.show, exp.value).void
^⇧P Type Info
Option[Quantity] => (Quantity => F[Boolean]) => F[Unit]
def make[F[_]: GenUUID: MonadThrow]
type MonadThrow[F[_]] = MonadError[F, Throwable]
An applicative that also allows you to raise and or handle an error value.
This type class allows one to abstract over error-handling applicatives.
trait ApplicativeError[F[_], E] extends Applicative[F] { …
This type class allows one to abstract over error-handling monads.
trait MonadError[F[_], E] extends ApplicativeError[F, E] with Monad[F] { …
scala> import cats.implicits._, cats.effect.IO, cats.effect.unsafe.implicits.global
scala> :type Option("snap").traverse_(IO.println)
cats.effect.IO[Unit]
scala> Option("snap").traverse_(IO.println).unsafeRunSync
snap
Applicative
Monad
Functor
ApplicativeError
MonadError
Traverse
Foldable
@typeclass(excludeParents = List("FoldableNFunctions"))
trait Foldable[F[_]] extends UnorderedFoldable[F] with FoldableNFunctions[F] {
…
Traverse F[A] using Applicative[G]. A values will be mapped into G[B] and combined using Applicative#map2.
This method is primarily useful when G[_] represents an action or effect, and the specific A aspect of
G[A] is not otherwise needed.
def traverse_[G[_], A, B](fa: F[A])(f: A => G[B])(implicit G: Applicative[G]): G[Unit] =
override def update(userId: UserId, cart: Cart): F[Unit] =
redis.hGetAll(userId.show).flatMap { itemIdToQuantityMap =>
itemIdToQuantityMap.toList.traverse_ { case (itemId, _) =>
ID.read[F, ItemId](itemId).flatMap { id =>
cart.items.get(id).traverse_ { quantity =>
redis.hSet(userId.show, itemId, quantity.show)
}
}
}
} *> redis.expire(userId.show, exp.value).void
override def update(userId: UserId, cart: Cart): F[Unit] =
redis.hGetAll(userId.show).flatMap { itemIdToQuantityMap =>
itemIdToQuantityMap.toList.traverse_ { case (itemId, _) =>
ID.read[F, ItemId](itemId).flatMap { id =>
cart.items.get(id).traverse_ { quantity =>
redis.hSet(userId.show, itemId, quantity.show)
}
}
}
} *> redis.expire(userId.show, exp.value).void
List[(String,String)] => ((String,String) => F[Unit]) => F[Unit]
def make[F[_]: GenUUID: MonadThrow]
type MonadThrow[F[_]] = MonadError[F, Throwable]
An applicative that also allows you to raise and or handle an error value.
This type class allows one to abstract over error-handling applicatives.
trait ApplicativeError[F[_], E] extends Applicative[F] { …
This type class allows one to abstract over error-handling monads.
trait MonadError[F[_], E] extends ApplicativeError[F, E] with Monad[F] { …
Applicative
Monad
Functor
ApplicativeError
MonadError
Traverse
Foldable
scala> import cats.implicits._, cats.effect.IO, cats.effect.unsafe.implicits.global
scala> :type List("snap", "crackle", "pop").traverse_(IO.println)
cats.effect.IO[Unit]
scala> List("snap", "crackle", "pop").traverse_(IO.println).unsafeRunSync
snap
crackle
pop
^⇧P Type Info
1 of 5

Recommended

Scala Left Fold Parallelisation - Three Approaches by
Scala Left Fold Parallelisation- Three ApproachesScala Left Fold Parallelisation- Three Approaches
Scala Left Fold Parallelisation - Three ApproachesPhilip Schwarz
39 views44 slides
Tagless Final Encoding - Algebras and Interpreters and also Programs by
Tagless Final Encoding - Algebras and Interpreters and also ProgramsTagless Final Encoding - Algebras and Interpreters and also Programs
Tagless Final Encoding - Algebras and Interpreters and also ProgramsPhilip Schwarz
45 views16 slides
Fusing Transformations of Strict Scala Collections with Views by
Fusing Transformations of Strict Scala Collections with ViewsFusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with ViewsPhilip Schwarz
20 views28 slides
A sighting of sequence function in Practical FP in Scala by
A sighting of sequence function in Practical FP in ScalaA sighting of sequence function in Practical FP in Scala
A sighting of sequence function in Practical FP in ScalaPhilip Schwarz
27 views4 slides
N-Queens Combinatorial Puzzle meets Cats by
N-Queens Combinatorial Puzzle meets CatsN-Queens Combinatorial Puzzle meets Cats
N-Queens Combinatorial Puzzle meets CatsPhilip Schwarz
33 views386 slides
Kleisli composition, flatMap, join, map, unit - implementation and interrelat... by
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...Kleisli composition, flatMap, join, map, unit - implementation and interrelat...
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...Philip Schwarz
10 views16 slides

More Related Content

More from Philip Schwarz

Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha... by
Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...Philip Schwarz
826 views20 slides
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t... by
Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...Philip Schwarz
1.2K views46 slides
Jordan Peterson - The pursuit of meaning and related ethical axioms by
Jordan Peterson - The pursuit of meaning and related ethical axiomsJordan Peterson - The pursuit of meaning and related ethical axioms
Jordan Peterson - The pursuit of meaning and related ethical axiomsPhilip Schwarz
141 views12 slides
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c... by
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...Philip Schwarz
100 views1 slide
Defining filter using (a) recursion (b) folding with S, B and I combinators (... by
Defining filter using (a) recursion (b) folding with S, B and I combinators (...Defining filter using (a) recursion (b) folding with S, B and I combinators (...
Defining filter using (a) recursion (b) folding with S, B and I combinators (...Philip Schwarz
78 views1 slide
The Sieve of Eratosthenes - Part 1 - with minor corrections by
The Sieve of Eratosthenes - Part 1 - with minor correctionsThe Sieve of Eratosthenes - Part 1 - with minor corrections
The Sieve of Eratosthenes - Part 1 - with minor correctionsPhilip Schwarz
110 views50 slides

More from Philip Schwarz(20)

Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha... by Philip Schwarz
Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
Philip Schwarz826 views
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t... by Philip Schwarz
Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
Philip Schwarz1.2K views
Jordan Peterson - The pursuit of meaning and related ethical axioms by Philip Schwarz
Jordan Peterson - The pursuit of meaning and related ethical axiomsJordan Peterson - The pursuit of meaning and related ethical axioms
Jordan Peterson - The pursuit of meaning and related ethical axioms
Philip Schwarz141 views
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c... by Philip Schwarz
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...
Philip Schwarz100 views
Defining filter using (a) recursion (b) folding with S, B and I combinators (... by Philip Schwarz
Defining filter using (a) recursion (b) folding with S, B and I combinators (...Defining filter using (a) recursion (b) folding with S, B and I combinators (...
Defining filter using (a) recursion (b) folding with S, B and I combinators (...
Philip Schwarz78 views
The Sieve of Eratosthenes - Part 1 - with minor corrections by Philip Schwarz
The Sieve of Eratosthenes - Part 1 - with minor correctionsThe Sieve of Eratosthenes - Part 1 - with minor corrections
The Sieve of Eratosthenes - Part 1 - with minor corrections
Philip Schwarz110 views
The Sieve of Eratosthenes - Part 1 by Philip Schwarz
The Sieve of Eratosthenes - Part 1The Sieve of Eratosthenes - Part 1
The Sieve of Eratosthenes - Part 1
Philip Schwarz1K views
The Uniform Access Principle by Philip Schwarz
The Uniform Access PrincipleThe Uniform Access Principle
The Uniform Access Principle
Philip Schwarz542 views
Computer Graphics in Java and Scala - Part 1b by Philip Schwarz
Computer Graphics in Java and Scala - Part 1bComputer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1b
Philip Schwarz261 views
The Expression Problem - Part 2 by Philip Schwarz
The Expression Problem - Part 2The Expression Problem - Part 2
The Expression Problem - Part 2
Philip Schwarz650 views
Computer Graphics in Java and Scala - Part 1 by Philip Schwarz
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1
Philip Schwarz403 views
The Expression Problem - Part 1 by Philip Schwarz
The Expression Problem - Part 1The Expression Problem - Part 1
The Expression Problem - Part 1
Philip Schwarz1.2K views
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac... by Philip Schwarz
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Philip Schwarz268 views
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ... by Philip Schwarz
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Philip Schwarz312 views
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ... by Philip Schwarz
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Refactoring: A First Example - Martin Fowler’s First Example of Refactoring, ...
Philip Schwarz630 views
‘go-to’ general-purpose sequential collections - from Java To Scala by Philip Schwarz
‘go-to’ general-purpose sequential collections -from Java To Scala‘go-to’ general-purpose sequential collections -from Java To Scala
‘go-to’ general-purpose sequential collections - from Java To Scala
Philip Schwarz767 views
The Functional Programming Triad of Map, Filter and Fold by Philip Schwarz
The Functional Programming Triad of Map, Filter and FoldThe Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and Fold
Philip Schwarz3.5K views
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala by Philip Schwarz
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaFunctional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Philip Schwarz824 views
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and... by Philip Schwarz
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit - Haskell and...
Philip Schwarz495 views
Quicksort - a whistle-stop tour of the algorithm in five languages and four p... by Philip Schwarz
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Philip Schwarz1.2K views

Recently uploaded

HarshithAkkapelli_Presentation.pdf by
HarshithAkkapelli_Presentation.pdfHarshithAkkapelli_Presentation.pdf
HarshithAkkapelli_Presentation.pdfharshithakkapelli
11 views16 slides
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -... by
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...Deltares
6 views15 slides
Les nouveautés produit Neo4j by
 Les nouveautés produit Neo4j Les nouveautés produit Neo4j
Les nouveautés produit Neo4jNeo4j
27 views46 slides
Geospatial Synergy: Amplifying Efficiency with FME & Esri ft. Peak Guest Spea... by
Geospatial Synergy: Amplifying Efficiency with FME & Esri ft. Peak Guest Spea...Geospatial Synergy: Amplifying Efficiency with FME & Esri ft. Peak Guest Spea...
Geospatial Synergy: Amplifying Efficiency with FME & Esri ft. Peak Guest Spea...Safe Software
391 views59 slides
WebAssembly by
WebAssemblyWebAssembly
WebAssemblyJens Siebert
32 views18 slides
Neo4j : Graphes de Connaissance, IA et LLMs by
Neo4j : Graphes de Connaissance, IA et LLMsNeo4j : Graphes de Connaissance, IA et LLMs
Neo4j : Graphes de Connaissance, IA et LLMsNeo4j
46 views20 slides

Recently uploaded(20)

DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -... by Deltares
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...
Deltares6 views
Les nouveautés produit Neo4j by Neo4j
 Les nouveautés produit Neo4j Les nouveautés produit Neo4j
Les nouveautés produit Neo4j
Neo4j27 views
Geospatial Synergy: Amplifying Efficiency with FME & Esri ft. Peak Guest Spea... by Safe Software
Geospatial Synergy: Amplifying Efficiency with FME & Esri ft. Peak Guest Spea...Geospatial Synergy: Amplifying Efficiency with FME & Esri ft. Peak Guest Spea...
Geospatial Synergy: Amplifying Efficiency with FME & Esri ft. Peak Guest Spea...
Safe Software391 views
Neo4j : Graphes de Connaissance, IA et LLMs by Neo4j
Neo4j : Graphes de Connaissance, IA et LLMsNeo4j : Graphes de Connaissance, IA et LLMs
Neo4j : Graphes de Connaissance, IA et LLMs
Neo4j46 views
MariaDB stored procedures and why they should be improved by Federico Razzoli
MariaDB stored procedures and why they should be improvedMariaDB stored procedures and why they should be improved
MariaDB stored procedures and why they should be improved
What Can Employee Monitoring Software Do?​ by wAnywhere
What Can Employee Monitoring Software Do?​What Can Employee Monitoring Software Do?​
What Can Employee Monitoring Software Do?​
wAnywhere18 views
DSD-INT 2023 Baseline studies for Strategic Coastal protection for Long Islan... by Deltares
DSD-INT 2023 Baseline studies for Strategic Coastal protection for Long Islan...DSD-INT 2023 Baseline studies for Strategic Coastal protection for Long Islan...
DSD-INT 2023 Baseline studies for Strategic Coastal protection for Long Islan...
Deltares10 views
How to Install and Activate Email-Researcher by eGrabber
How to Install and Activate Email-ResearcherHow to Install and Activate Email-Researcher
How to Install and Activate Email-Researcher
eGrabber19 views
El Arte de lo Possible by Neo4j
El Arte de lo PossibleEl Arte de lo Possible
El Arte de lo Possible
Neo4j34 views
A first look at MariaDB 11.x features and ideas on how to use them by Federico Razzoli
A first look at MariaDB 11.x features and ideas on how to use themA first look at MariaDB 11.x features and ideas on how to use them
A first look at MariaDB 11.x features and ideas on how to use them
Federico Razzoli44 views
DSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - Parker by Deltares
DSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - ParkerDSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - Parker
DSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - Parker
Deltares8 views
Advanced API Mocking Techniques by Dimpy Adhikary
Advanced API Mocking TechniquesAdvanced API Mocking Techniques
Advanced API Mocking Techniques
Dimpy Adhikary18 views
Upgrading Incident Management with Icinga - Icinga Camp Milan 2023 by Icinga
Upgrading Incident Management with Icinga - Icinga Camp Milan 2023Upgrading Incident Management with Icinga - Icinga Camp Milan 2023
Upgrading Incident Management with Icinga - Icinga Camp Milan 2023
Icinga36 views
DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM... by Deltares
DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...
DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...
Deltares7 views

A sighting of traverse_ function in Practical FP in Scala

  • 1. traverse_ a sighting of in @philip_schwarz slides by http://fpilluminated.com/ by
  • 2. trait ShoppingCart[F[_]] { def add(userId: UserId, itemId: ItemId, quantity: Quantity): F[Unit] def get(userId: UserId): F[CartTotal] def delete(userId: UserId): F[Unit] def removeItem(userId: UserId, itemId: ItemId): F[Unit] def update(userId: UserId, cart: Cart): F[Unit] } object ShoppingCart { def make[F[_]: GenUUID: MonadThrow]( items: Items[F], redis: RedisCommands[F, String, String], exp: ShoppingCartExpiration ): ShoppingCart[F] = new ShoppingCart[F] { … override def update(userId: UserId, cart: Cart): F[Unit] = redis.hGetAll(userId.show).flatMap { itemIdToQuantityMap => itemIdToQuantityMap.toList.traverse_ { case (itemId, _) => ID.read[F, ItemId](itemId).flatMap { id => cart.items.get(id).traverse_ { quantity => redis.hSet(userId.show, itemId, quantity.show) } } } } *> redis.expire(userId.show, exp.value).void … } } with some minor renaming, to ease comprehension for anyone lacking context – see repo for original
  • 3. override def update(userId: UserId, cart: Cart): F[Unit] = redis.hGetAll(userId.show).flatMap { itemIdToQuantityMap => itemIdToQuantityMap.toList.traverse_ { case (itemId, _) => ID.read[F, ItemId](itemId).flatMap { id => cart.items.get(id).traverse_ { quantity => redis.hSet(userId.show, itemId, quantity.show) } } } } *> redis.expire(userId.show, exp.value).void with some minor renaming, to ease comprehension for anyone lacking context – see repo for original override def update(userId: UserId, cart: Cart): F[Unit] = redis.hGetAll(userId.show).flatMap { itemIdToQuantityMap => itemIdToQuantityMap.toList.traverse_ { case (itemId, _) => ID.read[F, ItemId](itemId).flatMap { id => cart.items.get(id).traverse_ { quantity => redis.hSet(userId.show, itemId, quantity.show) } } } } *> redis.expire(userId.show, exp.value).void ^⇧P Type Info
  • 4. @typeclass(excludeParents = List("FoldableNFunctions")) trait Foldable[F[_]] extends UnorderedFoldable[F] with FoldableNFunctions[F] { … Traverse F[A] using Applicative[G]. A values will be mapped into G[B] and combined using Applicative#map2. This method is primarily useful when G[_] represents an action or effect, and the specific A aspect of G[A] is not otherwise needed. def traverse_[G[_], A, B](fa: F[A])(f: A => G[B])(implicit G: Applicative[G]): G[Unit] = override def update(userId: UserId, cart: Cart): F[Unit] = redis.hGetAll(userId.show).flatMap { itemIdToQuantityMap => itemIdToQuantityMap.toList.traverse_ { case (itemId, _) => ID.read[F, ItemId](itemId).flatMap { id => cart.items.get(id).traverse_ { quantity => redis.hSet(userId.show, itemId, quantity.show) } } } } *> redis.expire(userId.show, exp.value).void override def update(userId: UserId, cart: Cart): F[Unit] = redis.hGetAll(userId.show).flatMap { itemIdToQuantityMap => itemIdToQuantityMap.toList.traverse_ { case (itemId, _) => ID.read[F, ItemId](itemId).flatMap { id => cart.items.get(id).traverse_ { quantity => redis.hSet(userId.show, itemId, quantity.show) } } } } *> redis.expire(userId.show, exp.value).void ^⇧P Type Info Option[Quantity] => (Quantity => F[Boolean]) => F[Unit] def make[F[_]: GenUUID: MonadThrow] type MonadThrow[F[_]] = MonadError[F, Throwable] An applicative that also allows you to raise and or handle an error value. This type class allows one to abstract over error-handling applicatives. trait ApplicativeError[F[_], E] extends Applicative[F] { … This type class allows one to abstract over error-handling monads. trait MonadError[F[_], E] extends ApplicativeError[F, E] with Monad[F] { … scala> import cats.implicits._, cats.effect.IO, cats.effect.unsafe.implicits.global scala> :type Option("snap").traverse_(IO.println) cats.effect.IO[Unit] scala> Option("snap").traverse_(IO.println).unsafeRunSync snap Applicative Monad Functor ApplicativeError MonadError Traverse Foldable
  • 5. @typeclass(excludeParents = List("FoldableNFunctions")) trait Foldable[F[_]] extends UnorderedFoldable[F] with FoldableNFunctions[F] { … Traverse F[A] using Applicative[G]. A values will be mapped into G[B] and combined using Applicative#map2. This method is primarily useful when G[_] represents an action or effect, and the specific A aspect of G[A] is not otherwise needed. def traverse_[G[_], A, B](fa: F[A])(f: A => G[B])(implicit G: Applicative[G]): G[Unit] = override def update(userId: UserId, cart: Cart): F[Unit] = redis.hGetAll(userId.show).flatMap { itemIdToQuantityMap => itemIdToQuantityMap.toList.traverse_ { case (itemId, _) => ID.read[F, ItemId](itemId).flatMap { id => cart.items.get(id).traverse_ { quantity => redis.hSet(userId.show, itemId, quantity.show) } } } } *> redis.expire(userId.show, exp.value).void override def update(userId: UserId, cart: Cart): F[Unit] = redis.hGetAll(userId.show).flatMap { itemIdToQuantityMap => itemIdToQuantityMap.toList.traverse_ { case (itemId, _) => ID.read[F, ItemId](itemId).flatMap { id => cart.items.get(id).traverse_ { quantity => redis.hSet(userId.show, itemId, quantity.show) } } } } *> redis.expire(userId.show, exp.value).void List[(String,String)] => ((String,String) => F[Unit]) => F[Unit] def make[F[_]: GenUUID: MonadThrow] type MonadThrow[F[_]] = MonadError[F, Throwable] An applicative that also allows you to raise and or handle an error value. This type class allows one to abstract over error-handling applicatives. trait ApplicativeError[F[_], E] extends Applicative[F] { … This type class allows one to abstract over error-handling monads. trait MonadError[F[_], E] extends ApplicativeError[F, E] with Monad[F] { … Applicative Monad Functor ApplicativeError MonadError Traverse Foldable scala> import cats.implicits._, cats.effect.IO, cats.effect.unsafe.implicits.global scala> :type List("snap", "crackle", "pop").traverse_(IO.println) cats.effect.IO[Unit] scala> List("snap", "crackle", "pop").traverse_(IO.println).unsafeRunSync snap crackle pop ^⇧P Type Info