Multiple changes
In the course of using this module, I made several changes. I'm not certain they're all universally appealing. How would you like the pull request to happen? Just a single pull request with commits in decreasing order of appeal? Post a summary of the changes, then pick out which to keep, and make a pull request on that basis?
Yes, could you please post a list of changes to start with?
- Added weight to
Edge(i.e.data Edge k e = Edge k e k) - Hid
Graphbehind smart constructor verifying that edges reference actually existing vertices and that there aren't duplicate vertices - Added
eqandshowinstances forEdgeandGraph - Added
vertex' :: forall k e v. (Eq k) => (v -> k) -> k -> Graph k e v -> v - Added
inEdges :: forall k e v. (Eq k) => k -> Graph k e v -> [Edge k e](andoutEdges) - Added
bimap' :: forall e f k i v w. (w -> i) -> (k -> v) -> (e -> f) -> (v -> w) -> Graph k e v -> Graph i f w(andbimap,map'andmap) - Added
alter :: forall k e v. (Ord k) => (v -> k) -> (v -> v) -> k -> Graph k e v -> Graph k e v
@paf31 bump
Sorry for the delay. Let's maybe start with the simple ones, like Eq and Show instances, and smart constructors. Then let's look at inEdges and outEdges.
How are weighted edges used?
No problem. Start with as in paste here?
Eq and Show
instance eqEdge :: (Eq k, Eq e) => Eq (Edge k e) where
(==) (Edge fa ea ta) (Edge fb eb tb) = fa == fb && ea == eb && ta == tb
(/=) a b = not $ a == b
instance showEdge :: (Show k, Show e) => Show (Edge k e) where
show (Edge from e to) =
"Edge (" <> show from <> ") (" <> show e <> ") (" <> show to <> ")"
instance eqGraph :: (Eq k, Eq e, Eq v) => Eq (Graph k e v) where
(==) (Graph vsa esa) (Graph vsb esb) = vsa == vsb && esa == esb
(/=) a b = not $ a == b
instance showGraph :: (Show k, Show e, Show v) => Show (Graph k e v) where
show (Graph vs es) = "Graph (" <> show vs <> ") (" <> show es <> ")"
Smart constructor
graph :: forall k e v. (Ord k) => (v -> k) -> [v] -> [Edge k e] -> Maybe (Graph k e v)
graph f vs es =
if S.isEmpty (es' `S.difference` vs') && A.length (S.toList vs') == A.length vs
then Just $ Graph vs es
else Nothing where
vs' = S.fromList $ f <$> vs
es' = S.fromList $ A.concatMap (\(Edge from _ to) -> [from, to]) es
inEdges and outEdges
inEdges :: forall k e v. (Eq k) => k -> Graph k e v -> [Edge k e]
inEdges k (Graph _ es) = A.filter (\(Edge _ _ k) -> k == k) es
outEdges :: forall k e v. (Eq k) => k -> Graph k e v -> [Edge k e]
outEdges k (Graph _ es) = A.filter (\(Edge k _ _) -> k == k) es
Weighted edges
I actually ended up not using graphs. But weights seem pretty useful for shortest path, minimum spanning tree, etc.
I think this issue should be split into separate issues and discussed separately.