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

#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

#setEdgeWeight Source

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

Set an edge's weight.

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

#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

#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

#getEdgeWeight Source

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

Get an edge's weight.

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

#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

#_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

#_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

#_nodes Source

_nodes :: Lens' Graph (Array NodeId)

Lens to all node IDs in the graph.

view _nodes graph
-- => Array of all NodeIds

#_neighbors Source

_neighbors :: NodeId -> Traversal' Graph NodeId

Traverse all neighbors of a node.

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

#_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

#_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

#_edges Source

_edges :: Lens' Graph (Array Edge)

Lens to all edges in the graph.

view _edges graph
-- => Array of all edges

Re-exports from Hylograph.Optics.Tree

#nodeAtPath Source

nodeAtPath :: forall a. List Int -> Tree a -> Maybe (Tree a)

Get the node at a path if it exists. This is a simple accessor, not an optic.

nodeAtPath [0, 1, 2] tree
-- => Maybe the node at that path

#_root Source

_root :: forall a. Lens' (Tree a) a

Focus on the root value of a tree.

view _root (mkTree 1 [mkTree 2 [], mkTree 3 []])
-- => 1

set _root 100 tree
-- Changes root to 100, children unchanged

#_matching Source

_matching :: forall a. (a -> Boolean) -> Traversal' (Tree a) a

Traverse 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) a

Traverse all leaf nodes (nodes with no children).

toListOf _leaves tree
-- => List of all leaf values

over _leaves (*2) tree
-- Double all leaf values

#_forest Source

_forest :: forall a. Lens' (Tree a) (Forest a)

Alias for _children (Forest = List Tree)

#_children Source

_children :: forall a. Lens' (Tree a) (List (Tree a))

Focus on the children of a tree (as a List of Trees).

view _children tree
-- => List of child trees

over _children (filter hasValue) tree
-- Filter children by some predicate

#_childAt Source

_childAt :: forall a. Int -> Lens' (Tree a) (Maybe (Tree a))

Focus on a specific child by index. Returns Nothing if index out of bounds.

preview (_childAt 0) tree
-- => Maybe the first child

#_branches Source

_branches :: forall a. Traversal' (Tree a) a

Traverse all branch nodes (nodes with children).

toListOf _branches tree
-- => List of all branch values

#_atDepth Source

_atDepth :: forall a. Int -> Traversal' (Tree a) a

Traverse all nodes at a specific depth. Depth 0 is the root, depth 1 is immediate children, etc.

toListOf (_atDepth 2) tree
-- => List of all grandchild values

#_allNodes Source

_allNodes :: forall a. Traversal' (Tree a) (Tree a)

Traverse ALL nodes in the tree (pre-order). Note: This traverses subtrees, not just values.

toListOf _allNodes tree
-- => Every subtree in the tree