-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add mapK to transformers #1987
Add mapK to transformers #1987
Changes from all commits
5fca942
d0d061f
2c4e753
99a1510
d623055
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 |
---|---|---|
|
@@ -17,6 +17,21 @@ sealed abstract class Free[S[_], A] extends Product with Serializable { | |
final def map[B](f: A => B): Free[S, B] = | ||
flatMap(a => Pure(f(a))) | ||
|
||
/** | ||
* Modify the functor context `S` using transformation `f`. | ||
* | ||
* This is effectively compiling your free monad into another | ||
* language by changing the suspension functor using the given | ||
* natural transformation `f`. | ||
* | ||
* If your natural transformation is effectful, be careful. These | ||
* effects will be applied by `mapK`. | ||
*/ | ||
final def mapK[T[_]](f: S ~> T): Free[T, A] = | ||
foldMap[Free[T, ?]] { // this is safe because Free is stack safe | ||
λ[FunctionK[S, Free[T, ?]]](fa => Suspend(f(fa))) | ||
}(Free.catsFreeMonadForFree) | ||
|
||
/** | ||
* Bind the given continuation to the result of this computation. | ||
* All left-associated binds are reassociated to the right. | ||
|
@@ -147,11 +162,9 @@ sealed abstract class Free[S[_], A] extends Product with Serializable { | |
* | ||
* If your natural transformation is effectful, be careful. These | ||
* effects will be applied by `compile`. | ||
*/ | ||
final def compile[T[_]](f: FunctionK[S, T]): Free[T, A] = | ||
foldMap[Free[T, ?]] { // this is safe because Free is stack safe | ||
λ[FunctionK[S, Free[T, ?]]](fa => Suspend(f(fa))) | ||
}(Free.catsFreeMonadForFree) | ||
*/ | ||
@deprecated("Use mapK", "1.0.0") | ||
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 am not sure if we want to deprecate this one. |
||
final def compile[T[_]](f: FunctionK[S, T]): Free[T, A] = mapK(f) | ||
|
||
/** | ||
* Lift into `G` (typically a `EitherK`) given `InjectK`. Analogous | ||
|
@@ -169,7 +182,7 @@ sealed abstract class Free[S[_], A] extends Product with Serializable { | |
*}}} | ||
*/ | ||
final def inject[G[_]](implicit ev: InjectK[S, G]): Free[G, A] = | ||
compile(λ[S ~> G](ev.inj(_))) | ||
mapK(λ[S ~> G](ev.inj(_))) | ||
|
||
override def toString: String = | ||
"Free(...)" | ||
|
@@ -217,11 +230,18 @@ object Free extends FreeInstances { | |
def defer[F[_], A](value: => Free[F, A]): Free[F, A] = | ||
pure(()).flatMap(_ => value) | ||
|
||
/** | ||
* a FunctionK, suitable for composition, which calls mapK | ||
*/ | ||
def mapK[F[_], G[_]](fk: FunctionK[F, G]): FunctionK[Free[F, ?], Free[G, ?]] = | ||
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. why 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. This method is a copy-paste-rename from the original |
||
λ[FunctionK[Free[F, ?], Free[G, ?]]](f => f.mapK(fk)) | ||
|
||
/** | ||
* a FunctionK, suitable for composition, which calls compile | ||
*/ | ||
@deprecated("Use mapK", "1.0.0") | ||
def compile[F[_], G[_]](fk: FunctionK[F, G]): FunctionK[Free[F, ?], Free[G, ?]] = | ||
λ[FunctionK[Free[F, ?], Free[G, ?]]](f => f.compile(fk)) | ||
mapK(fk) | ||
|
||
/** | ||
* a FunctionK, suitable for composition, which calls foldMap | ||
|
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.
This is by accident?
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.
That's Emacs doing some extra housekeeping from having the file open. I'll revert it.
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.
Also-- you're fast! This PR was up for just a few minutes and you were already on top of it 😄.
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.
Haha, yeah, sorry for nitpicking so quickly 😄