-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
89 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cats | ||
package io | ||
|
||
import cats.data.Xor | ||
|
||
import java.lang.Throwable | ||
|
||
import scala.reflect.ClassTag | ||
|
||
final class IO[A] private[io] (private val eval: Eval[A]) extends Serializable { | ||
def unsafePerformIO(): A = eval.value | ||
|
||
def catchOnly[T >: Null <: Throwable: ClassTag]: IO[T Xor A] = | ||
new IO(Eval.always(Xor.catchOnly[T](eval.value))) | ||
|
||
def catchNonFatal: IO[Throwable Xor A] = | ||
new IO(Eval.always(Xor.catchNonFatal(eval.value))) | ||
|
||
def map[B](f: A => B): IO[B] = | ||
new IO(eval.map(f)) | ||
|
||
def flatMap[B](f: A => IO[B]): IO[B] = | ||
new IO(eval.flatMap(a => f(a).eval)) | ||
} | ||
|
||
object IO extends IOInstances { | ||
/** Capture a side-effecting expression as a new IO primitive. */ | ||
def always[A](a: => A): IO[A] = | ||
new IO[A](Eval.always(a)) | ||
|
||
def pure[A](a: A): IO[A] = | ||
new IO[A](Eval.now(a)) | ||
} | ||
|
||
trait IOInstances { | ||
implicit val ioInstances: Monad[IO] = | ||
new Monad[IO] { | ||
def pure[A](a: A): IO[A] = IO.pure(a) | ||
override def pureEval[A](a: Eval[A]): IO[A] = new IO(a) | ||
def flatMap[A, B](ma: IO[A])(f: A => IO[B]): IO[B] = ma.flatMap(f) | ||
override def map[A, B](ma: IO[A])(f: A => B): IO[B] = ma.map(f) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package cats | ||
package io | ||
|
||
import cats.laws.discipline.arbitrary._ | ||
|
||
import org.scalacheck.Arbitrary | ||
import org.scalacheck.Arbitrary.arbitrary | ||
|
||
trait IOSuite { | ||
implicit def ioArbitrary[A: Arbitrary]: Arbitrary[IO[A]] = | ||
Arbitrary(arbitrary[Eval[A]].map(ea => new IO(ea))) | ||
|
||
implicit def ioEq[A: Eq]: Eq[IO[A]] = | ||
Eq.by(_.unsafePerformIO()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cats | ||
package io | ||
|
||
import cats.laws.discipline.{MonadTests, SerializableTests} | ||
import cats.tests.CatsSuite | ||
import cats.laws.discipline.eq.tuple3Eq | ||
|
||
class IOTests extends CatsSuite with IOSuite { | ||
checkAll("IO", MonadTests[IO].monad[Int, String, Double]) | ||
checkAll("Monad[IO]", SerializableTests.serializable(Monad[IO])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters