Yog.Functional.Transform (YogEx v0.98.0)

Copy Markdown View Source

Higher-order transformations for inductive graphs — map, filter, and structural changes.

This module provides functions that operate on the graph as a whole using the inductive primitives from Yog.Functional.Model.

Available Transformations

TransformationFunctionDescription
Map Nodesmap_nodes/2Transform node contexts
Map Labelsmap_labels/2Transform node labels
Map Edge Labelsmap_edge_labels/2Transform edge labels
Filterfilter_nodes/2Remove nodes by predicate
Foldfold_nodes/3Accumulate over all nodes
Reversereverse/1Flip edge directions
To Directedto_directed/1Reinterpret as directed
To Undirectedto_undirected/1Symmetrize edges

Summary

Functions

Filters nodes in the graph based on a predicate function.

Folds over all nodes in the graph.

Transforms the labels of all edges using the given function.

Transforms the labels of all nodes using the given function.

Performs a map operation over all nodes in the graph.

Reverses the direction of all edges in a directed graph.

Converts an undirected graph to a directed one.

Converts a directed graph to an undirected one by symmetrizing edges.

Functions

filter_nodes(graph, fun)

Filters nodes in the graph based on a predicate function.

fold_nodes(model, initial, fun)

@spec fold_nodes(Yog.Functional.Model.t(), acc, (Yog.Functional.Model.Context.t(),
                                           acc ->
                                             acc)) :: acc
when acc: any()

Folds over all nodes in the graph.

map_edge_labels(graph, fun)

Transforms the labels of all edges using the given function.

Examples

iex> alias Yog.Functional.{Model, Transform}
iex> graph = Model.empty() |> Model.put_node(1, "A") |> Model.put_node(2, "B")
...> |> Model.add_edge!(1, 2, 10)
iex> graph = Transform.map_edge_labels(graph, fn label -> label * 2 end)
iex> Model.get_edge(graph, 1, 2)
{:ok, 20}

map_labels(graph, fun)

Transforms the labels of all nodes using the given function.

map_nodes(graph, fun)

Performs a map operation over all nodes in the graph.

Examples

iex> alias Yog.Functional.{Model, Transform}
iex> graph = Model.empty() |> Model.put_node(1, "A")
iex> graph = Transform.map_nodes(graph, fn ctx -> %{ctx | label: "B"} end)
iex> {:ok, ctx} = Model.get_node(graph, 1)
iex> ctx.label
"B"

reverse(graph)

Reverses the direction of all edges in a directed graph.

Examples

iex> alias Yog.Functional.{Model, Transform}
iex> graph = Model.empty() |> Model.put_node(1, "A") |> Model.put_node(2, "B")
...> |> Model.add_edge!(1, 2)
iex> graph = Transform.reverse(graph)
iex> Model.has_edge?(graph, 2, 1)
true
iex> Model.has_edge?(graph, 1, 2)
false

to_directed(graph)

Converts an undirected graph to a directed one.

to_undirected(graph)

@spec to_undirected(Yog.Functional.Model.t()) :: Yog.Functional.Model.t()

Converts a directed graph to an undirected one by symmetrizing edges.