Skip to content

Commit e45c9d4

Browse files
authored
Enhanced syntax for creation EitherT/OptionT (#276)
* Enhanced syntax for creation EitherT/OptionT * Renamed anyf lift methods to better express the target form * Revert "Renamed anyf lift methods to better express the target form" This reverts commit 37bb873.
1 parent aba7df8 commit e45c9d4

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

shared/src/main/scala/mouse/anyf.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package mouse
2+
3+
import cats.data.EitherT
4+
import cats.data.OptionT
25
import cats.~>
6+
import cats.Functor
37

48
trait AnyFSyntax {
59
implicit final def anyfSyntaxMouse[F[_], A](fa: F[A]): AnyFOps[F, A] = new AnyFOps(fa)
@@ -8,4 +12,11 @@ trait AnyFSyntax {
812
final class AnyFOps[F[_], A](private val fa: F[A]) extends AnyVal {
913
@inline def ||>[G[_]](f: F ~> G): G[A] = f(fa)
1014
@inline def thrushK[G[_]](f: F ~> G): G[A] = f(fa)
15+
16+
def liftEitherT[E](implicit F: Functor[F]): EitherT[F, E, A] =
17+
EitherT.right[E](fa)
18+
19+
def liftOptionT(implicit F: Functor[F]): OptionT[F, A] =
20+
OptionT.liftF(fa)
21+
1122
}

shared/src/main/scala/mouse/foption.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package mouse
22

3+
import cats.data.EitherT
34
import cats.data.OptionT
45
import cats.{Applicative, FlatMap, Functor, Monad, Traverse}
56

@@ -87,4 +88,7 @@ final class FOptionOps[F[_], A](private val foa: F[Option[A]]) extends AnyVal {
8788

8889
def liftOptionT: OptionT[F, A] =
8990
OptionT(foa)
91+
92+
def liftEitherT[E](ifNone: => E)(implicit F: Functor[F]): EitherT[F, E, A] =
93+
EitherT.fromOptionF(foa, ifNone)
9094
}

shared/src/test/scala/mouse/AnyFSyntaxTest.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package mouse
22

3+
import cats.data.EitherT
4+
import cats.data.OptionT
35
import cats.syntax.option._
46
import cats.syntax.functor._
7+
import cats.syntax.either._
58
import cats.{~>, Id}
69

710
class AnyFSyntaxTest extends MouseSuite {
@@ -39,4 +42,12 @@ class AnyFSyntaxTest extends MouseSuite {
3942
"This"
4043
)
4144
}
45+
46+
test("AnyFSyntax.liftEitherT") {
47+
assertEquals(List(1).liftEitherT[String], EitherT(List(1.asRight[String])))
48+
}
49+
50+
test("AnyFSyntax.liftOptionT") {
51+
assertEquals(List(1).liftOptionT, OptionT(List(Option(1))))
52+
}
4253
}

shared/src/test/scala/mouse/FOptionSyntaxTest.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package mouse
22

33
import cats.data.OptionT
4+
import cats.data.EitherT
5+
import cats.syntax.either._
46

57
class FOptionSyntaxTest extends MouseSuite {
68
test("FOptionSyntax.cata") {
@@ -110,4 +112,9 @@ class FOptionSyntaxTest extends MouseSuite {
110112
assertEquals(List(Option(1)).liftOptionT, OptionT(List(Option(1))))
111113
assertEquals(List(Option.empty[Int]).liftOptionT, OptionT(List(Option.empty[Int])))
112114
}
115+
116+
test("FOptionSyntax.liftEitherT") {
117+
assertEquals(List(Option(1)).liftEitherT("none"), EitherT(List(1.asRight[String])))
118+
assertEquals(List(Option.empty[Int]).liftEitherT("none"), EitherT(List("none".asLeft[Int])))
119+
}
113120
}

0 commit comments

Comments
 (0)