I have a tuple of values representing some state, and want to translate it by an addition (shift). My values are a longer version of ( Int, [Int], Int), and I want something conceptually (but not literally) like this:
shift n = ??? (+n) (id, map, id) -- simple(?)
which would be equivalent to:
shift n (a, b, c) = (a+n, map (+n) b, c+n)
I am happy to just go with this explicit function usage, but wondered it there was a more idiomatic point-free version using Applicative or Arrows or ..., or if they would just end-up obfuscating things. I thought that the point-free version shows the basic structure of the operation more clearly.
With the DeriveFunctor
language extension you can write
data MyState a = MyState a [a] a
deriving (Functor)
The derived instance looks like
instance Functor MyState where
fmap f (MyState a bs c) = MyState (f a) (map f bs) (f c)
Now you can define
shift :: MyState Int -> MyState Int
shift n = fmap (+n)
(You say your tuple is even longer than (Int, [Int], Int)
, so you may want to define your state type using record syntax.)