Mark Fisher
11/25/2019, 12:18 PMval o = Option.functor().multiplyBy2(Option(1))
assertThat(o.fix()).isEqualTo(Option(2))
But I've just come across this in the dependency injection notes
fun <F> Show<Kind<F, Int>>.printAllValues(fa: List<Kind<F, Int>>): Unit {
fa.forEach { println(fa.show()) }
}
I'm trying to write some code to use this, but I'm having trouble finding the right form to invoke it.
How would I call it with (say) a list of Option<Int> values?
Thanks!raulraja
11/25/2019, 12:22 PMimport arrow.Kind
import arrow.core.extensions.show
fun List<Int>.printAllValues(): Unit {
Int.show().run {
forEach { println(it.show()) }
}
}
fun main(args: Array<String>) {
listOf(1, 2, 3).printAllValues()
}
raulraja
11/25/2019, 12:22 PMInt.show
directly otherwise you would add Show<A>
as argument to the function if it was A
instead of Int
Mark Fisher
11/25/2019, 12:32 PMlistOf(Option(1), Option(2))
- I'm specifically trying to understand how to use the given printAllValues as an extension function of Show<Kind<F, Int>>
so I can work out how to pass a value in to itsimon.vergauwen
11/25/2019, 12:35 PMSome(1)
and None
then you could simply adjust the example the way Raul suggested.
import arrow.Kind
import arrow.core.extensions.show
fun <A> List<A>.printAllValues(show: Show<A>): Unit {
show.run {
forEach { println(it.show()) }
}
}
fun main(args: Array<String>) {
listOf(1, 2, 3).printAllValues(Int.show()
listOf(Some(1), None, Some(3)).printAllValues(Option.show(Int.show))
}
Mark Fisher
11/25/2019, 12:38 PMsimon.vergauwen
11/25/2019, 12:50 PMA
instead of Int
, so I didn’t really write a new function but I made his example function more generic instead.simon.vergauwen
11/25/2019, 12:50 PMInt
it now also works with Option<Int>
, String
or anything else you can provide a Show
instance for.Mark Fisher
11/25/2019, 12:50 PMfun <F> Show<Kind<F, Int>>.printAllValues(fa: List<Kind<F, Int>>): Unit {
fa.forEach { println(fa.show()) }
}
but don't know how to call it, or use it. I was after an example using a list of Optionssimon.vergauwen
11/25/2019, 12:51 PMOption.show(Int.show()).printAllValues(emptyList())
Mark Fisher
11/25/2019, 12:53 PMsimon.vergauwen
11/25/2019, 12:54 PMsimon.vergauwen
11/25/2019, 12:55 PMOption.show
is not parameterized to take a Show<A>
instance but uses toString()
instead.Mark Fisher
11/25/2019, 12:55 PMsimon.vergauwen
11/25/2019, 12:56 PMOption.show().printAllValues(items)
Mark Fisher
11/25/2019, 12:57 PMsimon.vergauwen
11/25/2019, 12:57 PMOptionShow<A>
should take a Show<A>
instead so can customize the printing of A
. If you’re interested in contributing I could help you make the fix 🙂simon.vergauwen
11/25/2019, 12:57 PMMark Fisher
11/25/2019, 12:58 PMsimon.vergauwen
11/25/2019, 1:07 PMprintAllValues
function because Show
for option is not defined for the HKT
but only for the concrete type. Show<Option<A>>
instead of Show<OptionOf<A>>
. Same offer applies if you want to contribute I’d love to help 😉simon.vergauwen
11/25/2019, 1:08 PMShow
definition for Option
your example should also work.
fun <A> Option.Companion.show2(): Show<OptionOf<A>> =
Show.invoke { fix().toString() }
simon.vergauwen
11/25/2019, 1:08 PMOptionOf<A>
instead of Option<A>
.simon.vergauwen
11/25/2019, 1:09 PMMark Fisher
11/25/2019, 1:10 PMsimon.vergauwen
11/25/2019, 1:12 PMA
instead of Kind<F, Int>
anyway.Mark Fisher
11/25/2019, 1:23 PMprintln(it.show())
from what I can tell.simon.vergauwen
11/25/2019, 1:24 PMMark Fisher
11/25/2019, 1:34 PMsimon.vergauwen
11/25/2019, 1:36 PMraulraja
11/25/2019, 7:44 PMraulraja
11/25/2019, 7:45 PMraulraja
11/25/2019, 7:45 PMraulraja
11/25/2019, 7:46 PM