Skip to content

Commit

Permalink
Merge from type-classes branch (version 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
louthy committed Mar 6, 2017
2 parents 66338fb + 8f060d7 commit d6971e7
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 1 deletion.
22 changes: 21 additions & 1 deletion LanguageExt.Core/DataTypes/Stack/Stack.Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,16 @@ public static IEnumerable<T> distinct<T>(Stck<T> stack) =>
public static IEnumerable<T> distinct<EQ, T>(Stck<T> stack) where EQ : struct, Eq<T> =>
List.distinct<EQ,T>(stack);

/// <summary>
/// Return an enumerable with all duplicate values removed
/// </summary>
/// <typeparam name="T">Stack item type</typeparam>
/// <param name="stack">Stack</param>
/// <returns>An enumerable with all duplicate values removed</returns>
[Pure]
public static IEnumerable<T> distinct<T, K>(Stck<T> stack, Func<T, K> keySelector, Option<Func<K, K, bool>> compare = default(Option<Func<K, K, bool>>)) =>
List.distinct(stack, keySelector, compare);

/// <summary>
/// Returns a new enumerable with the first 'count' items from the stack
/// </summary>
Expand Down Expand Up @@ -840,6 +850,16 @@ public static IEnumerable<T> Distinct<T>(this Stck<T> stack) =>
public static IEnumerable<T> Distinct<EQ,T>(this Stck<T> stack) where EQ : struct, Eq<T> =>
LanguageExt.List.distinct<EQ,T>(stack);

/// <summary>
/// Return an enumerable with all duplicate values removed
/// </summary>
/// <typeparam name="T">Stack item type</typeparam>
/// <param name="stack">Stack</param>
/// <returns>An enumerable with all duplicate values removed</returns>
[Pure]
public static IEnumerable<T> Distinct<T, K>(this Stck<T> stack, Func<T, K> keySelector, Option<Func<K, K, bool>> compare = default(Option<Func<K, K, bool>>)) =>
LanguageExt.List.distinct(stack, keySelector, compare);

/// <summary>
/// Returns a new enumerable with the first 'count' items from the stack
/// </summary>
Expand Down Expand Up @@ -883,6 +903,6 @@ public static IEnumerable<T> TakeWhile<T>(this Stck<T> stack, Func<T, int, bool>
/// <param name="pred">Predicate</param>
/// <returns>True if any item in the stack matches the predicate provided</returns>
[Pure]
public static bool Exists<T>(Stck<T> stack, Func<T, bool> pred) =>
public static bool Exists<T>(this Stck<T> stack, Func<T, bool> pred) =>
LanguageExt.List.exists(stack, pred);
}
162 changes: 162 additions & 0 deletions LanguageExt.Tests/HashSetTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
using LanguageExt;
using LanguageExt.Trans;
using static LanguageExt.Prelude;
using static LanguageExt.HashSet;
using Xunit;
using System;
using System.Linq;

namespace LanguageExtTests
{
public class HashSetTests
{
[Fact]
public void HashSetHasItemsTest()
{
Assert.False(HashSet(1, 2, 3).IsEmpty);
}

[Fact]
public void HashSetGeneratorTest()
{
var m1 = HashSet<string>();
m1 = add(m1, "hello");
Assert.True(m1.Count == 1 && contains(m1, "hello"));
}

[Fact]
public void HashSetGeneratorAndMatchTest()
{
var m2 = HashSet("a", "b", "c");

m2 = add(m2, "world");

var res = find(m2, "world").Match(
v => v,
() => "failed"
);

Assert.True(res == "world");
}

[Fact]
public void HashSetAddInOrderTest()
{
var m = HashSet(1);
m.Find(1).IfNone(() => failwith<int>("Broken"));

m = HashSet(1, 2);
m.Find(1).IfNone(() => failwith<int>("Broken"));
m.Find(2).IfNone(() => failwith<int>("Broken"));

m = HashSet(1, 2, 3);
m.Find(1).IfNone(() => failwith<int>("Broken"));
m.Find(2).IfNone(() => failwith<int>("Broken"));
m.Find(3).IfNone(() => failwith<int>("Broken"));

m = HashSet(1, 2, 3, 4);
m.Find(1).IfNone(() => failwith<int>("Broken"));
m.Find(2).IfNone(() => failwith<int>("Broken"));
m.Find(3).IfNone(() => failwith<int>("Broken"));
m.Find(4).IfNone(() => failwith<int>("Broken"));

m = HashSet(1, 2, 3, 4, 5);
m.Find(1).IfNone(() => failwith<int>("Broken"));
m.Find(2).IfNone(() => failwith<int>("Broken"));
m.Find(3).IfNone(() => failwith<int>("Broken"));
m.Find(4).IfNone(() => failwith<int>("Broken"));
m.Find(5).IfNone(() => failwith<int>("Broken"));
}

[Fact]
public void MapAddInReverseOrderTest()
{
var m = HashSet(1);
m.Find(1).IfNone(() => failwith<int>("Broken"));

m = HashSet(2, 1);
m.Find(1).IfNone(() => failwith<int>("Broken"));
m.Find(2).IfNone(() => failwith<int>("Broken"));

m = HashSet(3, 2, 1);
m.Find(1).IfNone(() => failwith<int>("Broken"));
m.Find(2).IfNone(() => failwith<int>("Broken"));
m.Find(3).IfNone(() => failwith<int>("Broken"));

m = HashSet(4, 3, 2, 1);
m.Find(1).IfNone(() => failwith<int>("Broken"));
m.Find(2).IfNone(() => failwith<int>("Broken"));
m.Find(3).IfNone(() => failwith<int>("Broken"));
m.Find(4).IfNone(() => failwith<int>("Broken"));

m = HashSet(5, 4, 3, 2, 1);
m.Find(1).IfNone(() => failwith<int>("Broken"));
m.Find(2).IfNone(() => failwith<int>("Broken"));
m.Find(3).IfNone(() => failwith<int>("Broken"));
m.Find(4).IfNone(() => failwith<int>("Broken"));
m.Find(5).IfNone(() => failwith<int>("Broken"));
}

[Fact]
public void MapAddInMixedOrderTest()
{
var m = HashSet(5, 1, 3, 2, 4);
m.Find(1).IfNone(() => failwith<int>("Broken"));
m.Find(2).IfNone(() => failwith<int>("Broken"));
m.Find(3).IfNone(() => failwith<int>("Broken"));
m.Find(4).IfNone(() => failwith<int>("Broken"));
m.Find(5).IfNone(() => failwith<int>("Broken"));

m = HashSet(1, 3, 5, 2, 4);
m.Find(1).IfNone(() => failwith<int>("Broken"));
m.Find(2).IfNone(() => failwith<int>("Broken"));
m.Find(3).IfNone(() => failwith<int>("Broken"));
m.Find(4).IfNone(() => failwith<int>("Broken"));
m.Find(5).IfNone(() => failwith<int>("Broken"));
}

[Fact]
public void MapRemoveTest()
{
var m = HashSet("a", "b", "c", "d", "e");

m.Find("a").IfNone(() => failwith<string>("Broken 1"));
m.Find("b").IfNone(() => failwith<string>("Broken 2"));
m.Find("c").IfNone(() => failwith<string>("Broken 3"));
m.Find("d").IfNone(() => failwith<string>("Broken 4"));
m.Find("e").IfNone(() => failwith<string>("Broken 5"));

Assert.True(m.Count == 5);

m = remove(m, "d");
Assert.True(m.Count == 4);
Assert.True(m.Find("d").IsNone);
m.Find("a").IfNone(() => failwith<string>("Broken 1"));
m.Find("b").IfNone(() => failwith<string>("Broken 2"));
m.Find("c").IfNone(() => failwith<string>("Broken 3"));
m.Find("e").IfNone(() => failwith<string>("Broken 5"));

m = remove(m, "a");
Assert.True(m.Count == 3);
Assert.True(m.Find("a").IsNone);
m.Find("b").IfNone(() => failwith<string>("Broken 2"));
m.Find("c").IfNone(() => failwith<string>("Broken 3"));
m.Find("e").IfNone(() => failwith<string>("Broken 5"));

m = remove(m, "b");
Assert.True(m.Count == 2);
Assert.True(m.Find("b").IsNone);
m.Find("c").IfNone(() => failwith<string>("Broken 3"));
m.Find("e").IfNone(() => failwith<string>("Broken 5"));

m = remove(m, "c");
Assert.True(m.Count == 1);
Assert.True(m.Find("c").IsNone);
m.Find("e").IfNone(() => failwith<string>("Broken 5"));

m = remove(m, "e");
Assert.True(m.Count == 0);
Assert.True(m.Find("e").IsNone);
}
}
}

0 comments on commit d6971e7

Please sign in to comment.