Skip to content

Instantly share code, notes, and snippets.

@iokasimov
Created February 24, 2017 17:34
Show Gist options
  • Save iokasimov/190819ca55c5f3afd3d646dfc1573f0f to your computer and use it in GitHub Desktop.
Save iokasimov/190819ca55c5f3afd3d646dfc1573f0f to your computer and use it in GitHub Desktop.
import qualified Line
import Data.Maybe
newtype Grid a = Grid { grid :: Line.Line (Line.Line a) } deriving Show
instance Functor Grid where
fmap f = Grid . (fmap . fmap) f . grid
extract :: Grid a -> a
extract = Line.extract . Line.extract . grid
left :: Grid a -> Maybe (Grid a)
left = fmap Grid . Line.left . grid
right :: Grid a -> Maybe (Grid a)
right = fmap Grid . Line.right . grid
up :: Grid a -> Maybe (Grid a)
up = fmap Grid . pull_maybe . fmap Line.left . grid
down :: Grid a -> Maybe (Grid a)
down = fmap Grid . pull_maybe . fmap Line.right . grid
pull_maybe :: Line.Line (Maybe a) -> Maybe (Line.Line a)
pull_maybe (Line.Line ls x rs) = check (distribute ls) x (distribute rs) where
check (Just _ls) (Just _x) (Just _rs) = Just $ Line.Line _ls _x _rs
check _ _ _ = Nothing
distribute :: [Maybe a] -> Maybe [a]
distribute [] = pure []
distribute (ax:axs) = (:) <$> ax <*> (distribute axs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment