[GUI] get and set
Krasimir Angelov
ka2_mail@yahoo.com
Sun, 16 Feb 2003 15:55:17 -0800 (PST)
--0-1662008171-1045439717=:78241
Content-Type: text/plain; charset=us-ascii
Content-Id:
Content-Disposition: inline
The getter and setter abstractions are really more
universal concept than Attr in GIO and StateVariable
in HOpenGL. The get and set functions are not limited
to IO monad and can be implemented for many others
monad types. Unfortunately proper implementation
requires functional dependencies. The attached module
contains definitions for get and set functions for IO,
ST, Reader, ReaderT, State, StateT, RWS and RWST
monads. The idea is that each monad can maintains some
kind of state and the corresponding Attr type gives
access to some part of the state. The good example is
the StateAttr with State monad.
Example:
data Point = Point {x :: Int, y :: Int}
attrX :: StateAttr Point Int
attrX = StateAttr x (\v p -> p{x=v})
attrY :: StateAttr Point Int
attrY = StateAttr y (\v p -> p{y=v})
move :: Int -> Int -> State Point Int
move vx vy = do
x <- get attrX
attrX =: x+vx
y <- get attrY
attrY =: y+vy
I propose to standardize this concept and add Attr
module to the GHC base package.
Cheers,
Krasimir
__________________________________________________
Do you Yahoo!?
Yahoo! Shopping - Send Flowers for Valentine's Day
http://shopping.yahoo.com
--0-1662008171-1045439717=:78241
Content-Type: application/octet-stream; name="Attr.hs"
Content-Transfer-Encoding: base64
Content-Description: Attr.hs
Content-Disposition: attachment; filename="Attr.hs"
bW9kdWxlIEF0dHIgd2hlcmUNCg0KaW1wb3J0IERhdGEuSU9SZWYNCmltcG9y
dCBEYXRhLlNUUmVmDQppbXBvcnQgQ29udHJvbC5Nb25hZC5TVA0KaW1wb3J0
IENvbnRyb2wuTW9uYWQuUmVhZGVyDQppbXBvcnQgQ29udHJvbC5Nb25hZC5T
dGF0ZSBoaWRpbmcgKGdldCkNCmltcG9ydCBDb250cm9sLk1vbmFkLlJXUyBo
aWRpbmcgKGdldCkNCg0KY2xhc3MgUmVhZGFibGUgdiBtIHwgdiAtPiBtIHdo
ZXJlDQogICBnZXQgOjogdiBhIC0+IG0gYQ0KDQpjbGFzcyBXcml0YWJsZSB2
IG0gfC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