diff options
author | Griffin Smith <grfn@gws.fyi> | 2020-07-04T00·32-0400 |
---|---|---|
committer | glittershark <grfn@gws.fyi> | 2020-07-04T15·30+0000 |
commit | 9b8d3185fe6cee9231ed20a1dbf0240d0c459a39 (patch) | |
tree | 29b8f78a81500043df1fa8ca289bdb2a35dc68ff /users/glittershark/xanthous/src/Xanthous/Data.hs | |
parent | 4455f28e426f49c2e3b8ef08961e5073a11a5b4f (diff) |
refactor(xan): Switch to V2 over tuples most places r/1207
These are generally rather nicer to work due to some typeclass instances, and integrate better with other ecosystems for things like linear algebra etc. Change-Id: I546c8da7b17234648f3d612b28741c1fded25447 Reviewed-on: https://cl.tvl.fyi/c/depot/+/910 Tested-by: BuildkiteCI Reviewed-by: glittershark <grfn@gws.fyi>
Diffstat (limited to 'users/glittershark/xanthous/src/Xanthous/Data.hs')
-rw-r--r-- | users/glittershark/xanthous/src/Xanthous/Data.hs | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/users/glittershark/xanthous/src/Xanthous/Data.hs b/users/glittershark/xanthous/src/Xanthous/Data.hs index e2967d274fd5..c9c11b553b67 100644 --- a/users/glittershark/xanthous/src/Xanthous/Data.hs +++ b/users/glittershark/xanthous/src/Xanthous/Data.hs @@ -28,6 +28,7 @@ module Xanthous.Data , loc , _Position , positionFromPair + , positionFromV2 , addPositions , diffPositions , stepTowards @@ -176,7 +177,7 @@ instance Num a => Group (Position' a) where -- | Positions convert to scalars by discarding their orientation and just -- measuring the length from the origin instance (Ord a, Num a, Scalar a) => Scalar (Position' a) where - scalar = fromIntegral . length . line (0, 0) . view _Position + scalar = fromIntegral . length . line 0 . view _Position fromScalar n = Position (fromScalar n) (fromScalar n) data Positioned a where @@ -220,15 +221,18 @@ loc = iso hither yon hither (Position px py) = Location (px, py) yon (Location (lx, ly)) = Position lx ly -_Position :: Iso' (Position' a) (a, a) +_Position :: Iso' (Position' a) (V2 a) _Position = iso hither yon where - hither (Position px py) = (px, py) - yon (lx, ly) = Position lx ly + hither (Position px py) = (V2 px py) + yon (V2 lx ly) = Position lx ly positionFromPair :: (Num a, Integral i, Integral j) => (i, j) -> Position' a positionFromPair (i, j) = Position (fromIntegral i) (fromIntegral j) +positionFromV2 :: (Num a, Integral i) => V2 i -> Position' a +positionFromV2 (V2 xx yy) = Position (fromIntegral xx) (fromIntegral yy) + -- | Add two positions -- -- Operation for the additive group on positions @@ -448,13 +452,13 @@ neighborDirections = Neighbors neighborPositions :: Num a => Position' a -> Neighbors (Position' a) neighborPositions pos = (`move` pos) <$> neighborDirections -neighborCells :: Num a => (a, a) -> Neighbors (a, a) +neighborCells :: Num a => V2 a -> Neighbors (V2 a) neighborCells = map (view _Position) . neighborPositions . review _Position arrayNeighbors :: (IArray a e, Ix i, Num i) - => a (i, i) e - -> (i, i) + => a (V2 i) e + -> V2 i -> Neighbors (Maybe e) arrayNeighbors arr center = arrLookup <$> neighborPositions (_Position # center) where |