Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/elm-community/graph
Functional Graph Library in Elm.
https://github.com/elm-community/graph
Last synced: 3 months ago
JSON representation
Functional Graph Library in Elm.
- Host: GitHub
- URL: https://github.com/elm-community/graph
- Owner: elm-community
- License: mit
- Fork: true (sgraf812/elm-graph)
- Created: 2016-06-08T01:11:42.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-06-14T11:20:30.000Z (5 months ago)
- Last Synced: 2024-06-19T11:39:41.783Z (5 months ago)
- Language: Elm
- Homepage: http://package.elm-lang.org/packages/elm-community/graph/latest
- Size: 156 KB
- Stars: 60
- Watchers: 6
- Forks: 16
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Graph [![Build Status](https://travis-ci.org/elm-community/graph.svg)](https://travis-ci.org/elm-community/graph)
An neat graph library for Elm.Got confused about what to wear when putting on shoes? This will help you out:
```elm
dressUp : Graph String () -- node labels are strings, edge labels are empty
dressUp =
let
nodes =
[ Node 0 "Socks"
, Node 1 "Undershirt"
, Node 2 "Pants"
, Node 3 "Shoes"
, Node 4 "Watch"
, Node 5 "Shirt"
, Node 6 "Belt"
, Node 7 "Tie"
, Node 8 "Jacket"
]e from to =
Edge from to ()edges =
[ e 0 3 -- socks before shoes
, e 1 2 -- undershorts before pants
, e 1 3 -- undershorts before shoes
, e 2 3 -- pants before shoes
, e 2 6 -- pants before belt
, e 5 6 -- shirt before belt
, e 5 7 -- shirt before tie
, e 6 8 -- belt before jacket
, e 7 8 -- tie before jacket
]
in
Graph.fromNodesAndEdges nodes edgesiWantToWearShoes: List String
iWantToWearShoes =
Graph.guidedDfs
Graph.alongIncomingEdges -- which edges to follow
(Graph.onDiscovery (\ctx list -> -- append node labels on discovery
ctx.node.label :: list))
[3 {- "Shoes" NodeId -}] -- start with the node labelled "Shoes"
[] -- accumulate starting with the empty list
dressUp -- traverse our dressUp graph from above
|> Tuple.first -- ignores the untraversed rest of the graphiWantToWearShoes == ["Pants", "Undershorts", "Socks", "Shoes"]
```So better wear pants, undershorts, pants and socks with your shoes.
(In case you wonder: There is also a `topologicalSort` function which can compute
valid linear orderings)# Credits
I was inspired by Martin Erwig's original idea realized in the
[functional graph library](http://hackage.haskell.org/package/fgl-5.5.2.1), but
I also tried to keep it as simple as possible, bringing the neatness of Elm to
graph libraries.