Skip to content

Instantly share code, notes, and snippets.

@yasuabe
Last active May 12, 2017 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasuabe/e8eb0af45c31984299acb2d1f2c37461 to your computer and use it in GitHub Desktop.
Save yasuabe/e8eb0af45c31984299acb2d1f2c37461 to your computer and use it in GitHub Desktop.
haskell implementation of Simpath: Edge
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