Hi all, I have a general question about structurin...
# arrow
t
Hi all, I have a general question about structuring my app/State when trying to be more functional. At its core, I have a data class that represents state and I'm using a redux like setup. So when a request comes in, I follow this pattern:
Copy code
data class State(val foo: Int = 1)
typealias NextState = State

// ...
val action = Action.Ping()
val next: NextState = state
  .dispatch(action)
  .save()

// Now maybe i'll grab a value off of next to send back to the user if that makes sense in the current context
//...
So this approach works fine, but I'm finding an increasing number of use cases where I want to return NextState and a Result. And I'd appreciate any feedback on what others think is a good way to handle this or a call out if my approach is bonkers ... still new to FP 🙂 I'm considering two approaches to achieve the above: •
fun <T> dispatch(action: Action): Either<Failure, Tuple2<NextState, T>>
•
fun dispatch(action: Action): Either<Failure, NextState>
In this approach I modify State to have non-serialised var that holds the return value:
data class State(val foo: Int = 1, var result: Option<Any> = None) { fun <T> getResult(): Option<T> }
Is there a better way of doing this, is there a preferred approach, is this sound? Any thoughts welcome.
p
t
I did when i started my FP journey + Arrow a few months back but it went right over my head then 🙂
Does it solve this type of problem? Edit: ah yes I can see just from the intro it does exactly this 😂
p
you will find an example of use here : https://www.47deg.com/blog/conway-kotlin/
I haven't tried it by my own but it sounds very interesting :)
t
brilliant i'll take a detailed look
any thoughts on the redux flow?
i don't see it mentioned very often in the kotlin world, but i do quite like it
p
I started to dev with kotlin 4 months ago, I wouln'd pretend to juge your flow. 🙂
t
I started with Kotlin 7 months ago .. so the blind leading the blind? 😂
p
anyway working this with a State object seems a valid FP approch. On the other hand I would not rely on State methods to compute a next state, I would prefer some utily classes, one per action type
something as UseCases in CLEAN architecture (https://proandroiddev.com/kotlin-clean-architecture-1ad42fcd97fa)
mostly inspired by that
Within the redux flow, state modifications are done by reducers/slices where each reducer/slice is responsible for updating different elements of state
so basically each action is handled by a specific reducer
s
The Redux flow is effectively a Moore Machine (a specialised representation of a finite state machine), which Arrow actually already has a data type for: https://arrow-kt.io/docs/arrow/ui/moore/index.html
Which essentially amounts to:
Copy code
data class Moore<Input, State>(val state: State, val handler: (Input) -> State)
This implies the handlers are free from effects though.
There is also a Mealy Machine, which allows for effects though I don't believe there is a representation of it in Arrow.
t
Oh thats very interesting i'll have to check it out
thanks for pointing it out