Skip to content

Commit aa7d89b

Browse files
Default file work
1 parent f0cc2f5 commit aa7d89b

File tree

4 files changed

+165
-17
lines changed

4 files changed

+165
-17
lines changed

Zipper.cs

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
public class BinTree<T> : IEquatable<BinTree<T>>
6+
{
7+
public BinTree(T value, BinTree<T> left, BinTree<T> right)
8+
{
9+
Value = value;
10+
Left = left;
11+
Right = right;
12+
}
13+
14+
public BinTree(BinTree<T> tree) : this(tree.Value, tree.Left, tree.Right)
15+
{
16+
}
17+
18+
public T Value { get; }
19+
public BinTree<T> Left { get; }
20+
public BinTree<T> Right { get; }
21+
22+
public bool Equals(BinTree<T> other)
23+
{
24+
if (other == null || !Equals(Value, other.Value))
25+
return false;
26+
27+
if (!ReferenceEquals(Left, other.Left) && (!Left?.Equals(other.Left) ?? false))
28+
return false;
29+
30+
if (!ReferenceEquals(Right, other.Right) && (!Right?.Equals(other.Right) ?? false))
31+
return false;
32+
33+
return true;
34+
}
35+
}
36+
37+
public abstract class BinTreeCrumb<T>
38+
{
39+
public BinTreeCrumb(T value, BinTree<T> tree)
40+
{
41+
Value = value;
42+
Tree = tree;
43+
}
44+
45+
public T Value { get; }
46+
public BinTree<T> Tree { get; }
47+
}
48+
49+
public class BinTreeLeftCrumb<T> : BinTreeCrumb<T>
50+
{
51+
public BinTreeLeftCrumb(T value, BinTree<T> tree) : base(value, tree)
52+
{
53+
}
54+
}
55+
56+
public class BinTreeRightCrumb<T> : BinTreeCrumb<T>
57+
{
58+
public BinTreeRightCrumb(T value, BinTree<T> tree) : base(value, tree)
59+
{
60+
}
61+
}
62+
63+
public class Zipper<T>
64+
{
65+
private readonly T value;
66+
private readonly BinTree<T> left;
67+
private readonly BinTree<T> right;
68+
private readonly List<BinTreeCrumb<T>> crumbs;
69+
70+
public Zipper(T value, BinTree<T> left, BinTree<T> right, List<BinTreeCrumb<T>> crumbs)
71+
{
72+
this.value = value;
73+
this.left = left;
74+
this.right = right;
75+
this.crumbs = crumbs;
76+
}
77+
78+
public T Value => value;
79+
80+
public Zipper<T> SetValue(T newValue) => new Zipper<T>(newValue, left, right, crumbs);
81+
82+
public Zipper<T> SetLeft(BinTree<T> binTree) => new Zipper<T>(value, binTree, right, crumbs);
83+
84+
public Zipper<T> SetRight(BinTree<T> binTree) => new Zipper<T>(value, left, binTree, crumbs);
85+
86+
public Zipper<T> Left()
87+
{
88+
if (left == null)
89+
return null;
90+
91+
var newCrumbs = new[] { new BinTreeLeftCrumb<T>(value, right) }.Concat(crumbs).ToList();
92+
return new Zipper<T>(left.Value, left.Left, left.Right, newCrumbs);
93+
}
94+
95+
public Zipper<T> Right()
96+
{
97+
if (right == null)
98+
return null;
99+
100+
var newCrumbs = new[] { new BinTreeRightCrumb<T>(value, left) }.Concat(crumbs).ToList();
101+
return new Zipper<T>(right.Value, right.Left, right.Right, newCrumbs);
102+
}
103+
104+
public Zipper<T> Up()
105+
{
106+
if (crumbs.Count == 0)
107+
return null;
108+
109+
var firstCrumb = crumbs[0];
110+
var remainingCrumbs = crumbs.Skip(1).ToList();
111+
112+
if (firstCrumb is BinTreeLeftCrumb<T>)
113+
return new Zipper<T>(firstCrumb.Value, new BinTree<T>(value, left, right), firstCrumb.Tree, remainingCrumbs);
114+
115+
if (firstCrumb is BinTreeRightCrumb<T>)
116+
return new Zipper<T>(firstCrumb.Value, firstCrumb.Tree, new BinTree<T>(value, left, right), remainingCrumbs);
117+
118+
return null;
119+
}
120+
121+
public BinTree<T> ToTree()
122+
{
123+
var tree = new BinTree<T>(value, left, right);
124+
125+
foreach (var crumb in crumbs)
126+
{
127+
if (crumb is BinTreeLeftCrumb<T>)
128+
tree = new BinTree<T>(crumb.Value, new BinTree<T>(tree), crumb.Tree);
129+
if (crumb is BinTreeRightCrumb<T>)
130+
tree = new BinTree<T>(crumb.Value, crumb.Tree, new BinTree<T>(tree));
131+
}
132+
133+
return tree;
134+
}
135+
136+
public static Zipper<T> FromTree(BinTree<T> tree) => new Zipper<T>(tree.Value, tree.Left, tree.Right, new List<BinTreeCrumb<T>>());
137+
}

build.fsx

+14-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ let allProjects = !! (buildDir @@ "*/*.csproj")
1111
let defaultProjects =
1212
!! (buildDir @@ "*/*.csproj") --
1313
(buildDir @@ "*/DotDsl.csproj") --
14+
(buildDir @@ "*/Hangman.csproj") --
1415
(buildDir @@ "*/React.csproj")
1516
let refactoringProjects =
1617
!! (buildDir @@ "*/TreeBuilding.csproj") ++
@@ -44,7 +45,7 @@ Target "IgnoreExampleImplementation" (fun _ ->
4445
System.Text.Encoding.UTF8 allProjects
4546
)
4647

47-
Target "BuildUsingDefaultImplementation" (fun _ ->
48+
Target "BuildUsingStubImplementation" (fun _ ->
4849
Seq.iter restoreAndBuild defaultProjects
4950
)
5051

@@ -59,23 +60,28 @@ Target "TestRefactoringProjects" (fun _ ->
5960
Seq.iter restoreAndTest refactoringProjects
6061
)
6162

62-
Target "TestUsingExampleImplementation" (fun _ ->
63-
let useExampleInsteadOfDefaultImplementation project =
63+
Target "ReplaceStubWithExampleImplementation" (fun _ ->
64+
let replaceStubWithExampleImplementation project =
6465
let projectDir = directory project
66+
let stubFile = projectDir @@ filename project + "" |> changeExt ".cs"
6567
let exampleFile = projectDir @@ "Example.cs"
66-
let defaultFile = projectDir @@ filename project + "" |> changeExt ".cs"
67-
CopyFile defaultFile exampleFile
68+
69+
CopyFile stubFile exampleFile
70+
71+
Seq.iter replaceStubWithExampleImplementation allProjects
72+
)
6873

69-
Seq.iter useExampleInsteadOfDefaultImplementation allProjects
74+
Target "TestUsingExampleImplementation" (fun _ ->
7075
Seq.iter restoreAndTest allProjects
7176
)
7277

7378
"Clean"
7479
==> "CopyExercises"
7580
==> "IgnoreExampleImplementation"
76-
==> "BuildUsingDefaultImplementation"
81+
==> "BuildUsingStubImplementation"
7782
==> "EnableAllTests"
7883
==> "TestRefactoringProjects"
84+
==> "ReplaceStubWithExampleImplementation"
7985
==> "TestUsingExampleImplementation"
8086

81-
RunTargetOrDefault "TestUsingExampleImplementation"
87+
RunTargetOrDefault "TestUsingExampleImplementation"

exercises/hangman/Hangman.cs

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33

4-
public delegate void HangmanChangedEventHandler(object sender, HangmanState state);
5-
64
public class HangmanState
75
{
86
public HangmanGame.Status Status { get; set; }
@@ -24,8 +22,6 @@ public HangmanGame(string word)
2422
{
2523
}
2624

27-
public event HangmanChangedEventHandler StateChanged;
28-
2925
public void Start()
3026
{
3127
throw new NotImplementedException("You need to implement this function.");

exercises/zipper/ZipperTest.cs

+14-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@ public class ZipperTest
55
private static BinTree<int> bt(int v, BinTree<int> l, BinTree<int> r) => new BinTree<int>(v, l, r);
66
private static BinTree<int> leaf(int v) => bt(v, null, null);
77

8-
private static readonly BinTree<int> empty = null;
9-
private static readonly BinTree<int> t1 = new BinTree<int>(1, bt(2, empty, leaf(3)), leaf(4));
10-
private static readonly BinTree<int> t2 = new BinTree<int>(1, bt(5, empty, leaf(3)), leaf(4));
11-
private static readonly BinTree<int> t3 = new BinTree<int>(1, bt(2, leaf(5), leaf(3)), leaf(4));
12-
private static readonly BinTree<int> t4 = new BinTree<int>(1, leaf(2), leaf(4));
8+
private readonly BinTree<int> empty;
9+
private readonly BinTree<int> t1;
10+
private readonly BinTree<int> t2;
11+
private readonly BinTree<int> t3;
12+
private readonly BinTree<int> t4;
13+
14+
public ZipperTest()
15+
{
16+
empty = null;
17+
t1 = new BinTree<int>(1, bt(2, empty, leaf(3)), leaf(4));
18+
t2 = new BinTree<int>(1, bt(5, empty, leaf(3)), leaf(4));
19+
t3 = new BinTree<int>(1, bt(2, leaf(5), leaf(3)), leaf(4));
20+
t4 = new BinTree<int>(1, leaf(2), leaf(4));
21+
}
1322

1423
[Fact]
1524
public void Data_is_retained()

0 commit comments

Comments
 (0)