Skip to content

Implement and test Foldable/Traversable instances #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
Breaking changes:

New features:
- Added `Foldable` and `Traversable` instances for `Graph` (#16 by @MaybeJustJames)

Bugfixes:

Expand Down
13 changes: 12 additions & 1 deletion src/Data/Graph.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import Prelude
import Data.Bifunctor (lmap)
import Data.CatList (CatList)
import Data.CatList as CL
import Data.Foldable (class Foldable)
import Data.Foldable (class Foldable, foldl, foldr, foldMap)
import Data.List (List(..))
import Data.List as L
import Data.Map (Map)
import Data.Map as M
import Data.Maybe (Maybe(..), maybe)
import Data.Tuple (Tuple(..), fst, snd)
import Data.Tuple.Nested ((/\))
import Data.Traversable (class Traversable, traverse)

-- | A graph with vertices of type `v`.
-- |
Expand All @@ -30,6 +32,15 @@ newtype Graph k v = Graph (Map k (Tuple v (List k)))
instance functorGraph :: Functor (Graph k) where
map f (Graph m) = Graph (map (lmap f) m)

instance foldableGraph :: Foldable (Graph k) where
foldl f z (Graph m) = foldl (\acc (Tuple k _) -> f acc k) z $ M.values m
foldr f z (Graph m) = foldr (\(Tuple k _) acc -> f k acc) z $ M.values m
foldMap f (Graph m) = foldMap (f <<< fst) $ M.values m

instance traversableGraph :: Traversable (Graph k) where
traverse f (Graph m) = Graph <$> (traverse (\(v /\ ks) -> (_ /\ ks) <$> (f v)) m)
sequence = traverse identity

-- | Unfold a `Graph` from a collection of keys and functions which label keys
-- | and specify out-edges.
unfoldGraph
Expand Down
12 changes: 10 additions & 2 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ import Prelude

import Effect (Effect, foreachE)
import Effect.Console (logShow)
import Data.Graph (unfoldGraph, topologicalSort)
import Data.List (toUnfoldable, range)
import Data.Foldable (foldr)
import Data.Maybe (Maybe(Just))
import Data.Traversable (traverse)
import Data.Graph (Graph, unfoldGraph, topologicalSort)
import Data.List (filter, toUnfoldable, range, (:), List(Nil))

main :: Effect Unit
main = do
let double x | x * 2 < 100000 = [x * 2]
| otherwise = []
graph :: Graph Int Int
graph = unfoldGraph (range 1 100000) identity double
foreachE (toUnfoldable (topologicalSort graph)) logShow
logShow
$ filter (_ /= 0) <<< foldr (:) Nil
<$> traverse (\n -> if n `mod` 2 == 0 then Just 0 else Just n) graph