I'm working on some functions that take a record and return a slightly modified record.
For example
import Control.Lens ((%~), (^.), (&))
modifyRecord :: SomeRecord -> SomeRecord -> SomeRecord
modifyRecord baseR currentR = currentR & thisPart %~ (fmap (someFn someValue))
where someValue = baseR ^. thisPart
The function modifyRecord
takes two arguments, both of the same type.
currentR
is the current state of the record
and
baseR
is the base state of the record
(i.e. No functions applied, never changed)
Composing several functions of this type means I'll have to compose partial functions, make a list of them
[fn1 baseState , fn2 baseState , fn3 baseState ... fnn baseState]
and then I'd fold over currentState
with function like foldl (flip ($))
so each fnn baseState
is a function in itself with type
SomeRecord -> SomeRecord
What I want to do is write those functions such that they only take the current state of the record and figure out base state on their own.
So
modifyRecord :: SomeRecord -> SomeRecord -> SomeRecord
to
modifyRecord :: SomeRecord -> SomeRecord
without actually modifying the record itself.
I want to avoid doing this
data SomeRecord = SomeRecord { value1 :: Float
, value1Base :: Float
, value2 :: Float
, value2Base :: Float
...
...
, valueN :: Float
, valueNBase :: Float
}
where the record itself would hold base values and function applied on it will avoid interacting with *Base
items.
Would that be possible?