Implement custom at and ix for NewInt types

This commit is contained in:
2025-08-25 00:50:41 +01:00
parent d776c91cfd
commit 25e64d5378
2 changed files with 17 additions and 13 deletions
+7 -8
View File
@@ -1,6 +1,4 @@
module Dodge.AssignHotkey ( module Dodge.AssignHotkey (assignHotkey) where
assignHotkey,
) where
import Dodge.Data.Equipment.Misc import Dodge.Data.Equipment.Misc
import Control.Lens import Control.Lens
@@ -11,11 +9,12 @@ import NewInt
-- it is not obvious to me whether hotkeys should belong to LWorld, CWorld or -- it is not obvious to me whether hotkeys should belong to LWorld, CWorld or
-- World -- World
assignHotkey :: NewInt ItmInt -> Hotkey -> LWorld -> LWorld assignHotkey :: NewInt ItmInt -> Hotkey -> LWorld -> LWorld
assignHotkey (NInt itid) hk lw = lw assignHotkey i hk lw = lw
& handleoldposition & handleoldposition
& hotkeys . at hk ?~ NInt itid & hotkeys . at hk ?~ i
& imHotkeys . unNIntMap . at itid ?~ hk -- & imHotkeys . unNIntMap . at itid ?~ hk
& imHotkeys . at i ?~ hk
where where
handleoldposition = fromMaybe id $ do handleoldposition = fromMaybe id $ do
olditid <- lw ^? hotkeys . ix hk . unNInt oldi <- lw ^? hotkeys . ix hk
return $ imHotkeys . unNIntMap . at olditid .~ Nothing return $ imHotkeys . at oldi .~ Nothing
+10 -5
View File
@@ -4,6 +4,8 @@
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE TypeFamilies #-}
--{-# LANGUAGE StandaloneDeriving #-}
module NewInt where module NewInt where
--import qualified Data.IntSet as IS --import qualified Data.IntSet as IS
@@ -19,11 +21,14 @@ newtype NewIntMap a b = NIntMap { _unNIntMap :: IM.IntMap b}
deriving newtype (Monoid,Semigroup) deriving newtype (Monoid,Semigroup)
deriving (Eq,Ord,Show,Read) deriving (Eq,Ord,Show,Read)
--instance Ixed (NewIntMap (NewInt a)) where type instance Index (NewIntMap a b) = NewInt a
-- type Index (NewIntMap (NewInt a)) = NewInt a type instance IxValue (NewIntMap a b) = b
-- type Index (NewIntMap (NewInt a)) = NewInt a
-- ix :: (NewInt a) -> IndexedTraversal' (NewIntMap (NewInt a)) instance Ixed (NewIntMap a b) where
-- ix (NInt k) (NewIntMap m) = NIntMap <$> ix k m ix (NInt i) f (NIntMap m) = fmap NIntMap (ix i f m)
instance At (NewIntMap a b) where
at (NInt i) f (NIntMap m) = fmap NIntMap (at i f m)
makeLenses ''NewInt makeLenses ''NewInt
makeLenses ''NewIntMap makeLenses ''NewIntMap