Is there any better way to cast nested Option like...
# arrow
v
Is there any better way to cast nested Option like below. Option<Option<List<pair<String, Option<String>>>>
b
Start with
.flatten
to make it
Option<List<Pair<String, Option<String>>>
, and then maybe describe more about the use case?
Depending on what you want the outcome to be it may be possible to use
.traverse
or
.sequence
to get to Option<List<Pair<String, String>>> or even Option<Map<String, String>>
👍 1
v
I Just wants to access the right most Option<String> value
Flatten is not available for option. FlatMap is available
b
.flatMap { it }
==
.flatten
Do you want to go from Option<Option<List<Pair<String, Option<String>>>> to List<String>?
v
Actually I'm writing test cases that needs to verify the value inside the string
So I have to cast way those Options to compare that string with my test string value
b
.flatMap { it }.getOrElse(emptyList()).map { it.right() }.sequence().getOrElse(emptyList()) == listOf(/* expected */)
should at least be in the ballpark
To be honest, though, that type signature is .. difficult .. at best to work with. Usually there are intermediate transformations that deal with reducing the complexity of the type signature into something easier to work
👍 2
v
I already thought of doing like this but replacing None with empty list shadows the usage of Option
b
Also, with this little context it's hard for me to suggest an efficient reduction or comparison for a test
v
Yes understood thanks 👍
r
There is no advantage of using Option over nullable types unless you are using polymorphism to target Option or a related transformer
👍 3
like OptionT
the only big diference is that in nullable types there is no flatMap or flatten because the environment already forbids from ??
but let and others are the same as map over option but in nullable types
b
Will arrow-meta have a syntax for comprehensions over nullable types?
r
nullable types or suspension or other lang features that bakes a monad removes the need fro all the combinators up to Async
we can add that syntax already over nullable types if we wanted with suspension if you mean this:
Copy code
val fa: A? = TODO()
val a: A by fa
v
@raulraja yes, I need to figure out a way to remove some level of nested option
r
@Bob Glamm all strict monads can shortcircuit a suspended continuation via their fold and if the unbiased casedis found raise an exception that the continuation handles itselfs and in this case would return null
b
yes, that's exactly what I was hoping
r
we are removing the way binding happens
delegating to flatMap for all strict monads specializing it and making that faster
@Venkat you can eliminate the nesting of option in your expression inside an Fx block
v
Ok let me try
r
Copy code
val fa: Option<A> = TODO()
Option.fx { 
  val a = !fa
  ... continue unfolding your nested list here
}
👍 3
and
!
every Option
your final result will be an option of your computation
!
will change to
by
👍 1