Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@jczuchnowski
Copy link
Member

Part of work for #1217 . In short - improve working with options:

    val value1: Task[Option[Int]] = Task(Some(1))
    val value2: Task[Option[Int]] = Task(Some(2))

    val result: IO[Option[Throwable], Int] = for {
      val1 <- value1.some
      val2 <- value2.some
    } yield val1 + val2
        
    val resultOption: Task[Option[Int]] = result.peelError

@jczuchnowski jczuchnowski changed the title #1217 - operators for option type [WIP] #1217 - operators for option type Jul 23, 2019
@adamgfraser
Copy link
Contributor

In case it is helpful, here is the work I did on implementing none and fuseError.

  def none[B](implicit ev: A <:< Option[B]): ZIO[R, Option[E], Unit] =
    self.foldM(
      e => ZIO.fail(Some(e)),
      a => ev(a).fold[ZIO[R, Option[E], Unit]](ZIO.succeed(()))(_ => ZIO.fail(None))
    )
  def fuseError[E1, E2 <: E1](default: E2)(implicit ev: E <:< Option[E1]): ZIO[R, E1, A] =
    self.mapError(e => ev(e).getOrElse(default))

final def some[B](implicit ev: A <:< Option[B]): ZIO[R, Option[E], B] =
self.foldM(e => ZIO.fail(Some(e)), a => a.fold[ZIO[R, Option[E], B]](ZIO.fail(Option.empty[E]))(ZIO.succeed(_).mapError(_ => Option.empty[E])))

final def peelError[E1](implicit ev: E <:< Option[E1]): ZIO[R, E1, Option[A]] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we should call this optional. Better name than peelError, anyway. Otherwise looks good!

@jczuchnowski
Copy link
Member Author

@jdegoes @adamgfraser what are the actual use cases for none and fuseError. I'm trying to come up with a meaningful test cases.

@adamgfraser
Copy link
Contributor

adamgfraser commented Jul 28, 2019

@jczuchnowski none is basically asserting that the optional value in a ZIO is empty and failing if it is not. So you should have:

IO.succeed(None).none === IO(())
IO.succeed(Some(1)).none === IO.fail(Option.empty)
IO.fail("Fail").none === IO.fail(Some("Fail"))

Fuse error is taking a ZIO that could fail with Some(error) or None and adding a default error value to fail with to replace the None case. So you should have:

IO.fail(Some("Fail")).fuseError("Default") === IO.fail("Fail")
IO.fail(None).fuseError("Default") === IO.fail("Default")

final def forkOn(ec: ExecutionContext): ZIO[R, E, Fiber[E, A]] =
self.on(ec).fork

final def fuseError[E1, E2 <: E1](default: E2)(implicit ev: E <:< Option[E1]): ZIO[R, E1, A] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we call this flattenErrorOption?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely more descriptive.

jdegoes
jdegoes previously approved these changes Aug 3, 2019
Copy link
Member

@jdegoes jdegoes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small rename, then good to merge!

@jczuchnowski jczuchnowski changed the title [WIP] #1217 - operators for option type #1217 - operators for option type Aug 4, 2019
@jdegoes jdegoes merged commit 51b841a into zio:master Aug 4, 2019
ghostdogpr pushed a commit to ghostdogpr/scalaz-zio that referenced this pull request Aug 5, 2019
* zio#1217 - operators for option type

* zio#1217 - more operators

* rename and add tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants