-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGSet.elm
106 lines (64 loc) · 2.15 KB
/
GSet.elm
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
module Test.Examples.GSet exposing
( GSet, GSetOperation(..)
, empty, insert
, fromWeave, toWeave
, value
)
{-| An example set using the work-in-progress Ordt.Weave API of the package `alexandrepiveteau/elm-ordt`, that simply
inserts each operation forever in a grow-only set.
# Counters
@docs GSet, GSetOperation
# Build
@docs empty, insert
# Weaves
@docs fromWeave, toWeave
#Query
@docs value
-}
import Ordt.Weave as Weave exposing (Weave)
import Ordt.Weft exposing (Weft)
import Set as CoreSet
-- SETS
type GSetOperation comparable
= Insert comparable
type GSet site comparable
= Set_built_in (Weave site (GSetOperation comparable))
empty : GSet comparableA comparableB
empty =
Set_built_in Weave.empty
integrate : comparableA -> GSetOperation comparableB -> GSet comparableA comparableB -> GSet comparableA comparableB
integrate site operation (Set_built_in weave) =
let
newWeave =
Weave.push site operation CoreSet.empty weave
in
Set_built_in newWeave
insert : comparableA -> comparableB -> GSet comparableA comparableB -> GSet comparableA comparableB
insert site element weave =
integrate site (Insert element) weave
-- WEAVES
fromWeave : Weave comparableA (GSetOperation comparableB) -> GSet comparableA comparableB
fromWeave weave =
Set_built_in weave
toWeave : GSet comparableA comparableB -> Weave comparableA (GSetOperation comparableB)
toWeave (Set_built_in weave) =
weave
-- QUERY
{-| Performs one step of accumulating a `SetOperation` into a core `Set` value. When applied
repeatedly, this function can reduce a whole weave of operations.
Note that for this set we do not care at all about the previous steps when performing this
accumulation.
-}
step : Weft comparableA -> GSetOperation comparableB -> CoreSet.Set comparableB -> CoreSet.Set comparableB
step _ operation acc =
case operation of
Insert element ->
CoreSet.insert element acc
{-| Evaluate the current elements of this set.
-}
value : GSet comparableA comparableB -> CoreSet.Set comparableB
value (Set_built_in weave) =
Weave.foldl
step
CoreSet.empty
weave