-
Notifications
You must be signed in to change notification settings - Fork 451
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
Arrow Fx #1263
Arrow Fx #1263
Conversation
# Conflicts: # modules/docs/arrow-docs/src/main/kotlin/EffectsHelper.kt # modules/effects/arrow-effects-data/src/test/kotlin/arrow/effects/IOTest.kt # modules/effects/arrow-effects-data/src/test/kotlin/arrow/effects/MVarTest.kt # modules/effects/arrow-effects-data/src/test/kotlin/arrow/effects/PromiseTest.kt # modules/effects/arrow-effects-extensions/src/test/kotlin/arrow/effects/RefTest.kt
# Conflicts: # modules/docs/arrow-examples/src/test/kotlin/arrow/DataTypeExamples.kt
# Conflicts: # modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/MonadError.kt
# Conflicts: # modules/effects/arrow-effects-data/src/main/kotlin/arrow/effects/internal/IOBracket.kt # modules/effects/arrow-effects-data/src/main/kotlin/arrow/effects/typeclasses/Async.kt # modules/effects/arrow-effects-data/src/main/kotlin/arrow/effects/typeclasses/Concurrent.kt
...effects-data/src/main/kotlin/arrow/effects/typeclasses/MonadDeferCancellableContinuations.kt
Show resolved
Hide resolved
modules/effects/arrow-effects-data/src/main/kotlin/arrow/effects/typeclasses/UnsafeRun.kt
Show resolved
Hide resolved
...ffects/arrow-effects-data/src/main/kotlin/arrow/effects/typeclasses/suspended/AsyncSyntax.kt
Show resolved
Hide resolved
...ffects/arrow-effects-data/src/main/kotlin/arrow/effects/typeclasses/suspended/AsyncSyntax.kt
Show resolved
Hide resolved
...rrow-effects-data/src/main/kotlin/arrow/effects/typeclasses/suspended/SuspendToKindSyntax.kt
Outdated
Show resolved
Hide resolved
...ects/arrow-effects-data/src/main/kotlin/arrow/effects/typeclasses/suspended/concurrent/Fx.kt
Show resolved
Hide resolved
modules/effects/arrow-effects-data/src/main/kotlin/arrow/effects/unsafe.kt
Outdated
Show resolved
Hide resolved
modules/effects/arrow-effects-data/src/test/kotlin/arrow/effects/EffectsSuspendDSLTests.kt
Show resolved
Hide resolved
modules/meta/arrow-meta/src/main/java/arrow/common/utils/AbstractProcessor.kt
Show resolved
Hide resolved
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.
Little confused with binding
and fx
. Do we want to keep both?
Still, need to review the docs. Awesome work! 🥇
@@ -165,7 +165,7 @@ object ConcurrentLaws { | |||
|
|||
fun <F> Concurrent<F>.cancelableFReceivesCancelSignal(EQ: Eq<Kind<F, Int>>, ctx: CoroutineContext) = | |||
forAll(Gen.int()) { i -> | |||
binding { | |||
fx { |
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.
@raulraja correct me if I am wrong but binding
does not include direct syntax
or suspended syntax
but binding is being deprecated
.
modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/Foldable.kt
Show resolved
Hide resolved
modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/MonadError.kt
Show resolved
Hide resolved
modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/MonadErrorContinuations.kt
Outdated
Show resolved
Hide resolved
import kotlin.coroutines.startCoroutine | ||
|
||
interface MonadSyntax<F> : ApplicativeSyntax<F>, Monad<F> { | ||
override fun <A> f(fa: suspend () -> A): Kind<F, A> = |
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.
Wasn't this named effect
? Or could we name it to something that is more explicit about what it does? liftSuspend { }
?
...ffects/arrow-effects-data/src/main/kotlin/arrow/effects/typeclasses/suspended/AsyncSyntax.kt
Show resolved
Hide resolved
...s/arrow-effects-data/src/main/kotlin/arrow/effects/typeclasses/suspended/ConcurrentSyntax.kt
Show resolved
Hide resolved
...s/arrow-effects-data/src/main/kotlin/arrow/effects/typeclasses/suspended/ConcurrentSyntax.kt
Show resolved
Hide resolved
...es/effects/arrow-effects-data/src/main/kotlin/arrow/effects/typeclasses/suspended/prelude.kt
Show resolved
Hide resolved
modules/effects/arrow-effects-data/src/main/kotlin/arrow/effects/unsafe.kt
Outdated
Show resolved
Hide resolved
@pakoito @nomisRev @JorgeCastilloPrz I addressed all your comments. Let me know if there is anything else you want to discuss. We should probably discuss names over slack. The only one that is important to keep is the |
# Conflicts: # modules/docs/arrow-docs/docs/docs/effects/fiber/README.MD # modules/docs/arrow-docs/docs/docs/integrations/kotlinxcoroutines/README.md # modules/docs/arrow-docs/src/main/kotlin/EffectsHelper.kt # modules/effects/arrow-effects-data/src/test/kotlin/arrow/effects/PromiseTest.kt # modules/effects/arrow-effects-kotlinx-coroutines-data/src/test/kotlin/arrow/effects/coroutines/DeferredKTest.kt # modules/effects/arrow-effects-kotlinx-coroutines-extensions/src/main/kotlin/arrow/effects/coroutines/extensions/deferredk.kt # modules/effects/arrow-effects-rx2-data/src/main/kotlin/arrow/effects/rx2/FlowableK.kt # modules/effects/arrow-effects-rx2-data/src/main/kotlin/arrow/effects/rx2/MaybeK.kt # modules/effects/arrow-effects-rx2-data/src/main/kotlin/arrow/effects/rx2/ObservableK.kt # modules/effects/arrow-effects-rx2-data/src/main/kotlin/arrow/effects/rx2/SingleK.kt
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.
Overall looks good to me, thanks for clarifying the doubts. I've added a couple more comments but can't find a strong reason to not merge and iterate. Great job.
...s/arrow-effects-data/src/main/kotlin/arrow/effects/typeclasses/suspended/ConcurrentSyntax.kt
Show resolved
Hide resolved
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.
Didn't review the docs but I trust you :D
Arrow Fx
Arrow Fx is a library that brings purity, referential transparency and direct syntax for effectful programs denoted as suspended functions.
Arrow Fx removes the need for all the combinators in the Functor hierarchy empowering programmers to express equivalent programs for all suspended capable runtimes like Kotlinx Coroutines, Reactor Framework, Rx2, etc bridging the gap of FP features a user must learn before
is productive defining algebras and interpreters.
The below program illustrates the Hello World of effectful programming with Arrow Fx:
Fx
Hello World
Pure functions
Side effects
A
suspend
function denotes a suspended computation. All side effects should be suspended and non-evaluated in compositions to ensure purity and referential transparency of the effectful expressions.Attempting to run a side effect in the pure environment is disallowed by the Kotlin compiler unless we prove that we know how to handle success and error outcomes from evaluating the effect once the suspension is resumed.
sayHello() //fails to compile
The Kotlin compiler won't allow side-effects denoted as
suspend
to run or compile until you provide aContinuation<A>
that handles success and error cases.Composing effects and pure expressions
Executing effectful programs
All programs encapsulated in
fx
block yield a value at the end wrapped in the concurrent data type the user wishes to make its program concrete to.Running effectful computations is restricted to runtime extensions of the
Unsafe
type class whichIO
provides extensions for.Usage of
unsafe
is reserved to the end of the world and may be the only impure execution of a well-typed functional program:Arrow Fx makes emphasis in guaranteeing users understand where they are performing side effects in their program declaration.
Fx eliminates the need for
F
parameterized tagless style programs.The following combinators illustrate how the Functor hierarchy functions are pointless in the environment given we can declare programs that respect the same semantics thanks to the Kotlin suspension system. These below are examples of a few of the most well known combinators that would disappear from your day to day FP programming and what others would look like flattened in the environment via automatic suspended binding after each combinator is applied in the suspended environment:
just(1).map { it + 1 }
1 + 1
just(1)
1
mapN(just(1), just(2), ::Tuple2)
1 toT 2
tupled(just(1), just(2))
1 toT 2
IO { 1 }.flatMap { n -> IO { n + 1 } }
1 + 1
IO { IO { 1 } }.flatten()
1
IO.delay { 1 }
effect { 1 }
IO.defer { IO { 1 } }
effect { 1 }
Enhancing the ergonomics of Concurrent and Async FP Programs
The examples below demonstrates the expressive power that suspended functions and Arrow Fx brings to reactive programming by eliminating
effects nesting in a simple program that uses parallelism to process effectful computations in parallel and non-blocking.
Concurrent parMap
ank
operatorank:fail
which prints errors in the markdown. /cc @Guardiola31337