Skip to content

Instantly share code, notes, and snippets.

@Daenyth
Last active August 22, 2023 15:58
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save Daenyth/75d3b4edd415fc2de12b0cf5d26e7a9d to your computer and use it in GitHub Desktop.
Save Daenyth/75d3b4edd415fc2de12b0cf5d26e7a9d 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)
attempt F[A] F[Either[E, A]] Given ApplicativeError[F, E]
. . . .
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.evalTap 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
@wTheRockb
Copy link

@Daenyth
Copy link
Author

Daenyth commented May 22, 2018

Another cats typeclass function signature listing: https://arosien.github.io/cats-cheatsheets/typeclasses.pdf

A graphic diagram of typeclass relationships: https://github.com/tpolecat/cats-infographic

@wTheRockb
Copy link

@wTheRockb
Copy link

@Daenyth
Copy link
Author

Daenyth commented Sep 26, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment