Stian N
04/03/2020, 9:00 AMevery { someFunction(any()) } answers { Either.left("Go left")) }
max.cruz
04/03/2020, 1:50 PMWarning: arrow.core.extensions.ListKCrosswalk$crosswalk$1$2$1$1: can't find referenced class arrow.core.extensions.ListKCrosswalk$crosswalk$1$2
Warning: arrow.core.extensions.ListKCrosswalk$crosswalk$1$2$1$1: can't find referenced class arrow.core.extensions.ListKCrosswalk$crosswalk$1$2
I found this closed issue: https://github.com/arrow-kt/arrow/issues/807
Someone know if there is a workaround for this problem?Jake
04/03/2020, 11:46 PMGopal S Akshintala
04/04/2020, 12:26 PMthan_
04/06/2020, 5:12 PMherlevsen
04/07/2020, 11:33 AMOptionT
. My stack is IO<Option<User>>
. I cant figure out what to import to get OptionT.fx.
I have tried importing arrow.mtl.extensions.fx like described here: https://arrow-kt.io/docs/0.10/arrow/mtl/optiont/, but that doesn’t workherlevsen
04/07/2020, 3:47 PMundefined
from Haskell, in Kotlin? I can of course just cast any value to the type i need, but just typing any
would be easier 🙂dnowak
04/08/2020, 8:58 AMAnton K
04/08/2020, 6:57 PMhttps://youtu.be/hmX2s3pe_qk?t=1494▾
s
parameter in Suspend
? You can find my implementation here: https://github.com/kuschanton/fp-in-kotlin/blob/master/src/main/kotlin/fp/kotlin/monad/free/Expr03.kt
Any help appreciated 🙂
Kind regards,
Arrow newbiepakoito
04/09/2020, 1:05 PMpakoito
04/09/2020, 1:05 PMSatyam Agarwal
04/09/2020, 2:29 PMpakoito
04/10/2020, 12:21 PMpakoito
04/10/2020, 12:23 PMsemantically,so youis correctEither<Nothing, Either<Throwable, Int>>
fold({ exception }, { it.fold({ error }, { success })})
Jérémy CROS
04/10/2020, 2:08 PMJakub Pi
04/13/2020, 4:26 PMtrait Parsers[ParseError, Parser[+_]] {
def run[A](p: Parser[A])(input: String): Either[ParseError,A]
}
I've tried to express this using Arrow as below:
class ForParser private constructor()
typealias ParserOf<T> = Kind<ForParser, T>
inline fun <T> ParserOf<T>.fix(): Parser<T> = this as Parser<T>
class Parser<A>(val v: A) : ParserOf<A>
interface Parsers<ParseError> {
fun <A> run(p : Parser<A>) : (String) -> Either<ParseError, A>
fun char(c : Char) : Parser<Char>
}
So, this is my first pass which actually doesn't give any compile-time errors. But I'm confused by a few points:
• Parsers interface is now only generic on the error and not on the Parser, what am I missing and how do I express it?
• What is the syntax for the annotation @higherkind
to eliminate the first three lines of boilerplate? If I just annotate the Parser class, ParserOf is flagged as undefined.
• I'm not calling fix() anywhere, would I do that in the concrete implementation?
• Scala seems to have a nice syntax for declaring curried functions and I've eliminated the named parameter input when I translated the run function. Is there a better way to express this?
BTW Found this talk really helpful https://yowconference.com/talks/jacob-bass/yow-lambda-jam-2018/higher-kinded-types-in-a-lower-kinded-language-functional-programming-in-kotlin-code-jam-6368/CLOVIS
04/13/2020, 7:16 PMpakoito
04/14/2020, 3:25 PMdnowak
04/14/2020, 10:03 PMIO.unsafeRunSync()
so dangerous that it cannot be called in production code? I need to execute IO
in the current thread and unsafeRunSync
seems reasonable in such case. Questions:
1. What can go wrong during the execution of unsafeRunSync
?
2. How to execute IO
in the current thread?Nicolas Lattuada
04/15/2020, 9:23 AMpakoito
04/15/2020, 9:41 AMrcfgnu
04/15/2020, 11:48 AMfun x() : IO<Either<Failure,String>>
fun y(xResult: String) : IO<Either<Failure,Int>>
fun z(yResult: Int) : IO<Either<Failure,String>>
It's my design totally wrong?Jorge Castillo
04/15/2020, 11:56 AMPhani Mahesh
04/19/2020, 7:00 AMIvan Brko
04/22/2020, 1:02 PMfun magic(s: String): Either<Exception, String>
val x = magic("2")
val value = when(x) {
is Either.Left -> when (x.a){
is NumberFormatException -> "Not a number!"
is IllegalArgumentException -> "Can't take reciprocal of 0!"
else -> "Unknown error"
}
is Either.Right -> "Got reciprocal: ${x.b}"
}
And above the code, the following is written: You should also notice that we are using SmartCast for accessing Left
and Right
values.
I expected that to mean that after matching X with Either.Left, the compiler would know that x is of type Exception (and the same for right, but there that x is of type String). Here you still have to call .a or .b to get the value.
So sorry if this is a stupid question, but what exactly is smart casted here and would it be possible to get Kotlin to know that x is of type String after matching it with Right here?antonicg
04/22/2020, 4:00 PMcatch
function from the Either
is a suspend function? Is it possible to catch an exception without using an IO
?Ivan Brko
04/22/2020, 6:59 PMfun <T : Any> Result<T>.toEither(): Either<RequestError, T> =
fold({ Either.right(it)}, {Either.left(calculateError())})
However, this doesn't compile (although Intellij doesn't show that anything is wrong with the code), as Either.right
creates Either<Nothing, T>
and Either.left
creates Either<T, Nothing>
, so neither branching of the fold returns the needed type. Can I create wanted Either here (without having to create it with val, specify its exact type and returning that). I hoped simply doing something like Either<RequestError, T>.right
would work, but of course it doesn't 😄pakoito
04/24/2020, 3:46 PMhandleErrorWith
?Skotar
04/24/2020, 8:27 PMclass OneError
class TwoError
private fun oneEither(): Either<OneError, String> = OneError().left()
private fun twoEither(value: String): Either<TwoError, Int> = value.toInt().right()
fun either() {
oneEither()
.flatMap { twoEither(it) }
.fold(
{ throw Exception(it::class.simpleName) },
{ ... }
)
}
private fun oneIO(): IO<OneError, String> = IO.raiseError(OneError())
private fun twoIO(value: String): IO<TwoError, Int> = IO.just(value.toInt())
fun io() {
oneIO()
.flatMap { twoIO(it) }
.unsafeRunSyncEither()
.fold(
{ throw Exception(it::class.simpleName) },
{ ... }
)
}
The function either
throws java.lang.Exception: OneError
as I expect. But the function io
throws OneError cannot be cast to class TwoError
. When I look at the signature of flatMap
(fun <E, A, B, E2 : E> IOOf<E, A>.flatMap(f: (A) -> IOOf<E2, B>): IO<E2, B>
), I think that it shouldn't even compile.
Can you tell me if my understanding is correct? I expect that IO
with two types should behave like Either
.davec
04/24/2020, 9:34 PMfun doSomething(name:String): Either<FailReason,Int>
and I have a names:List<String>
.
I could do val result:List<Either<FailReason,Int>> = names.map { doSomething(it) }
which is going to give me a list of Either
instances. Fine.
Now let's assume I want the iteration to stop upon the first case that doSomething()
returns a left. This could be done using a sequence like this:
val result2:Either<FailReason,Int> = names.asSequence().map { doSomething(it) }.first { it is Either.Left }
but it's going to return the first left that fails, or the right that results from the last call to doSomething()
. All intermediate "right" values will be discarded.
What if I wanted it to return Either<FailReason,List<Int>>
? Essentially, return the first left (terminating any subsequent calls to doSomething()
), or a list of all the rights? Does Arrow provide any help here? (Also I'm not looking for a solution involving while loops or mutable variables.)davec
04/24/2020, 9:34 PMfun doSomething(name:String): Either<FailReason,Int>
and I have a names:List<String>
.
I could do val result:List<Either<FailReason,Int>> = names.map { doSomething(it) }
which is going to give me a list of Either
instances. Fine.
Now let's assume I want the iteration to stop upon the first case that doSomething()
returns a left. This could be done using a sequence like this:
val result2:Either<FailReason,Int> = names.asSequence().map { doSomething(it) }.first { it is Either.Left }
but it's going to return the first left that fails, or the right that results from the last call to doSomething()
. All intermediate "right" values will be discarded.
What if I wanted it to return Either<FailReason,List<Int>>
? Essentially, return the first left (terminating any subsequent calls to doSomething()
), or a list of all the rights? Does Arrow provide any help here? (Also I'm not looking for a solution involving while loops or mutable variables.)fun <F,A,B> Sequence<F>.shortCircuitEither(fn: (F) -> Either<A,B>): Either<A,List<B>> {
val items = mutableListOf<B>()
this.forEach {item ->
fn(item).fold(
{ return it.left() },
{ items.add(it) }
)
}
return items.right()
}
Usage: val result =names.asSequence().shortCircuitEither { doSomething(it) }
pakoito
04/25/2020, 3:00 AMsequence
, which is a specialised traverse
names.traverse(Either.applicative()) { doSomething(it) }
Either<Error, List<Bla>>
if doSomething
returns Either<Error, Bla>
List<Either<Error, Bla>>
just call sequence(Either.applicative())
davec
04/25/2020, 6:25 PMtraverse
doesn't actually return an Either
, everything's wrapped in a Kind
. Example:
val numbers = listOf(3,5,0,1,10)
fun fn(a:Int):Either<FailReason, Int> = if (a == 0) FailReason.left() else a.right()
val traverseResult:Kind<EitherPartialOf<FailReason>, Kind<ForListK, Int>> =
numbers.traverse(Either.applicative(), ::fn)
val resultAsEither:Either<FailReason, List<Int>> = traverseResult.fix().flatMap{ it.fix().right() }
pakoito
04/28/2020, 11:34 PMtraverseResult.fix().map{ it.fix() }