-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide interpreters with several monad instances #18
Changes from 8 commits
d9df6e5
cbfb06b
5429868
39fb7d8
02e7cfa
3f9efd1
e24321f
5921aaa
6b56f6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd overwrite |
||
|
||
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.
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" | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can drop the braces here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense |
||
|
||
} | ||
|
||
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] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that the instance of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you tested I'd and task in a file where either one was required as implícits. @dialelo and I have experienced implícits ambiguity presumably because Task also conforms to Id given I'd is applicable to anything. Not sure we should provide this instance here. We may have to look into a LowerPriorityImplicits trait stack to avoid ambiguity. See also cats for examples of this. |
||
|
||
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) | ||
} | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!