Skip to content

Instantly share code, notes, and snippets.

@nashid
Forked from Daenyth/MonadAndFs2Ops.md
Created March 6, 2019 19:48
Show Gist options
  • Save nashid/e92916ba409272657d248d79307c596d to your computer and use it in GitHub Desktop.
Save nashid/e92916ba409272657d248d79307c596d to your computer and use it in GitHub Desktop.
Cheat sheet for common cats monad and fs2 operation shapes
Operation Input Result Notes
map F[A] , A => B F[B] Functor
apply F[A] , F[A => B] F[B] Applicative
(fa, fb, ...).mapN (F[A], F[B], ...) , (A, B, ...) => C F[C] Applicative
(fa, fb, ...).tupled (F[A], F[B], ...) F[(A, B, ...)] Applicative
flatMap F[A] , A => F[B] F[B] Monad
traverse F[A] , A => G[B] G[F[A]] Traversable; fa.traverse(f) == fa.map(f).sequence; "foreach with effects"
sequence F[G[A]] G[F[A]] Same as fga.traverse(identity)
. . . .
Stream.emit A Stream[Pure, A]
Stream.emits Iterable[A] Stream[Pure, A]
stream.covary[F] Stream[Pure, A] Stream[F, A] Often useful after emit or emits. ; F is anything with cats-effect Effect[F] available. Usually cats.effect.IO
Stream.eval F[A] Stream[F, A] F: Effect
stream.evalMap Stream[F, A] , A => F[B] Stream[F, B] Run an effect for each element in the stream, returning each result
stream.observe1 Stream[F, A], A => F[Unit] Stream[F, A] Run an effect for each element in the stream, ignoring the result
stream.compile.drain Stream[F, A] F[Unit] Translate the stream description to a description of effects
stream.compile.toVector Stream[F, A] F[Vector[A]] Translate the stream description to a description of effects returning a vector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment