-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from 47deg/rafa-replace-Id-by-Eval
Provide interpreters with several monad instances
- Loading branch information
Showing
22 changed files
with
260 additions
and
145 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
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
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,21 @@ | ||
package github4s.scalaz | ||
|
||
import cats.MonadError | ||
import scalaz.concurrent.Task | ||
|
||
object implicits { | ||
|
||
implicit val g4sTaskMonadError: MonadError[Task, Throwable] = new MonadError[Task, Throwable] { | ||
|
||
override def pure[A](x: A): Task[A] = Task.now(x) | ||
|
||
override def map[A, B](fa: Task[A])(f: A ⇒ B): Task[B] = fa.map(f) | ||
|
||
override def flatMap[A, B](fa: Task[A])(ff: A ⇒ Task[B]): Task[B] = fa.flatMap(ff) | ||
|
||
override def raiseError[A](e: Throwable): Task[A] = Task.fail(e) | ||
|
||
override def handleErrorWith[A](fa: Task[A])(f: Throwable ⇒ Task[A]): Task[A] = fa.handleWith({ case x ⇒ f(x) }) | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
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,16 @@ | ||
package github4s | ||
|
||
case class GithubApiUrls( | ||
baseUrl: String, | ||
authorizeUrl: String, | ||
accessTokenUrl: String | ||
) | ||
|
||
object GithubDefaultUrls { | ||
|
||
implicit val defaultUrls: GithubApiUrls = GithubApiUrls( | ||
"https://api.github.com/", | ||
"https://github.com/login/oauth/authorize?client_id=%s&redirect_uri=%s&scope=%s&state=%s", | ||
"https://github.com/login/oauth/access_token" | ||
) | ||
} |
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
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,60 @@ | ||
package github4s | ||
|
||
import cats.std.FutureInstances | ||
import cats.std.future._ | ||
import cats.{ Monad, Id, Eval, MonadError } | ||
import github4s.free.interpreters.Interpreters | ||
import scala.concurrent.{ ExecutionContext, Future } | ||
|
||
object implicits extends Interpreters with EvalInstances with IdInstances with FutureInstances | ||
|
||
trait EvalInstances { | ||
|
||
implicit val evalMonadError: MonadError[Eval, Throwable] = new MonadError[Eval, Throwable] { | ||
|
||
override def pure[A](x: A): Eval[A] = Eval.now(x) | ||
|
||
override def map[A, B](fa: Eval[A])(f: A ⇒ B): Eval[B] = fa.map(f) | ||
|
||
override def flatMap[A, B](fa: Eval[A])(ff: A ⇒ Eval[B]): Eval[B] = | ||
fa.flatMap(ff) | ||
|
||
override def raiseError[A](e: Throwable): Eval[A] = | ||
Eval.later({ throw e }) | ||
|
||
override def handleErrorWith[A](fa: Eval[A])(f: Throwable ⇒ Eval[A]): Eval[A] = | ||
Eval.later({ | ||
try { | ||
fa.value | ||
} catch { | ||
case e: Throwable ⇒ f(e).value | ||
} | ||
}) | ||
} | ||
|
||
} | ||
|
||
trait IdInstances { | ||
|
||
implicit def idMonadError(implicit I: Monad[Id]): MonadError[Id, Throwable] = new MonadError[Id, Throwable] { | ||
|
||
override def pure[A](x: A): Id[A] = I.pure(x) | ||
|
||
override def ap[A, B](ff: Id[A ⇒ B])(fa: Id[A]): Id[B] = I.ap(ff)(fa) | ||
|
||
override def map[A, B](fa: Id[A])(f: Id[A ⇒ B]): Id[B] = I.map(fa)(f) | ||
|
||
override def flatMap[A, B](fa: Id[A])(f: A ⇒ Id[B]): Id[B] = I.flatMap(fa)(f) | ||
|
||
override def raiseError[A](e: Throwable): Id[A] = throw e | ||
|
||
override def handleErrorWith[A](fa: Id[A])(f: Throwable ⇒ Id[A]): Id[A] = { | ||
try { | ||
fa | ||
} catch { | ||
case e: Exception ⇒ f(e) | ||
} | ||
} | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.