Hi Folks! I hope you are all keeping well in these...
# arrow
m
Hi Folks! I hope you are all keeping well in these COVID times. I'm nearing the end of my journey with the FP in Kotlin book (just put finishing touches on chapter 14 - Local Effects 🎉) but am in need of some help and advice from the Arrow masters. 😄 For those who haven't read the Scala red book, this chapter deals with a datatype called
ST
, short for State Token, which is a type that encapsulates local memory mutation and isolates the outside world from it.
ST
is also a
Monad
, and works very similar to the
State
monad. In my code samples while working with
ST
, I am ending up with gnarly nested
flatMap
and
map
sequences that seem so redundant and difficult to interpret for the reader. I would love to replace these nested monstrosities with neat for comprehension
fx
blocks, and I have been digging into the arrow source code to get some tips about how to achieve this. The problem is that adding
fx
binding capabilities to my own data type seems way complicated. Is there an easy way to achieve this? (or at least a guide or tutorial that instructs how to go about equipping your own data types with
fx
binding)? Hoping that someone can advise or shed some light on this. I really appreciate any help you can provide.
👍 1
j
That has me interested: How does scala implement
ST
? Haskell uses a universally quantified type variable as a
Token
which moves this entirely to the type level, so dunno how different scala here is.
r
@marc0der should be possible to add simply suspended bind to that data type with arrow-continuations as we are now doing computation blocks. If you have some code for the data type we can provide the relevant code to do binding over it
m
Thanks @raulraja, that would be super helpful. I'll post a gist to my code samples tomorrow when I have a moment.
👍 1
r
Hi Marco. It would look something like the code below. I have made changes to make your datatype be more inline and based on suspend because otherwise it’s not useful in Kotlin. Given all those flatMap etc are not inline anyone running suspension inside would not be able to use it alongside the pure computations. https://gist.github.com/raulraja/e8bba66133f061ca07e5578c48264915
Unfortunately there seems to be a bug in arrow continuations shift impl or I did something wrong but the comprehension version gets lost and spins forever. / cc @Jannis may know more
m
@raulraja sorry for not giving you more context. I've updated the gist to show you how the entire chain is invoked from a
main()
function. Also worth noting that the entire algebra is run from the
runST
interpreter that lives in the
ST
companion object. We shouldn't need to use any
suspend
, as this monad doesn't deal with any long running blocking calls. It is only used for segregating local effects like reference mutation from pure code. Hope it makes more sense now!
@raulraja quick update, I managed to get it working. I was merely missing some boilerplate: https://gist.github.com/marc0der/d1b89b6077639fdd6ffd6df7b9b3855a#gistcomment-3528446
As always, thanks so much for your willingness to help!
👏 1
r
awesome!