Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A sighting of traverse_ function in Practical F...

A sighting of traverse_ function in Practical FP in Scala

Philip Schwarz

October 08, 2023
Tweet

More Decks by Philip Schwarz

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. @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
  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 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