glance/app/Util.hs

57 lines
1.7 KiB
Haskell
Raw Normal View History

2016-01-23 05:29:46 +03:00
{-# LANGUAGE NoMonomorphismRestriction, FlexibleContexts, TypeFamilies #-}
module Util (
portToPort,
iconToPort,
iconToIcon,
iconToIconEnds,
--iconHeadToPort,
2016-01-23 05:29:46 +03:00
iconTailToPort,
toNames,
noEnds,
nameAndPort,
justName
2016-01-23 05:29:46 +03:00
)where
import Control.Arrow(first)
import Diagrams.Prelude(IsName, toName, Name)
import Types(EdgeEnd(..), Edge(..), NameAndPort(..))
2016-01-23 05:29:46 +03:00
mapFst :: (a -> b) -> [(a, c)] -> [(b, c)]
mapFst f = map (first f)
toNames :: (IsName a) => [(a, b)] -> [(Name, b)]
toNames = mapFst toName
noEnds :: (EdgeEnd, EdgeEnd)
2016-01-23 05:29:46 +03:00
noEnds = (EndNone, EndNone)
nameAndPort :: IsName a => a -> Int -> NameAndPort
nameAndPort n p = NameAndPort (toName n) (Just p)
justName :: IsName a => a -> NameAndPort
justName n = NameAndPort (toName n) Nothing
2016-01-23 05:29:46 +03:00
-- Edge constructors --
portToPort :: (IsName a, IsName b) => a -> Int -> b -> Int -> Edge
portToPort a b c d = Edge (nameAndPort a b, nameAndPort c d) noEnds
2016-01-23 05:29:46 +03:00
iconToPort :: (IsName a, IsName b) => a -> b -> Int -> Edge
iconToPort a c d = Edge (justName a, nameAndPort c d) noEnds
2016-01-23 05:29:46 +03:00
iconToIcon :: (IsName a, IsName b) => a -> b -> Edge
iconToIcon a c = Edge (justName a, justName c) noEnds
2016-01-23 05:29:46 +03:00
-- If there are gaps between the arrow and the icon, try switching the first two arguments
-- with the last two arguments
iconToIconEnds :: (IsName a, IsName b) => a -> EdgeEnd -> b -> EdgeEnd -> Edge
iconToIconEnds a b c d = Edge (justName a, justName c) (b, d)
2016-01-23 05:29:46 +03:00
-- iconHeadToPort :: (IsName a, IsName b) => a -> EdgeEnd -> b -> Int -> Edge
-- iconHeadToPort a endHead c d = Edge (justName a, nameAndPort c d) (EndNone, endHead)
2016-01-23 05:29:46 +03:00
iconTailToPort :: (IsName a, IsName b) => a -> EdgeEnd -> b -> Int -> Edge
iconTailToPort a endTail c d = Edge (justName a, nameAndPort c d) (endTail, EndNone)