Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Mesh where
- import Data.Map (Map)
- import qualified Data.Map as Map
- import Control.Applicative
- import Control.Monad
- import Control.Monad.Trans
- import Control.Monad.Trans.Maybe
- import Control.Monad.State
- data Mesh a = Mesh (Map Int (Cell a)) deriving (Eq,Show)
- data Cell a = Cell a [Int] | Border a Int deriving (Eq,Show)
- mesh = Mesh $ Map.fromList [(0,(Cell 'a' [1,2,3])),(1,(Border 'b' 0)),(2,(Border 'c' 0)),(3,(Border 'd' 0))]
- cell (Mesh buf) id = Map.lookup id buf
- neighbour mesh _ (Border _ _) = Nothing
- neighbour mesh idx (Cell _ ids) = join $ (cell mesh) <$> safeIndex idx ids
- where safeIndex idx ids
- | idx < 0 = Nothing
- | idx < length ids = Just $ ids!!idx
- | otherwise = Nothing
- owner mesh (Cell _ _) = Nothing
- owner mesh (Border _ id) = cell mesh id
- type MMesh a b = MaybeT (State (Mesh a)) b
- getMesh = get :: MMesh a (Mesh a)
- cellM id = getMesh >>= (MaybeT . return) <$> (\m -> cell m id)
- neighbourM idx cell = getMesh >>= (MaybeT . return) <$> (\m -> neighbour m idx cell)
- ownerM cell = getMesh >>= (MaybeT . return) <$> (\m -> owner m cell)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement