is there a simple way to do something like add two...
# arrow
s
is there a simple way to do something like add two
Option<Int>
values?
m
What do you want to do if one of the options are empty?
s
in this specific case nothing. basically trying to assert that
value 1
+
value 2
<
value 3
but I could define a default value if that makes it easier
yeah actually does make sense to have a default value of 0 for an empty option…since the sum should still be less than a specific value
m
Ok, so something like
Copy code
option1.getOrElse { 0 } + option2.getOrElse { 0 } < 3
s
yeah. that’s basically what I came up with too. felt like I might have been missing a more functional way of representing that logic 🤔
but if not, thanks for the help!
a
You can use
Option
binding so
Copy code
Option.fx().fx{
val value1 = !option1
val value2 = !option2
value1 + value2 < 3
}
That will return an`Option<Boolean>` so you can use it as you want
👍 3
s
I think I like that solution the best. since
Option<Boolean>
is what I need
s
For this use-case that is definitely the most simple & elegant solution.
r
In the future if we have type classes you can do
Option(1) + Option(1)
because we can summon the semigroup of Int automatically
🎉 4
a
🤞
b
I was wondering how to summon
fx
for non-
IO
types. Thanks @AdrianRaFo
a
you're welcome
d
It looks like there is an Applicative instance for Optional, so in theory you can lift addition function over the Optional<Int>s in haskell it would look like:
Copy code
x = Just 1
y = Just 2
(+) <$> x <*> y
which would yield an Otional<int> of 3? You can then fmap (> 3) over that to give you your optional<Boolean> ? Apologies, I have only just started looking at Arrow, I can see applicative in the docs for optional, I just can’t find a way to lift a function over the applicatives. Would be keen to know if this is possible in Arrow
Aha! Ap (same as <*> in example above) is here https://arrow-kt.io/docs/arrow/typeclasses/applicative/#kindf-aap
s
Yes, it’s called
ap
in Arrow. It’s not so popular pattern in Kotlin because of language semantics, similar situation in Scala.
d
so it might look something like?!?!
Copy code
Option.applicative().run { Some(1).ap(Some(2)).ap(Some({ x: Int, y: Int -> x + y })) }
s
But maybe that also depends a little on what you’re used to.
As you can see the resulting code is not very elegant compared to Haskell where it “gets automatically” curried.
d
I see - thanks… so the partial application is the missing part?
s
Well the reason it works so well in Haskell is because
(+) <$> x
returns
Maybe (Int -> Int)
d
yeah, I’ve often thought that applicative style code is missing in vanilla kotlin, we have map and flatmap in all lists - I figured applicatives would be next, just not sure how it would look.
s
It’s mostly because the shapes don’t play nice together in non-curried languages. Having
ap
is useful when doing FP stuff but I’ve found that it becomes less useful in vanilla Kotlin. Most of the times there is a similar snippets that reach much better.
d
I see, thanks for the info Simon, I do intend to plunge deeper into Arrow.
s
Anytime 🙂 Be sure to swing by #arrow with questions or feedback! & in case you’re interested #arrow-contributors.
👍 1