Module
Hylograph.Optics
- Package
- purescript-hylograph-optics
- Repository
- afcondon/purescript-hylograph-optics
Hylograph Optics
A comprehensive optics library for Hylograph data structures.
This module provides lenses, prisms, traversals, and affine traversals for working with Trees, Graphs, and other visualization data structures.
Quick Reference
Trees:
_root- Focus on root value_children- Focus on child list_leaves- Traverse all leaves_atDepth n- Traverse nodes at depth n_nodeAtPath [i,j,k]- Focus on node at path
Graphs:
_node id- Focus on specific node_edge from to- Focus on specific edge_edgeWeight from to- Focus on edge weight_neighbors id- Traverse neighbors_nodesWhere pred- Traverse matching nodes
Example Usage
import Hylograph.Optics
import Data.Lens (view, set, over, preview, toListOf)
-- Trees
view _root myTree -- Get root value
set _root 100 myTree -- Set root to 100
over _leaves (*2) myTree -- Double all leaves
toListOf (_atDepth 2) myTree -- Get all grandchildren
-- Graphs
preview (_node (NodeId "A")) graph -- Get node if exists
set (_edgeWeight a b) 2.5 graph -- Update edge weight
toListOf (_neighbors a) graph -- Get all neighbors of A
Re-exports from Hylograph.Optics.Graph
#_outEdges Source
_outEdges :: NodeId -> Traversal' Graph EdgeTraverse all outgoing edges from a node.
toListOf (_outEdges (NodeId "A")) graph
-- => All edges where A is the source
#_nodesWhere Source
_nodesWhere :: (NodeId -> Boolean) -> Traversal' Graph NodeIdTraverse nodes matching a predicate.
toListOf (_nodesWhere (\(NodeId s) -> String.length s == 1)) graph
-- => All single-letter node IDs
#_neighbors Source
_neighbors :: NodeId -> Traversal' Graph NodeIdTraverse all neighbors of a node.
toListOf (_neighbors (NodeId "A")) graph
-- => All nodes connected to A
#_inEdges Source
_inEdges :: NodeId -> Traversal' Graph EdgeTraverse all incoming edges to a node.
toListOf (_inEdges (NodeId "A")) graph
-- => All edges where A is the target
#_edgesWhere Source
_edgesWhere :: (Edge -> Boolean) -> Traversal' Graph EdgeTraverse edges matching a predicate.
toListOf (_edgesWhere (\e -> e.weight > 1.0)) graph
-- => All edges with weight > 1.0
Re-exports from Hylograph.Optics.Tree
#_matching Source
_matching :: forall a. (a -> Boolean) -> Traversal' (Tree a) aTraverse nodes matching a predicate on their value.
toListOf (_matching (> 5)) tree
-- => All node values greater than 5
over (_matching isEven) (*2) tree
-- Double all even values
#_leaves Source
_leaves :: forall a. Traversal' (Tree a) aTraverse all leaf nodes (nodes with no children).
toListOf _leaves tree
-- => List of all leaf values
over _leaves (*2) tree
-- Double all leaf values
#_branches Source
_branches :: forall a. Traversal' (Tree a) aTraverse all branch nodes (nodes with children).
toListOf _branches tree
-- => List of all branch values