Last active
March 9, 2016 21:56
-
-
Save lovasoa/a865376f33328b316d0f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Html | |
import Svg exposing (..) | |
import Svg.Attributes exposing (..) | |
import Graphics.Element exposing (show) | |
import Dict | |
empty = {nodes = Dict.empty, nodeData = Dict.empty, edgeData = Dict.empty} | |
emptyNode = {from = [], to = []} | |
addNode id g = {g | nodes = Dict.insert id emptyNode g.nodes} | |
addNodeData id data g = | |
let g = addNode id g | |
in {g | nodeData = Dict.insert id data g.nodeData} | |
addEdge id1 id2 g = | |
let | |
up from maybeNode = | |
let node = Maybe.withDefault emptyNode maybeNode | |
in Just {node | | |
from = if from then id1 :: node.from else node.from, | |
to = if from then node.to else id2 :: node.to | |
} | |
in {g | nodes = Dict.update id1 (up True) <| Dict.update id2 (up False) g.nodes} | |
addEdgeData id1 id2 data g = | |
let g = addEdge id1 id2 g | |
in {g | edgeData = Dict.insert (id1,id2) data g.edgeData} | |
listNodes g = List.map (\n -> (n, Dict.get n g.nodeData)) <| Dict.keys g.nodes | |
graph = addEdgeData "Hello" "World" 2 | |
<| addNodeData "World" (900,15) | |
<| addNodeData "Hello" (90,150) | |
<| empty | |
point x y = let (x, y) = (toString x, toString y) in circle [cx x, cy y, r "10"] [] | |
main = svg [width "1000", height "1000"] | |
(List.map (\(c, d) -> let (x,y) = Maybe.withDefault (0,0) d in point x y) (listNodes graph)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment