Zio: Implement ZIO.not

Created on 11 Sep 2020  路  3Comments  路  Source: zio/zio

In my code base, I found useful having such combinator:

def not[R, E](effect: ZIO[R, E, Boolean]) =
  effect.map(result => !result)

e.g., if you have method activated(foo), it's quite concise to write not(activated(foo)).

Most helpful comment

I think this makes sense. I might suggest making it a method on the trait itself since it involves a single value and calling it negate to match a couple of other combinators we have in various places in ZIO that do the same thing. So it would be:

trait ZIO[-R, +E, +A] { self =>
  def negate(implicit ev: A <:< Boolean): ZIO[R, E, Boolean] =
    map(a => !ev(a))
}

Then it could be used like activated(foo).negate.

Could also have the not variant on the companion object if we wanted.

All 3 comments

It's quite easy to add it, so I can do it myself. Though, I'd like to have approval from core contributors to do it, in order to avoid doing wasteful work.

I think this makes sense. I might suggest making it a method on the trait itself since it involves a single value and calling it negate to match a couple of other combinators we have in various places in ZIO that do the same thing. So it would be:

trait ZIO[-R, +E, +A] { self =>
  def negate(implicit ev: A <:< Boolean): ZIO[R, E, Boolean] =
    map(a => !ev(a))
}

Then it could be used like activated(foo).negate.

Could also have the not variant on the companion object if we wanted.

consider doing #3635 too 馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jdegoes picture jdegoes  路  4Comments

adamgfraser picture adamgfraser  路  4Comments

iravid picture iravid  路  3Comments

reibitto picture reibitto  路  3Comments

adamgfraser picture adamgfraser  路  4Comments