-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDiff.elm
More file actions
76 lines (53 loc) · 1.65 KB
/
Copy pathDiff.elm
File metadata and controls
76 lines (53 loc) · 1.65 KB
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
module Diff exposing (..)
import Essential exposing (Student, Course, Enrollment, EssentialModel)
import ListBackedSet as Set exposing (Set)
type alias Diff a =
{ remove : List a, add : List a }
isEmpty : Diff a -> Bool
isEmpty diff =
List.isEmpty diff.remove && List.isEmpty diff.add
emptyDiff : Diff a
emptyDiff =
{ remove = [], add = [] }
setDiff : Set a -> Set a -> Diff a
setDiff old new =
let
common =
Set.intersect old new
added =
Set.diff new common
removed =
Set.diff old common
in
{ remove = Set.toList removed, add = Set.toList added }
type alias ModelDiff =
{ studentDiff : Diff Student
, courseDiff : Diff Course
, enrollmentDiff : Diff Enrollment
}
{-| Figure out diff between two models. (Note that this generates far from minimal diff.)
-}
diff : EssentialModel -> EssentialModel -> ModelDiff
diff old new =
ModelDiff
(setDiff old.students new.students)
(setDiff old.courses new.courses)
(setDiff old.enrollments new.enrollments)
patchSet : Set a -> Diff a -> Set a
patchSet set diff =
let
setMinusRemoved =
Set.foldl Set.remove set (Set.fromList diff.remove)
setMinusRemovedPlusAdd =
Set.foldl Set.insert setMinusRemoved (Set.fromList diff.add)
in
setMinusRemovedPlusAdd
patch : EssentialModel -> ModelDiff -> EssentialModel
patch model modelDiff =
{ students = patchSet model.students modelDiff.studentDiff
, courses = patchSet model.courses modelDiff.courseDiff
, enrollments = patchSet model.enrollments modelDiff.enrollmentDiff
}
--
--
--