Consider I have a list. I want to iterate over it and map it's elements -- but the mapping might also require to change others than the element I'm currently iterating over.
Let's say I have a switch
(has a field of list of light id's it controls, and bool field if its switched on) and light
(has an id field, and a bool field if its shining) structure types defined. I want to update list by turning off all switches, and correspondingly turning off all lights related to them. E.g. I'd want to perform mapping, so that list like this
(list
(light 'd #t)
(switch '(c) #f)
(light 'a #t)
...
(switch '(a b) #t)
...
(light 'b #t)
(light 'c #f))
becomes
(list
(light 'd #t)
(switch '(c) #f)
(light 'a #f)
...
(switch '(a b) #f)
...
(light 'b #f)
(light 'c #f))
This is a trivial example, but in actual use the list could be big, the predicate of which exactly other elements should be changed not obvious, and the coupling might depend on the function -- i.e. light might be depending not from a switch, but from some other element in the different context.
What's the best approach in functional, immutable way?
I guess turning lights all off is bad example, as it makes it too trivial. I'm looking for a more general solution, where actions may be different across elements depending on their (and their controller's) data.
The initial and result items must be in a flat structure (though I'm not sure how many there are besides lists / vectors)
The solution shouldn't assume that elements are easy to categorize (e.g. because categorization wildly varies depending on the context the same data is used)