Advertisement
Guest User

Untitled

a guest
Sep 6th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Mesh where
  2.  
  3. import Data.Map (Map)
  4. import qualified Data.Map as Map
  5.  
  6. import Control.Applicative
  7.  
  8. import Control.Monad
  9. import Control.Monad.Trans
  10. import Control.Monad.Trans.Maybe
  11.  
  12. import Control.Monad.State
  13.  
  14. data Mesh a = Mesh (Map Int (Cell a)) deriving (Eq,Show)
  15.  
  16. data Cell a = Cell a [Int] | Border a Int deriving (Eq,Show)
  17.  
  18. mesh = Mesh $ Map.fromList [(0,(Cell 'a' [1,2,3])),(1,(Border 'b' 0)),(2,(Border 'c' 0)),(3,(Border 'd' 0))]
  19.  
  20. cell (Mesh buf) id = Map.lookup id buf
  21.  
  22. neighbour mesh _   (Border _ _) = Nothing
  23. neighbour mesh idx (Cell _ ids) = join $ (cell mesh) <$> safeIndex idx ids
  24.     where safeIndex idx ids
  25.             | idx < 0 = Nothing
  26.             | idx < length ids = Just $ ids!!idx
  27.             | otherwise = Nothing
  28.  
  29. owner mesh (Cell _ _)    = Nothing
  30. owner mesh (Border _ id) = cell mesh id
  31.            
  32. type MMesh a b = MaybeT (State (Mesh a)) b
  33.  
  34. getMesh = get :: MMesh a (Mesh a)
  35.  
  36. cellM id = getMesh >>= (MaybeT . return) <$> (\m -> cell m id)
  37.  
  38. neighbourM idx cell = getMesh >>= (MaybeT . return) <$> (\m -> neighbour m idx cell)
  39.  
  40. ownerM cell = getMesh >>= (MaybeT . return) <$> (\m -> owner m cell)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement