I have some data defined as
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
module Test where
import Control.Lens
import Data.MonoTraversable
data Custom = Custom { _a :: Maybe Float
, _b :: Maybe Float
}
makeLenses ''Custom
type instance Element Custom = Maybe Float
instance MonoFunctor Custom where
omap f (Custom ja jb) = Custom (f ja) (f jb)
data MainI = MainI { _name :: Maybe String
, _dTypes :: Maybe Custom
}
makeLenses ''MainI
I have a function defined like this
addEm :: MainI -> MainI -> MainI
addEm toGet toSet = toSet & dTypes %~ (\x -> emFunctions <$> x <*> baseTypes)
where baseTypes =
(toGet & (dTypes . _Just) %~ (omap (\x -> pure (* 1.5) <*> x))) ^. dTypes
which just takes each value inside Custom
, adds 1.5
times the values in toGet
into the values in toSet
.
It does that with this helper function
emFunctions :: Custom -> Custom -> Custom
emFunctions toGetC toSetC = toSetC
& a %~ (\x -> (+) <$> x <*> toGetC ^. a)
& b %~ (\x -> (+) <$> x <*> toGetC ^. b)
So I was wondering if there's a better way of doing this?
Particularly doing it without the helper function.
Also a related question: Would it be possible to sum up the Custom
data? As in if _a == 5.5
and _b == 5.0
the sumCustom
function would return 10.5