Hello all, new to the channel. I’m working with t...
# arrow
r
Hello all, new to the channel. I’m working with the “Functional Programming in Kotlin” book by Manning. Though this book was just finalized in August, they appear to be using some deprecated Arrow library code in their code examples. Here’s an example from Chapter 6: Listing 6.18. State propagation using a for-comprehension
Copy code
val ns2: State<RNG, List<Int>> =
   State.fx(Id.monad()) { 
       val x: Int = int.bind() 
       val y: Int = int.bind() 
       val xs: List<Int> = ints(x).bind() 
       xs.map { it % y } 
   }
Using latest Arrow libs, the above snippet fails syntax checker in Idea. ‘Id’ is not recognized, and Arrow docs say it was slated for deprecation. Wondering if anyone can help refactor this to current Arrow version, or recommend previous version this will work with? Asked on Manning forum, but got no reply.
r
Hi @Randall Perry, The State and Id monad are no longer part of Arrow as of long ago. if you want to see similar examples you can find them in https://arrow-kt.io/docs/patterns/monad_comprehensions/ https://arrow-kt.io/docs/patterns/error_handling/ and https://arrow-kt.io/docs/patterns/monads/
🙌 1
We offer an different alternative to the state data type which is
Atomic
https://arrow-kt.io/docs/apidocs/arrow-fx-coroutines/arrow.fx.coroutines/-atomic/ you can thread an Atomic reference through any suspend computation as argument or keeping a scoped reference to it and use its api to update its state inside suspension
👀 1
r
Thanks, I’ll have a look and see if I can integrate this into their code examples.
r
if you have any more questions or want to bring any more examples happy to go over them and see how that would look like in Arrow. @marc0der also hangs out around here and he can maybe help clarify as we work through any of those.
m
Hi @Randall Perry, happy to help where I can. That chapter was written about a year and a half ago when the State monad was still a thing in Arrow, but the API seems to have changed a bit since then 😅 The inclusion of Arrow in that part of the book was mostly to demonstrate how the for-comprehension could work with get and set methods. It's not all that vital to the chapter as a whole. I'd also say that many other languages and libraries still us the State monad, so as a concept it remains perfectly valid from an academic perspective.
That said, we'll definitely try to incorporate that in subsequent releases of the book. And BTW, if you clone the book's repo from GitHub, it works perfectly in intellij.
r
Doh! I had cloned it when I started the book, but forgot about it. Thanks
👍 1