Skip to content

Commit

Permalink
apply review suggestions regarding imap
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Nov 26, 2024
1 parent c2061c3 commit 587ad2b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
29 changes: 12 additions & 17 deletions src/swarm-topography/Swarm/Game/Scenario/Topography/Grid.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module Swarm.Game.Scenario.Topography.Grid (
Grid (..),
NonEmptyGrid (..),
gridToVec,
zipNumberedNE,
mapWithCoordsNE,
mapWithCoords,
allMembers,
Expand All @@ -14,12 +13,14 @@ module Swarm.Game.Scenario.Topography.Grid (
)
where

import Control.Lens.Indexed (FunctorWithIndex, imap)
import Data.Aeson (ToJSON (..))
import Data.Foldable qualified as F
import Data.List.NonEmpty (NonEmpty)
import Data.List.NonEmpty qualified as NE
import Data.Maybe (fromMaybe)
import Data.Semigroup
import Data.Tuple.Extra (both)
import Data.Vector qualified as V
import GHC.Generics (Generic)
import Swarm.Game.World.Coords
Expand All @@ -28,6 +29,11 @@ import Prelude hiding (zipWith)
newtype NonEmptyGrid c = NonEmptyGrid (NonEmpty (NonEmpty c))
deriving (Generic, Show, Eq, Functor, Foldable, Traversable, ToJSON)

instance FunctorWithIndex Coords NonEmptyGrid where
imap f (NonEmptyGrid g) =
NonEmptyGrid $
imap (\i -> imap (\j -> f (Coords $ both fromIntegral (i, j)))) g

data Grid c
= EmptyGrid
| Grid (NonEmptyGrid c)
Expand All @@ -51,25 +57,15 @@ mapRowsNE ::
NonEmptyGrid b
mapRowsNE f (NonEmptyGrid rows) = NonEmptyGrid $ f rows

allMembersNE :: NonEmptyGrid c -> NonEmpty c
allMembersNE (NonEmptyGrid g) = sconcat g

allMembers :: Grid a -> [a]
allMembers EmptyGrid = []
allMembers g = F.toList g

nonemptyCount :: (Integral i) => NonEmpty i
nonemptyCount = NE.iterate succ 0

zipNumberedNE ::
Integral i =>
(i -> a -> b) ->
NonEmpty a ->
NonEmpty b
zipNumberedNE f = NE.zipWith f nonemptyCount

mapWithCoordsNE :: (Coords -> a -> b) -> NonEmptyGrid a -> NonEmpty b
mapWithCoordsNE f (NonEmptyGrid g) =
sconcat $ NE.zipWith outer nonemptyCount g
where
outer i = zipNumberedNE $ \j -> f (Coords (i, j))
mapWithCoordsNE f = allMembersNE . imap f

mapWithCoords :: (Coords -> a -> b) -> Grid a -> [b]
mapWithCoords _ EmptyGrid = []
Expand All @@ -78,8 +74,7 @@ mapWithCoords f (Grid g) = NE.toList $ mapWithCoordsNE f g
-- | Converts linked lists to vectors to facilitate
-- random access when assembling the image
gridToVec :: Grid a -> V.Vector (V.Vector a)
gridToVec EmptyGrid = V.empty
gridToVec (Grid (NonEmptyGrid g)) = V.fromList . map (V.fromList . NE.toList) $ NE.toList g
gridToVec g = V.fromList . map V.fromList $ getRows g

instance (ToJSON a) => ToJSON (Grid a) where
toJSON EmptyGrid = toJSON ([] :: [a])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Swarm.Game.Scenario.Topography.Structure.Recognition.Prep (
) where

import Control.Arrow ((&&&))
import Control.Lens.Indexed (imap)
import Data.HashMap.Strict qualified as HM
import Data.HashSet qualified as HS
import Data.Hashable (Hashable)
Expand All @@ -31,7 +32,7 @@ allStructureRows :: [StructureWithGrid b a] -> [StructureRow b a]
allStructureRows =
concatMap $ NE.toList . transformRows
where
transformRows g = zipNumberedNE (StructureRow g) rows
transformRows g = imap (StructureRow g . fromIntegral) rows
where
NonEmptyGrid rows = entityGrid g

Expand Down Expand Up @@ -150,7 +151,7 @@ explodeRowEntities annotatedRow@(ConsolidatedRowReferences rowMembers _ width) =
map swap
. catMaybes
. NE.toList
$ zipNumberedNE (\idx -> fmap (PositionWithinRow idx annotatedRow,)) rowMembers
$ imap (\idx -> fmap (PositionWithinRow (fromIntegral idx) annotatedRow,)) rowMembers

deriveEntityOffsets :: PositionWithinRow b a -> InspectionOffsets
deriveEntityOffsets (PositionWithinRow pos _) = mkOffsets pos width
Expand Down

0 comments on commit 587ad2b

Please sign in to comment.