Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a traverseWithKey

  • Loading branch information...
commit 401529b6011a08dcc81efe0bc41296028d0e082b 1 parent 46dd4c5
@batterseapower authored
Showing with 12 additions and 1 deletion.
  1. +3 −1 Data/Graph/Wrapper.hs
  2. +9 −0 Data/Graph/Wrapper/Internal.hs
View
4 Data/Graph/Wrapper.hs
@@ -27,7 +27,9 @@ module Data.Graph.Wrapper (
topologicalSort, depthNumbering,
- SCC(..), stronglyConnectedComponents, sccGraph
+ SCC(..), stronglyConnectedComponents, sccGraph,
+
+ traverseWithKey
) where
import Data.Graph.Wrapper.Internal
View
9 Data/Graph/Wrapper/Internal.hs
@@ -5,6 +5,8 @@
{-# OPTIONS_HADDOCK not-home #-}
module Data.Graph.Wrapper.Internal where
+import Control.Applicative (Applicative)
+
import Data.Array
import Data.Maybe (fromMaybe)
import qualified Data.Graph as G
@@ -41,6 +43,13 @@ instance Traversable.Traversable (Graph i) where
traverse f g = fmap (\gvva -> g { gVertexVertexArray = gvva }) (Traversable.traverse f (gVertexVertexArray g))
+traverseWithKey :: Applicative t => (i -> a -> t b) -> Graph i a -> t (Graph i b)
+traverseWithKey f g = fmap (\gvva -> g { gVertexVertexArray = gvva }) (traverseWithIndex (\gv -> f (gVertexIndex g gv)) (gVertexVertexArray g))
+ where
+ traverseWithIndex :: Applicative t => (G.Vertex -> a -> t b) -> Array G.Vertex a -> t (Array G.Vertex b)
+ traverseWithIndex f a = fmap (array (bounds a)) $ flip Traversable.traverse (assocs a) $ \(k, v) -> fmap ((,) k) $ f k v
+
+
{-# RULES "indexGVertex/gVertexIndex" forall g i. gVertexIndex g (indexGVertex g i) = i #-}
{-# RULES "gVertexIndex/indexGVertex" forall g v. indexGVertex g (gVertexIndex g v) = v #-}
Please sign in to comment.
Something went wrong with that request. Please try again.