Add lenses.hs
This commit is contained in:
commit
673371b905
1 changed files with 71 additions and 0 deletions
71
lenses.hs
Normal file
71
lenses.hs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
|
||||
{-
|
||||
|
||||
here i show how to use lenses to handle states (didnt found any example on states in internet)
|
||||
|
||||
-}
|
||||
|
||||
|
||||
{-# LANGUAGE TemplateHaskell #-}
|
||||
import Lens.Micro.TH (makeLenses)
|
||||
import Lens.Micro ((%~), (.~), (&), _2)
|
||||
import Lens.Micro.Platform (at) -- Correct import for Map operations
|
||||
import Data.IORef (IORef, newIORef, modifyIORef', readIORef)
|
||||
import Data.Map (Map)
|
||||
import qualified Data.Map as Map
|
||||
|
||||
|
||||
data AppState = AppState
|
||||
{ _ui :: UIState
|
||||
, _user :: User
|
||||
} deriving (Show)
|
||||
|
||||
data UIState = UIState
|
||||
{ _theme :: String
|
||||
, _windowSize :: (Int, Int)
|
||||
, _keyBindings :: Map String String
|
||||
} deriving (Show)
|
||||
|
||||
data User = User
|
||||
{ _name :: String
|
||||
, _prefs :: UserPrefs
|
||||
} deriving (Show)
|
||||
|
||||
data UserPrefs = UserPrefs
|
||||
{ _fontSize :: Int
|
||||
, _darkMode :: Bool
|
||||
} deriving (Show)
|
||||
|
||||
|
||||
makeLenses ''AppState
|
||||
makeLenses ''UIState
|
||||
makeLenses ''User
|
||||
makeLenses ''UserPrefs
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
let initialState = AppState
|
||||
{ _ui = UIState
|
||||
{ _theme = "light"
|
||||
, _windowSize = (800, 600)
|
||||
, _keyBindings = Map.fromList [("Ctrl+S", "save")]
|
||||
}
|
||||
, _user = User
|
||||
{ _name = "Alice"
|
||||
, _prefs = UserPrefs
|
||||
{ _fontSize = 12
|
||||
, _darkMode = False
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stateRef <- newIORef initialState
|
||||
|
||||
-- Corrected lens chain with &
|
||||
modifyIORef' stateRef $ \s ->
|
||||
s & ui . keyBindings . at "Ctrl+X" .~ Just "cut"
|
||||
& user . prefs . darkMode .~ True
|
||||
& ui . windowSize . _2 %~ (+100)
|
||||
|
||||
finalState <- readIORef stateRef
|
||||
print finalState
|
||||
Loading…
Reference in a new issue