Keep in mind that you have Nel states (Nel means NonEmptyList to make things shorter) and you want to combine states with one state using some function f for the left side of the state and g for the right side of the state.
So you want something like this:
def foldStatesG[S, A](in: NonEmptyList[State[S, A]])(f: (A, A) => A)(g: (S, S) => S): State[S, A] = {
in.foldLeft1((s1, s2) => State(e => {
val ss1 = s1(e)
val ss2 = s2(e)
g(ss1._1, ss2._1) -> f(ss1._2, ss2._2)
}))
}
I am sure that I am inventing a bicycle, and such a thing already exists, perhaps in a more general way. But, looking through the crag, I could not find or recognize him. I would be grateful for any help on this topic.
The second question describes how I came to such a problem. I wanted to do a little simulation when you have an Enemy (just consider him Double) and all possible spells that can hit him Nel [Spell] . So basically I want to generate all possible sequences. For example, if Nel [Spell] = ($, #), then given enemy E, the progression will look like
E -> (), then Nel(E -> $, E ->
Without going into details, I need something like this:
def combineS(variations: NonEmptyList[Spell]): State[NonEmptyList[(Enemy, List[Spell])], Boolean]
In other words, you provide it with all possible moves and simulate all possible states. You can consider it as a kind of decision tree that departs at every step. Therefore, I determined how to act with one spell.
def combineS1(spell: Spell): State[(NonEmptyList[(Enemy, List[Spell])]), Boolean]
// some logic
The problem is that I cannot implement it with a simple move:
def combineS(variations: NonEmptyList[Spell]): State[NonEmptyList[(Enemy, List[Spell])], Boolean] = {
val x = variations traverse combine1 // State[Nel[..], Nel[Boolean]
x map { _.suml1(conjunction)}
}
, .
:
def combineS(variations: NonEmptyList[Spell]): State[NonEmptyList[(Enemy, List[Spell])], AllDead] = {
val x: NonEmptyList[State[NonEmptyList[(Enemy, List[Spell])], Boolean]] = variations map combineS
foldStates(x)(_ && _)(append)
}
, , foldState.
( , ). , , , .
( , ?)
, stackoverflow , , , ?