tavish pegram
05/27/2020, 12:50 AMsuspend
modifier) or is it best practice to just use IO to run the effects at the boundary of the system and stick to suspend
to indicate impurity?
For example
suspend fun printAString(s: String): Unit
suspend fun printAString(s: String): IO<Unit>
At the moment I am attempting to stick to a ports and adapters architecture with suspend
for impure functions generally, IO
return types at the service level, and actually unwrapping/running the IO at the controllers. The data is also the boundary but it didn't make a lot of sense to me to unwrap anything there so I've just been using suspend
in the repos.
As a follow-up, does anyone know if it is possible to custom alias the modifier to something like impure
to distinguish from coroutine suspend methods? Or am I thinking about that incorrectly? Like
impure fun printAString(s: String): Unit
Is that something we might want to use Arrow.Meta for?
Thanks!Peter
05/27/2020, 3:30 PMforeach
equivalent for Either
in arrow?streetsofboston
05/27/2020, 3:39 PMfold
works, but it always seems to be a bit wrong. The fold
method returns a value; it doesn’t seem fit to execute a side-effect like a forEach
would do….Marcin Gryszko
05/27/2020, 4:22 PMarrow-fx-mtl
. After writing my message I started to implement my own Concurrent
for EitherT
🙂pakoito
05/27/2020, 4:44 PMsuspend fun bla(): Either<SomeError, Result> = Either.fx {
val ble = somethingWithEither().bind()
somethingElse(ble)
}
pakoito
05/27/2020, 4:44 PMpakoito
05/27/2020, 7:49 PMFred Friis
05/28/2020, 12:00 AMHi, I have a question about bracket
<https://arrow-kt.io/docs/0.10/fx/typeclasses/bracket>
Now, let's say we want to open a file, do some work with it, and then close it. With Bracket, we could make that process look like this:
val safeComputation = openFile("data.json")
.bracket(
release = { file -> closeFile(file) },
use = { file -> fileToString(file) })
This would ensure the file gets closed right after completing the use operation, which would be fileToString(file) here. If that operation throws an error, the file would also be closed.
I understand this, but what if the risky thing we were doing was not in the fileToString() but also in openFile? Eg if the underlying file library throws an exception because the file doesn't exist?
raulraja
05/28/2020, 12:49 PMkushalp
05/28/2020, 8:51 PMValid
, Invalid
and Either
seamlessly? I'm thinking that this must be a fairly common pattern and would like to understand how others have done this in a simple way:
1. Make request to remote API that returns Either<ErrorException, ResponseData>
where ResponseData
is some data class
2. Validate all of the fields in ResponseData
such that they can be converted into Invalid
or Valid
3. Operate on the happy/sad pathspakoito
05/29/2020, 1:46 AMmySet.k().sequence(Either.applicative())
may just work, it should be a member or extension function actuallyMarcin Gryszko
05/29/2020, 6:46 AMConcurrent
- I tried in my program replacing Concurrent.parMapN
with a ParApplicative
instance (obtained by Concurrent.parApplicative(ctx)
). Then, I replaced parMapN
with mapN
and checked which threads effects passed to mapN
were executed in. To my surprise, they were executed in the caller's thread:
C.parApplicative(dispatchers().io()).run {
mapN(getAthleteActivities(), getAthlete()) { (apiActivities, apiAthlete) -> /* transform */ }
}
I checked quickly the implementation; to have a concurrent execution, I have to use map2
extension function:
C.parApplicative(dispatchers().io()).run {
getAthleteActivities().map2(getAthlete()) { (apiActivities, apiAthlete) -> /* transform */ }
}
Is this a conscious design decision? Should mapN
be delegated to parMapN
?than_
05/29/2020, 10:39 AMval list = listOf(1,2,3,4).k()
ListK.monadFilter().fx.monadFilter {
list.bindWithFilter { it % 2 == 0 }
}.let(::println)
crashes with NPE. But this works fine
val list = listOf(1,2,3).k()
ListK.monadFilter().fx.monadFilter {
list.bindWithFilter { it % 2 == 0 }
}.let(::println)
Jörg Winter
05/29/2020, 11:05 AMrt
05/29/2020, 2:16 PMjulian
05/29/2020, 2:35 PMkushalp
05/29/2020, 8:23 PMKind<ForListK, String>
and how can I convert it into a List<String>
?Chris Cordero
05/30/2020, 2:29 AMIO.fx
object in another IO.fx
object?Chris Cordero
05/30/2020, 2:41 AMIO.fx
and when you would just use IO
simon.vergauwen
06/01/2020, 2:22 PMIO.sleep(5.seconds).flatMap {
IO.effect { println("${Instant.now()}: test") }
.repeat(IO.concurrent(), Schedule.spaced(IO.monad(), 5.seconds))
}
Chris Cordero
06/01/2020, 4:15 PM.k()
doing?Chris Cordero
06/02/2020, 4:10 AMIO<List<A>>
is there a great way to transform it into a List<IO<A>>
instead of doing:
val foo: IO<List<A>> = ...
val (bar) = foo
val final = bar.map { IO.just(it) }
tim
06/02/2020, 9:12 AMpakoito
06/02/2020, 9:17 PMval oe: Eq<Option<Int>> =
instead of the inferred `OptionEq<A>`then the second line will stop workingjulian
06/03/2020, 1:31 AMsealed class A {
data class B(val b: String) : A()
data class C(val c: Int) : A()
}
class D
val a: A = TODO()
fun f(a: A): D {
when (a) {
is A.B -> { TODO() }
is A.C -> { TODO() }
}
}
I end up with so many of these `when`s throughout my code. Am I doing something wrong? Or is my allergy to it due to coming from doing only OOP for years? With OOP A
would have some abstract method that B
and C
would implement.CLOVIS
06/03/2020, 9:53 AMValidated<E, A>
as Union2<Success<A>, Collection<E>>
thanerian
06/03/2020, 9:54 AMraulraja
06/03/2020, 2:39 PMtim
06/03/2020, 5:37 PMval a = Either.right(1) // or left
val b = Either.right(2) // or left
val c = Either.right(3) // or left
val d = // magic
d // Either<Nothing, List<Int>>
I've tried playing with flatMap, but i end up with flatMaps inside flapMaps ... which isn't very nice:
val d = a.flatMap { a ->
b.flatMap {
Either.right(listOf(a, it))
}
}
...
I think I'm just missing something basic here as I cant imagine my use case here is unique?tim
06/03/2020, 6:46 PMval a = Either.right(1)
val b = Either.right(2)
val c = Either.right(3)
val d = Either
.applicative<Nothing>()
.mapN(a, b, c) { (a, b, c) ->
listOf(a, b, c)
}
.fix()
.fold(
ifLeft = { println("try again :(") },
ifRight = { println("success: $it") }
)
But I'm now integrating it into my actual use case which is about parsing variables from a url pathtim
06/03/2020, 6:46 PMval a = Either.right(1)
val b = Either.right(2)
val c = Either.right(3)
val d = Either
.applicative<Nothing>()
.mapN(a, b, c) { (a, b, c) ->
listOf(a, b, c)
}
.fix()
.fold(
ifLeft = { println("try again :(") },
ifRight = { println("success: $it") }
)
But I'm now integrating it into my actual use case which is about parsing variables from a url pathjulian
06/03/2020, 6:49 PMtim
06/03/2020, 6:53 PMval a = getEither(1)
val b = getEither(2)
val c = getEither(3)
val d = Either
.applicative<Int>()
.mapN(a, b, c) { (a, b, c) ->
listOf(a, b, c)
}
.fix()
.fold(
ifLeft = { println("try again :(") },
ifRight = { println("success: $it") }
)
fun getEither(value: Int) : Either<String, Int> {
return if(Random.nextBoolean()) {
Either.left("ouch!")
} else {
Either.right(value)
}
}
pakoito
06/03/2020, 6:53 PMEither.conditionally
tim
06/03/2020, 6:56 PM