Hi all! I'm a newcomer of the functional programmi...
# arrow
h
Hi all! I'm a newcomer of the functional programming in general; after a 3-days applied FP course (in scala) I've learned the basics of functional programming; after the course I switched to Kotlin+Arrow since I already knew the language quite well. Currently I'm facing some problems when approaching to a further steps in my understanding; I'm trying to better understand the monad transformers topic, and I've found online this little example: https://underscore.io/blog/posts/2014/07/27/readerwriterstate.html I'm struggling to write it in arrow since I can't understand how to use the ReaderT/WriterT transformers in order to achieve the same result described in the post. Do you have any similar example but done with Kotlin+Arrow?
K 1
l
By the way: Welcome to the FP world!
👍 1
h
Thank you! I'll take a look at it!
I'm trying to understand how that solution works, but I can't find the complete source code and the snippets in the post are incomplete; especially, I can't understand the definition of the AsyncResult data class described there...
r
that is old stuff, that post has not been updated.
you can compose individually Reader, State and Writer and there are also MonadReader, MonadState and MonadWriter in the arrow-mtl module which avoid nesting alltogether
Unfortunately those are not yet documented beyond the tests and laws we have for them
h
Thank you @raulraja, I've rewritten with Arrow the code of the Scala example, but I'm not sure it's the right way... You can find it here: https://gist.github.com/colomboe/6e7d9c0923f6e348edd0e5b2bc488b40
l
If you're not using
bind()
inside your comprehension, you don't need it =]
You could try to use polymorphic code in you example. Like:
Copy code
fun <F, A> find(key: Key, monad: Monad<F>): Work<Kind<F, A>> {
    work { monad.just(Unit) })
}
then, just called it like this:
Copy code
val found = Database.find("foo", Option.monad())
so you don't have to always count on a Option, maybe you decide to use a Either, Try... Keep it just as a monad makes it more flexible
h
Thank you Leandro for your suggestion! I'll try to update the code with your suggestions. I tried to keep the code as similar as possible to the one in scala. I'm wondering if the usage of the ReaderT/WriterT is correct in this sample. Also because of the fix.value.fix.value on row 34... It seems a signal of something wrong...
l
I don't know much about the WriterT, so I can't judge if this part is correct or not.
But just use monad comprehensions (the
binding
part) when you need to flatten a code that has many `flatMap`'s
And, if you wanna get fancy, don't use concrete Wrappers, use just
Kind
, to use can make a more generic code. You can take a look here: https://arrow-kt.io/docs/patterns/polymorphic_programs/ (It is a bit hard to understand at first, but it is a great concept)
h
Yes, I'm aware of the use of
Kind
and higher-kinded types; as I stated, I tried to make the code more similar to the orginal example in scala in order to keep it focused on the reader/writer data types.