Created
April 23, 2020 21:04
-
-
Save aavogt/c44a162bbceb9040e425acd6a9ff152e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE StandaloneDeriving, FlexibleContexts, UndecidableInstances #-} | |
-- zippers is probably a better answer | |
import Control.Lens | |
import Data.Maybe | |
-- with index | |
data WI m = WI (Index m) m | |
deriving instance (Show m, Show (Index m)) => Show (WI m) | |
mkwi' :: Ixed m => Index m -> m -> WI m | |
mkwi' i m = fromJust (mkwi i m) | |
mkwi :: Ixed m => Index m -> m -> Maybe (WI m) | |
mkwi i m | has (ix i) m = Just (WI i m) | |
mkwi _ _ = Nothing | |
-- with index element | |
wie :: Ixed m => Lens' (WI m) (IxValue m) | |
wie f (WI i m) = WI i <$> unsafeSingular (ix i) f m | |
-- with index index, though if mkwi | |
wii f (Just (WI i m)) = flip mkwi m <$> f i | |
d = mkwi' 1 "abc" | |
main = do | |
d & print | |
d & wie .~ 'x' & print | |
let Just e = Just d & wii .~ 0 | |
e & wie .~ 'x' & print | |
{- | |
*Main> main | |
WI 1 "abc" | |
WI 1 "axc" | |
WI 0 "xbc" | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment