From 32c0abd72a869c24cfb5fc218712a290e867b2a1 Mon Sep 17 00:00:00 2001 From: Alberto Ballano Date: Sat, 23 Feb 2019 17:03:51 +0100 Subject: [PATCH 1/8] Fix some errors in Fx docs. --- .../arrow-docs/docs/docs/effects/fx/async/README.md | 13 +++++++------ .../docs/docs/effects/fx/polymorphism/README.md | 6 +++--- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/modules/docs/arrow-docs/docs/docs/effects/fx/async/README.md b/modules/docs/arrow-docs/docs/docs/effects/fx/async/README.md index 6ee0fc937c7..cafa9c7992b 100644 --- a/modules/docs/arrow-docs/docs/docs/effects/fx/async/README.md +++ b/modules/docs/arrow-docs/docs/docs/effects/fx/async/README.md @@ -57,7 +57,7 @@ val program = fx { val fiberA = !NonBlocking.startFiber(effect { threadName() }) val fiberB = !NonBlocking.startFiber(effect { threadName() }) val threadA = !fiberA.join() - val threadB = !fiberA.join() + val threadB = !fiberB.join() !effect { println(threadA) } !effect { println(threadB) } } @@ -95,7 +95,7 @@ suspend fun threadName(): String = Thread.currentThread().name data class ThreadInfo( - val threadA : String, + val threadA: String, val threadB: String ) @@ -187,12 +187,13 @@ import arrow.effects.IO import arrow.unsafe import arrow.effects.extensions.io.unsafeRun.runBlocking import arrow.effects.extensions.io.fx.fxCancellable + +fun main() { // The edge of our world //sampleStart -val (_, disposable) = fxCancellable { - !effect { println("BOOM!") } -} + val (_, disposable) = fxCancellable { + !effect { println("BOOM!") } + } //sampleEnd -fun main() { // The edge of our world println(disposable) } ``` diff --git a/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md b/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md index 848435a2f7f..a3e3f09ab05 100644 --- a/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md +++ b/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md @@ -26,11 +26,11 @@ import arrow.effects.extensions.io.fx.fx suspend fun printThreadName(): Unit = println(Thread.currentThread().name) -/* for all `F` that provide an `Fx` extension define a program function +/* for all `F` that provide an `Fx` extension define a program function */ fun Fx.program(): Kind = - fx { !effect { sideEffect() } } + fx { !effect { printThreadName() } } -/* for all `F` that provide an `UnsafeRun` extension define a main function +/* for all `F` that provide an `UnsafeRun` extension define a main function */ fun UnsafeRun.main(fx: Fx): Int = unsafe { runBlocking { fx.program() } } From 110d1fb1cdeb69b9d4a8b1e7b85d159d0b90568f Mon Sep 17 00:00:00 2001 From: Alberto Ballano Date: Sat, 23 Feb 2019 17:04:15 +0100 Subject: [PATCH 2/8] Added comments about crashing or not working code. --- modules/docs/arrow-docs/docs/docs/effects/fx/async/README.md | 4 ++-- .../arrow-docs/docs/docs/effects/fx/polymorphism/README.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/docs/arrow-docs/docs/docs/effects/fx/async/README.md b/modules/docs/arrow-docs/docs/docs/effects/fx/async/README.md index cafa9c7992b..e7115a65e0b 100644 --- a/modules/docs/arrow-docs/docs/docs/effects/fx/async/README.md +++ b/modules/docs/arrow-docs/docs/docs/effects/fx/async/README.md @@ -242,7 +242,7 @@ suspend fun printThreadName(): Unit = println(Thread.currentThread().name) suspend fun program() = - async { printThreadName() } + async { printThreadName() } //XXX This doesn't execute: "IllegalAccessError: tried to access field kotlinx.coroutines.Dispatchers.Default from class kotlinx.coroutines.DeferredKt" fun main() { runBlocking { program().await() } @@ -262,7 +262,7 @@ suspend fun printThreadName(): Unit = println(Thread.currentThread().name) suspend fun program() = - async(start = CoroutineStart.LAZY) { printThreadName() } + async(start = CoroutineStart.LAZY) { printThreadName() } //XXX This doesn't execute: same as above fun main() { runBlocking { program().await() } diff --git a/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md b/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md index a3e3f09ab05..bd863f653e3 100644 --- a/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md +++ b/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md @@ -36,7 +36,7 @@ fun UnsafeRun.main(fx: Fx): Int = /* Run program in the IO monad */ fun main() = - IO.unsafeRun().main(IO.fx()) + IO.unsafeRun().main(IO.fx()) //XXX This doesn't compile //sampleEnd ``` From f28175a4c2c4f4ff60f524ebf4ccb436c089ca5b Mon Sep 17 00:00:00 2001 From: Alberto Ballano Date: Mon, 25 Feb 2019 09:50:22 +0100 Subject: [PATCH 3/8] Added unsafe and runblocking to example. Otherwise it won't print properly --- .../arrow-docs/docs/docs/effects/fx/polymorphism/README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md b/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md index bd863f653e3..aa55ad5b91e 100644 --- a/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md +++ b/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md @@ -219,18 +219,20 @@ import arrow.effects.extensions.io.fx.fx import arrow.core.toT //sampleStart -val result1 = +val result1 = unsafe { runBlocking { fx { val a = !effect { 1 } val b = !effect { 2 } a toT b } +}} -val result2 = +val result2 = unsafe { runBlocking { fx { val b = !effect { 2 } !effect { 1 } toT b } +}} //sampleEnd fun main() { From bb0bc402504bb0a1a6c5d59b210d86a1cfbf3318 Mon Sep 17 00:00:00 2001 From: Alberto Ballano Date: Fri, 15 Mar 2019 18:11:57 +0100 Subject: [PATCH 4/8] Fixed main function not executable Undo some changes. --- .../docs/docs/effects/fx/polymorphism/README.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md b/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md index c94d12dd8eb..6626eb85969 100644 --- a/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md +++ b/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md @@ -35,15 +35,16 @@ suspend fun sideEffect(): Int { /* for all `F` that provide an `Fx` extension define a program function */ fun Fx.program(): Kind = - fx { !effect { printThreadName() } } + fx { !effect { sideEffect() } } /* for all `F` that provide an `UnsafeRun` extension define a main function */ fun UnsafeRun.main(fx: Fx): Int = unsafe { runBlocking { fx.program() } } /* Run program in the IO monad */ -fun main() = - IO.unsafeRun().main(IO.fx()) //XXX This doesn't compile +fun main(args: Array) { + IO.unsafeRun().main(IO.fx()) +} //sampleEnd ``` @@ -226,20 +227,18 @@ import arrow.effects.extensions.io.fx.fx import arrow.core.toT //sampleStart -val result1 = unsafe { runBlocking { +val result1 = fx { val a = !effect { 1 } val b = !effect { 2 } a toT b } -}} -val result2 = unsafe { runBlocking { +val result2 = fx { val b = !effect { 2 } !effect { 1 } toT b } -}} //sampleEnd fun main() { From 3e224b39f1510e74bfe6c0c63ca1a922f3a63937 Mon Sep 17 00:00:00 2001 From: Alberto Ballano Date: Fri, 15 Mar 2019 18:22:39 +0100 Subject: [PATCH 5/8] Added a doubt --- .../arrow-docs/docs/docs/effects/fx/polymorphism/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md b/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md index 6626eb85969..a56e551800c 100644 --- a/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md +++ b/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md @@ -195,8 +195,8 @@ import arrow.data.extensions.list.fx.fx //sampleStart val result1 = unsafe { fx { - val (a) = listOf(1, 2) - val (b) = listOf(true, false) + val (a) = listOf(1, 2) // This is destructuring the list and is obtaining the first element, + val (b) = listOf(true, false) // that's why the output is `ListK(list=[Tuple2(a=1, b=true)])` a toT b } } From c5404ce81aeda3993edbaa5121d33d447f3b13c6 Mon Sep 17 00:00:00 2001 From: Alberto Ballano Date: Fri, 15 Mar 2019 18:38:10 +0100 Subject: [PATCH 6/8] Replaced ank block with kotlin. --- modules/docs/arrow-docs/docs/docs/effects/fx/README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/modules/docs/arrow-docs/docs/docs/effects/fx/README.md b/modules/docs/arrow-docs/docs/docs/effects/fx/README.md index ad381ee76a3..aad1607279d 100644 --- a/modules/docs/arrow-docs/docs/docs/effects/fx/README.md +++ b/modules/docs/arrow-docs/docs/docs/effects/fx/README.md @@ -74,8 +74,7 @@ Applying and composing suspended side effects is allowed in the presence of othe In the example below, `sayHello` and `sayGoodBye` are valid inside `greet` because they are all suspended functions. -```kotlin:ank:playground -//sampleStart +```kotlin suspend fun sayGoodBye(): Unit = println("Good bye World!") @@ -86,7 +85,6 @@ suspend fun greet(): Unit { sayHello() // this is ok because sayGoodBye() // `greet` is also `suspend` } -//sampleEnd ``` #### `fx` composition From 38c37d6991784e5b393d02ffac1cd3bc1cc3b7c5 Mon Sep 17 00:00:00 2001 From: Alberto Ballano Date: Sat, 16 Mar 2019 18:22:56 +0100 Subject: [PATCH 7/8] Fix list binding sample --- .../docs/docs/effects/fx/polymorphism/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md b/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md index a56e551800c..15ef6a5c839 100644 --- a/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md +++ b/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md @@ -193,18 +193,18 @@ import arrow.core.toT import arrow.data.extensions.list.fx.fx //sampleStart -val result1 = unsafe { +val result1 = unsafe { fx { - val (a) = listOf(1, 2) // This is destructuring the list and is obtaining the first element, - val (b) = listOf(true, false) // that's why the output is `ListK(list=[Tuple2(a=1, b=true)])` + val a = !listOf(1, 2).k() + val b = !listOf(true, false).k() a toT b } } val result2 = unsafe { fx { - val (b) = listOf(true, false) - listOf(1, 2) toT b + val b = !listOf(true, false).k() + !listOf(1, 2).k() toT b } } //sampleEnd From 3ca6a272f2c92f79ec5b0f876637953835d11c64 Mon Sep 17 00:00:00 2001 From: Alberto Ballano Date: Sat, 16 Mar 2019 18:49:00 +0100 Subject: [PATCH 8/8] Added missing import. --- .../arrow-docs/docs/docs/effects/fx/polymorphism/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md b/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md index 15ef6a5c839..7847f07e79d 100644 --- a/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md +++ b/modules/docs/arrow-docs/docs/docs/effects/fx/polymorphism/README.md @@ -188,9 +188,10 @@ Arrow identifies non-blocking binding in these non-commutative monads as unsafe For consistency, if you want to shoot yourself in the foot, performing free environmental effect application for these non-commutative types requires the user to give explicit permission to activate the unsafe `fx` block. ```kotlin:ank:playground -import arrow.unsafe import arrow.core.toT import arrow.data.extensions.list.fx.fx +import arrow.data.k +import arrow.unsafe //sampleStart val result1 = unsafe {