haskell - How do I avoid referring to all state variables when updating only a few? -


an idiom use composing couple of procedures (with memory) follows:

p1 :: state (int, string) () p1 =     (a, b) <-     ... ...     put (a', b)  p2 :: state (int, string) () p2 =     (a, b) <-     ... else ...     put (a, b')  main =     ... initializing a0 b0 ...     print . flip evalstate (a0, b0)           . sequence $ replicate 10 p1 ++ repeat p2 

however, number of state variable grows, gets way more verbose necessary:

p1 :: state (int, string, bool, int, string, bool) () p1 =     (a, b, c, d, e, f) <-     ... ...     put (a, b, c', d, e, f')  p2 :: state (int, string, bool, int, string, bool) () p2 =     (a, b, c, d, e, f) <-     ... ...     put (a', b', c, d, e, f)  main =     print . flip evalstate (a0, b0, c0, d0, e0, f0)           . sequence $ replicate 10 p1 ++ repeat p2 

as wondering, there way of updating few state variables without having refer unused ones? thinking ioref state (in fact there package stateref), i'm not sure if there common idioms other people have been using.

this seems job lenses. control.lens.tuple module .= , use:

p1 =    <- use _1    -- --    _1 .= a' 

however, it's better if give things in state proper names, e.g.

{-# language templatehaskell #-  data record = mkrecord { _age :: int                        , _name :: string                        , _programmer :: bool                        } deriving (show, eq) makelenses ''record 

that way, have better names field:

p1 =    <- use age    -- --    age .= a' 

note still helps if don't want use lenses, since can use record syntax update data:

 p1 =       r <-       let = _age r       ---       put $ r{_age = a'} 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -