Reputation: 879
I need a function that updates a state multiple times depending on a list of values: for each value of the list it may possibly update the state or leave it unchanged. So I figured I'd need a function of a type like:
[a] -> b -> (a->b->b) -> b
Where [a] is a list of values for which goes that for each of them the state b may be updated (depending of the value of a). Then the resulting b is a new state that has all necessary updates applied.
However I could not find any function that on Hoogle that does this, so I figure I'd have to make one myself however I have no idea how I could do this. Are there any functions existing that I can use to accomplish such function?
If anyone could help me out on this it'd be much appreciated!
Best regards, Skyfe.
EDIT: A (simplified) example of what I could have & need:
[a] = [1, 5, 3, 6]
b = State{x, y, z}
f :: (a->b->b)
f a b = if someAlgorithm a then b{x=someFunc x, y=y+1} else b
=> Then the function I need should execute f on all a's and for each time it does so, it should return the new (updated or unchanged) b as argument for the new call to f along with the next element from the list [a], and so on untill it has done this for all elements and results into a final b with all updates applied.
Upvotes: 1
Views: 320
Reputation: 15693
What you want is foldr f b a
after applying filter someAlgorithm
on your list. If you don't know these functions, look them up on hoogle.
Upvotes: 2