Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2cc225e

Browse files
committed
Upgrade to v4.8 Framework
1 parent fe692d2 commit 2cc225e

File tree

5 files changed

+127
-4
lines changed

5 files changed

+127
-4
lines changed

FunProgLib/FunProgLib.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>FunProgLib</RootNamespace>
1111
<AssemblyName>FunProgLib</AssemblyName>
12-
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
<TargetFrameworkProfile />
1515
</PropertyGroup>

FunProgLib/lists/List.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// Okasaki, Chris. "2.1 Lists." Purely Functional Data Structures.
1010
// Cambridge, U.K.: Cambridge UP, 1998. 7-11. Print.
1111

12+
using System.Diagnostics;
13+
1214
namespace FunProgLib.lists
1315
{
1416
using System;
@@ -58,7 +60,8 @@ public bool MoveNext()
5860
public T Current => _list.Element;
5961

6062
public void Dispose()
61-
{ }
63+
{
64+
}
6265
}
6366
}
6467

@@ -100,5 +103,37 @@ private static Node Rev(Node listIn, Node listOut)
100103
var next = new Node(Head(listIn), listOut);
101104
return Rev(Tail(listIn), next);
102105
}
106+
107+
public static TB FoldRight<TB>(Node xs, TB z, Func<T, TB, TB> f)
108+
{
109+
if (IsEmpty(xs)) return z;
110+
return f(xs.Element, FoldRight<TB>(xs.Next, z, f));
111+
}
112+
113+
public static TB FoldLeftR<TB>(Node xs, TB z, Func<TB, T, TB> f)
114+
{
115+
var identity = new Func<TB, TB>(b => b);
116+
var combinerDelayer =
117+
new Func<T, Func<TB, TB>, Func<TB, TB>>((a, delayedExec) => b => delayedExec(f(b, a)));
118+
var chain = FoldRight(xs, identity, combinerDelayer);
119+
return chain(z);
120+
}
121+
122+
public static TB FoldLeft<TB>(Node xs, TB z, Func<TB, T, TB> f)
123+
{
124+
// while (true)
125+
// {
126+
// if (IsEmpty(xs)) return z;
127+
// var xs1 = xs;
128+
// xs = xs.Next;
129+
// z = f(z, xs1.Element);
130+
// }
131+
132+
if (IsEmpty(xs)) return z;
133+
return FoldLeft<TB>(xs.Next, f(z, xs.Element), f);
134+
}
135+
136+
public static TB FoldRightL<TB>(Node xs, TB z, Func<T, TB, TB> f) =>
137+
FoldLeft(xs, new Func<TB, TB>(b => b), (g, a) => b => g(f(a, b)))(z);
103138
}
104-
}
139+
}

FunProgTests/FunProgTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<AppDesignerFolder>Properties</AppDesignerFolder>
99
<RootNamespace>FunProgTests</RootNamespace>
1010
<AssemblyName>FunProgTests</AssemblyName>
11-
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
11+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
1212
<FileAlignment>512</FileAlignment>
1313
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
1414
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>

FunProgTests/lists/ListTests.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,85 @@ public void CatTest()
118118
var list3 = List<string>.Cat(list1, list2);
119119
Assert.AreEqual("[now,, How, cow?, brown]", list3.ToReadableString());
120120
}
121+
122+
[TestMethod]
123+
public void FoldRightSumTest()
124+
{
125+
var data = new[]{ 1, 2, 3, 4, 5 };
126+
var list = data.Aggregate(List<int>.Empty, (current, word) => List<int>.Cons(word, current));
127+
128+
var sum = List<int>.FoldRight(list, 0, (x, y) => x + y);
129+
Assert.AreEqual(15, sum);
130+
}
131+
132+
[TestMethod]
133+
public void FoldLeftSumTest()
134+
{
135+
var data = new[] { 1, 2, 3, 4, 5 };
136+
var list = data.Aggregate(List<int>.Empty, (current, word) => List<int>.Cons(word, current));
137+
138+
var sum = List<int>.FoldLeft(list, 0, (x, y) => x + y);
139+
Assert.AreEqual(15, sum);
140+
}
141+
142+
[TestMethod]
143+
public void FoldLeftRSumTest()
144+
{
145+
var data = new[] { 1, 2, 3, 4, 5 };
146+
var list = data.Aggregate(List<int>.Empty, (current, word) => List<int>.Cons(word, current));
147+
148+
var sum = List<int>.FoldLeftR(list, 0, (x, y) => x + y);
149+
Assert.AreEqual(15, sum);
150+
}
151+
152+
[TestMethod]
153+
public void FoldRightLSumTest()
154+
{
155+
var data = new[] { 1, 2, 3, 4, 5 };
156+
var list = data.Aggregate(List<int>.Empty, (current, word) => List<int>.Cons(word, current));
157+
158+
var sum = List<int>.FoldRightL(list, 0, (x, y) => x + y);
159+
Assert.AreEqual(15, sum);
160+
}
161+
162+
[TestMethod]
163+
public void FoldRightProductTest()
164+
{
165+
var data = new[] { 1.0, 2.0, 3.0, 4.0, 5.0 };
166+
var list = data.Aggregate(List<double>.Empty, (current, word) => List<double>.Cons(word, current));
167+
168+
var product = List<double>.FoldRight(list, 1.0, (x, y) => x * y);
169+
Assert.AreEqual(120.0, product);
170+
}
171+
172+
[TestMethod]
173+
public void FoldLeftProductTest()
174+
{
175+
var data = new[] { 1.0, 2.0, 3.0, 4.0, 5.0 };
176+
var list = data.Aggregate(List<double>.Empty, (current, word) => List<double>.Cons(word, current));
177+
178+
var product = List<double>.FoldLeft(list, 1.0, (x, y) => x * y);
179+
Assert.AreEqual(120.0, product);
180+
}
181+
182+
[TestMethod]
183+
public void FoldLeftRProductTest()
184+
{
185+
var data = new[] { 1.0, 2.0, 3.0, 4.0, 5.0 };
186+
var list = data.Aggregate(List<double>.Empty, (current, word) => List<double>.Cons(word, current));
187+
188+
var product = List<double>.FoldLeftR(list, 1.0, (x, y) => x * y);
189+
Assert.AreEqual(120.0, product);
190+
}
191+
192+
[TestMethod]
193+
public void FoldRightLProductTest()
194+
{
195+
var data = new[] { 1.0, 2.0, 3.0, 4.0, 5.0 };
196+
var list = data.Aggregate(List<double>.Empty, (current, word) => List<double>.Cons(word, current));
197+
198+
var product = List<double>.FoldRightL(list, 1.0, (x, y) => x * y);
199+
Assert.AreEqual(120.0, product);
200+
}
121201
}
122202
}

FunProgramming.sln.DotSettings

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Eakin/@EntryIndexedValue">True</s:Boolean>
3+
4+
<s:Boolean x:Key="/Default/UserDictionary/Words/=lenf/@EntryIndexedValue">True</s:Boolean>
5+
<s:Boolean x:Key="/Default/UserDictionary/Words/=lenr/@EntryIndexedValue">True</s:Boolean>
6+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Okasaki/@EntryIndexedValue">True</s:Boolean>
7+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Prog/@EntryIndexedValue">True</s:Boolean>
8+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Snoc/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

0 commit comments

Comments
 (0)