Module

Hylograph.Optics.Graph

Package
purescript-hylograph-optics
Repository
afcondon/purescript-hylograph-optics

Graph Optics

Lenses, traversals, and accessors for weighted graphs.

#_nodes Source

_nodes :: Lens' Graph (Array NodeId)

Lens to all node IDs in the graph.

view _nodes graph
-- => Array of all NodeIds

#getNode Source

getNode :: NodeId -> Graph -> Maybe NodeId

Get a node by ID if it exists.

getNode (NodeId "A") graph
-- => Just (NodeId "A") if exists, Nothing otherwise

#getNodePosition Source

getNodePosition :: NodeId -> Graph -> Maybe { x :: Number, y :: Number }

Get a node's position if set.

getNodePosition (NodeId "A") graph
-- => Just { x: 100.0, y: 200.0 } if positioned

#setNodePosition Source

setNodePosition :: NodeId -> { x :: Number, y :: Number } -> Graph -> Graph

Set a node's position.

setNodePosition (NodeId "A") { x: 100.0, y: 200.0 } graph

#_edges Source

_edges :: Lens' Graph (Array Edge)

Lens to all edges in the graph.

view _edges graph
-- => Array of all edges

#getEdge Source

getEdge :: NodeId -> NodeId -> Graph -> Maybe Edge

Get an edge by source and target.

getEdge (NodeId "A") (NodeId "B") graph
-- => Just edge if it exists

#getEdgeWeight Source

getEdgeWeight :: NodeId -> NodeId -> Graph -> Maybe Number

Get an edge's weight.

getEdgeWeight (NodeId "A") (NodeId "B") graph
-- => Just 1.5 (the weight)

#setEdgeWeight Source

setEdgeWeight :: NodeId -> NodeId -> Number -> Graph -> Graph

Set an edge's weight.

setEdgeWeight (NodeId "A") (NodeId "B") 2.0 graph
-- Update edge weight

#_neighbors Source

_neighbors :: NodeId -> Traversal' Graph NodeId

Traverse all neighbors of a node.

toListOf (_neighbors (NodeId "A")) graph
-- => All nodes connected to A

#_outEdges Source

_outEdges :: NodeId -> Traversal' Graph Edge

Traverse all outgoing edges from a node.

toListOf (_outEdges (NodeId "A")) graph
-- => All edges where A is the source

#_inEdges Source

_inEdges :: NodeId -> Traversal' Graph Edge

Traverse all incoming edges to a node.

toListOf (_inEdges (NodeId "A")) graph
-- => All edges where A is the target

#_nodesWhere Source

_nodesWhere :: (NodeId -> Boolean) -> Traversal' Graph NodeId

Traverse nodes matching a predicate.

toListOf (_nodesWhere (\(NodeId s) -> String.length s == 1)) graph
-- => All single-letter node IDs

#_edgesWhere Source

_edgesWhere :: (Edge -> Boolean) -> Traversal' Graph Edge

Traverse edges matching a predicate.

toListOf (_edgesWhere (\e -> e.weight > 1.0)) graph
-- => All edges with weight > 1.0