Skip to content

Commit

Permalink
Add IdentityBoth instance for zio.prelude.data.Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
guizmaii committed Nov 13, 2024
1 parent 787c1d6 commit 46f9bf0
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions core/shared/src/main/scala/zio/prelude/AssociativeBoth.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package zio.prelude

import zio._
import zio.prelude.coherent.CovariantIdentityBoth
import zio.prelude.data.Optional
import zio.prelude.newtypes.{AndF, Failure, OrF}
import zio.stm.ZSTM
import zio.stream.{ZSink, ZStream}
Expand Down Expand Up @@ -1191,6 +1192,24 @@ object AssociativeBoth extends AssociativeBothLowPriority {
}
}

/**
* The [[IdentityBoth]] (with [[AssociativeBoth]]) instance for [[Optional]].
*/
implicit val OptionalnIdentityBoth: IdentityBoth[Optional] =
new IdentityBoth[Optional] {
val any: Optional[Any] = Optional.Present(())

def both[A, B](fa: => Optional[A], fb: => Optional[B]): Optional[(A, B)] =
fa match {
case Optional.Present(a) =>
fb match {
case Optional.Present(b) => Optional.Present((a, b))
case Optional.Absent => Optional.Absent
}
case Optional.Absent => Optional.Absent
}
}

/**
* The `AssociativeBoth` instance for And `Schedule`.
*/
Expand Down

0 comments on commit 46f9bf0

Please sign in to comment.