9 people like it.
Like the snippet!
A Small DSL for Building Graphs
A small DSL for graph building by combining paths. It's just a set of edges under the hood, so no need to worry about duplication causing problems. This is part of a larger project I'm calling edgy.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
|
module Graphs
type Graph<'a when 'a: comparison> =
{
/// Element to attach to
Current: 'a
/// (From --> To) Set
Edges: ('a * 'a) Set
}
type Graph
with
// N <-| a
static member op_LessMinusBar (l: Graph<'a>, r: 'a) : Graph<'a> =
{ Current = r; Edges = l.Edges |> Set.add (r, l.Current) }
// N |-> a
static member op_BarMinusGreater (l: Graph<'a>, r: 'a) : Graph<'a> =
{ Current = r; Edges = l.Edges |> Set.add (l.Current, r) }
/// Pseudo Graph Record Type Constructor
let Graph (inval: 'a) = { Current = inval; Edges = Set.empty }
/// Combines all graphs
let Combine = Seq.reduce (fun l r -> { r with Edges = Set.union l.Edges r.Edges })
// Example
let totalGraph =
[
Graph 2 <-| 1
Graph 2 <-| 1 |-> 3
Graph 2 |-> 4 <-| 3 |-> 5
Graph 2 |-> 4 <-| 3 <-| 8
Graph 4 |-> 6
] |> Combine
// val totalGraph : Graph<int> =
// {Current = 6;
// Edges = set [(1, 2); (1, 3); (2, 4); (3, 4); (3, 5); (4, 6); (8, 3)];}
|
module Graphs
type Graph<'a (requires comparison)> =
{Current: 'a;
Edges: Set<'a * 'a>;}
static member ( |-> ) : l:Graph<'a> * r:'a -> Graph<'a>
static member ( <-| ) : l:Graph<'a> * r:'a -> Graph<'a>
Full name: Graphs.Graph<_>
Graph.Current: 'a
Element to attach to
Graph.Edges: Set<'a * 'a>
(From --> To) Set
Multiple items
module Set
from Microsoft.FSharp.Collections
--------------------
type Set<'T (requires comparison)> =
interface IComparable
interface IEnumerable
interface IEnumerable<'T>
interface ICollection<'T>
new : elements:seq<'T> -> Set<'T>
member Add : value:'T -> Set<'T>
member Contains : value:'T -> bool
override Equals : obj -> bool
member IsProperSubsetOf : otherSet:Set<'T> -> bool
member IsProperSupersetOf : otherSet:Set<'T> -> bool
...
Full name: Microsoft.FSharp.Collections.Set<_>
--------------------
new : elements:seq<'T> -> Set<'T>
val l : Graph<'a> (requires comparison)
val r : 'a (requires comparison)
val add : value:'T -> set:Set<'T> -> Set<'T> (requires comparison)
Full name: Microsoft.FSharp.Collections.Set.add
Multiple items
val Graph : inval:'a -> Graph<'a> (requires comparison)
Full name: Graphs.Graph
Pseudo Graph Record Type Constructor
--------------------
type Graph<'a (requires comparison)> =
{Current: 'a;
Edges: Set<'a * 'a>;}
static member ( |-> ) : l:Graph<'a> * r:'a -> Graph<'a>
static member ( <-| ) : l:Graph<'a> * r:'a -> Graph<'a>
Full name: Graphs.Graph<_>
val inval : 'a (requires comparison)
val empty<'T (requires comparison)> : Set<'T> (requires comparison)
Full name: Microsoft.FSharp.Collections.Set.empty
val Combine : (Graph<int> list -> Graph<int>)
Full name: Graphs.Combine
Combines all graphs
module Seq
from Microsoft.FSharp.Collections
val reduce : reduction:('T -> 'T -> 'T) -> source:seq<'T> -> 'T
Full name: Microsoft.FSharp.Collections.Seq.reduce
val l : Graph<int>
val r : Graph<int>
val union : set1:Set<'T> -> set2:Set<'T> -> Set<'T> (requires comparison)
Full name: Microsoft.FSharp.Collections.Set.union
Graph.Edges: Set<int * int>
(From --> To) Set
val totalGraph : Graph<int>
Full name: Graphs.totalGraph
More information