Last active
May 12, 2017 12:27
-
-
Save yasuabe/e8eb0af45c31984299acb2d1f2c37461 to your computer and use it in GitHub Desktop.
haskell implementation of Simpath: Edge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Simpath.Edge where | |
import Prelude hiding (either) | |
import Data.Function | |
import Data.Set (Set) | |
import Control.Applicative | |
import qualified Data.Set as Set | |
import qualified Data.Foldable as Foldable | |
type Node = Int | |
data Edge = Edge { left :: Node, right :: Node } deriving (Show, Eq, Ord) | |
edge :: Node -> Node -> Edge | |
edge = (liftA2 . liftA2) Edge min max | |
start :: Edge | |
start = edge 0 1 | |
onBoth :: (a -> a -> b) -> (Node -> a) -> Edge -> b | |
onBoth f g (Edge l r) = on f g l r | |
either:: (Node -> Bool) -> Edge -> Bool | |
either = onBoth (||) | |
contains :: Node -> Edge -> Bool | |
contains = either . (==) | |
modify :: (Node -> Node) -> Edge -> Edge | |
modify = onBoth edge | |
opposite :: Node -> Edge -> Node | |
opposite n (Edge l r) = if l == n then r else l | |
isOpen :: Edge -> Bool | |
isOpen = onBoth (/=) id | |
find :: Node -> Set Edge -> Maybe Edge | |
find = Foldable.find . contains | |
connect :: Node -> Node -> Edge -> Edge | |
connect from to = edge from . opposite to |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment