Fred Friis
03/25/2021, 3:00 AMsimon.vergauwen
03/25/2021, 7:59 AMList#map
, but this is arguably an FP operator.
Same with suspend
, Kotlin brings it into the language as a feature so it becomes almost transparent to the user to use it within an OOP-styled project but Continuation
and IO
are also FP constructs.
In this regard, Kotlin does a great effort to bring a lot of functional patterns into the language. sealed class
, data class
, when
, if as a statement
, etc are a couple of other things common in functional languages.
So as you can see the line between these two is rather thin, and I'm a bit unsure what you mean with your question: "Like yes you can construct computations and map and filter and chain etc them in weird and wonderful ways but what's the practical actual use?"
I'd say this is unrelated to FP, and using map
& filter
is just a way to write much clean code without having to write so much boilerplate.
But to answer your final question:
But I'd REALLY struggle to make any case at all for just coding vs creating an object of code and then calling .runOne of the ideas behind using
IO
or suspend
which I also already mentioned before is that you cannot call IO
or suspend
from regular code.
Or in other words, if you mark all your side-effecting code with suspend
the compiler doesn't allow to call it from non-suspend
code. So the compiler can prevent you from making a mistake here.
This allows you to easily split up your code is easy to test pure code with pure function. i.e. map: (UserDto) -> User
and side-effecting code fetch: suspend (id: Int) -> UserDto
.
The point of calling run
is to bridge both worlds with each other. It could also be defined as operator fun <A> (suspend () -> A). invoke(): A
or operator fun <A> IO<A>.invoke(): A
but that defeats the purpose of being explicit.simon.vergauwen
03/25/2021, 8:17 AMstojan
03/25/2021, 10:01 AMraulraja
03/25/2021, 12:17 PMJörg Winter
03/26/2021, 9:05 AM