From 9a5470e9e0995c66b32444126ffa8f111fab6ac1 Mon Sep 17 00:00:00 2001 From: Marissa Date: Thu, 26 Jun 2025 13:22:36 -0400 Subject: [PATCH] Add `Monad#{when,unless}M` --- core/src/main/scala/cats/Monad.scala | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/src/main/scala/cats/Monad.scala b/core/src/main/scala/cats/Monad.scala index c694f854e3..9a07f68aef 100644 --- a/core/src/main/scala/cats/Monad.scala +++ b/core/src/main/scala/cats/Monad.scala @@ -162,6 +162,12 @@ trait Monad[F[_]] extends FlatMap[F] with Applicative[F] { tailRecM(branches.toList)(step) } + def whenM[A](cond: F[Boolean])(f: => F[A]): F[Unit] = + ifM(cond)(ifTrue = void(f), ifFalse = unit) + + def unlessM[A](cond: F[Boolean])(f: => F[A]): F[Unit] = + ifM(cond)(ifTrue = unit, ifFalse = void(f)) + /** * Modifies the `A` value in `F[A]` with the supplied function, if the function is defined for the value. * Example: