Skip to content

Commit

Permalink
Added Tuple2 and extension methods for Tuple2 and IEnumerable of Tuple2
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed Feb 13, 2010
1 parent 204eff3 commit 9079ed3
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 0 deletions.
28 changes: 28 additions & 0 deletions NExtLib.Tests/Linq/EnumerableExtensionTests.cs
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NExtLib.Linq;
using NExtLib.UnitTest;

namespace NExtLib.Tests.Linq
{
[TestFixture]
public class EnumerableExtensionTests
{
private readonly IEnumerable<string> TestList = new List<string> { "Foo", "Bar", "Quux" };

[Test]
public void TestWithIndex()
{
int expectedIndex = 0;

foreach (var item in TestList.WithIndex())
{
item.Item2.ShouldEqual(expectedIndex);
expectedIndex++;
}
}
}
}
1 change: 1 addition & 0 deletions NExtLib.Tests/NExtLib.Tests.csproj
Expand Up @@ -40,6 +40,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Linq\EnumerableExtensionTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
Expand Down
16 changes: 16 additions & 0 deletions NExtLib/Linq/EnumerableExtensions.cs
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NExtLib.Linq
{
public static class EnumerableExtensions
{
public static IEnumerable<Tuple<T, int>> WithIndex<T>(this IEnumerable<T> source)
{
int index = 0;
return source.Select(item => Tuple.Create(item, index++));
}
}
}
82 changes: 82 additions & 0 deletions NExtLib/Linq/TupleEnumerableExtensions.cs
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NExtLib.Linq
{
public static class TupleEnumerableExtensions
{
public static bool Any<T1, T2>(this IEnumerable<Tuple<T1, T2>> source,
Func<T1, T2, bool> predicate)
{
return source.Any(tuple => predicate(tuple.Item1, tuple.Item2));
}

public static bool All<T1, T2>(this IEnumerable<Tuple<T1, T2>> source,
Func<T1, T2, bool> predicate)
{
return source.All(tuple => predicate(tuple.Item1, tuple.Item2));
}

public static int Count<T1, T2>(this IEnumerable<Tuple<T1, T2>> source,
Func<T1, T2, bool> predicate)
{
return source.Count(tuple => predicate(tuple.Item1, tuple.Item2));
}

public static long LongCount<T1, T2>(this IEnumerable<Tuple<T1, T2>> source,
Func<T1, T2, bool> predicate)
{
return source.LongCount(tuple => predicate(tuple.Item1, tuple.Item2));
}

public static Tuple<T1, T2> First<T1, T2>(this IEnumerable<Tuple<T1, T2>> source,
Func<T1, T2, bool> predicate)
{
return source.First(tuple => predicate(tuple.Item1, tuple.Item2));
}

public static Tuple<T1, T2> FirstOrDefault<T1, T2>(this IEnumerable<Tuple<T1, T2>> source,
Func<T1, T2, bool> predicate)
{
return source.FirstOrDefault(tuple => predicate(tuple.Item1, tuple.Item2));
}

public static Tuple<T1, T2> Last<T1, T2>(this IEnumerable<Tuple<T1, T2>> source,
Func<T1, T2, bool> predicate)
{
return source.Last(tuple => predicate(tuple.Item1, tuple.Item2));
}

public static Tuple<T1, T2> LastOrDefault<T1, T2>(this IEnumerable<Tuple<T1, T2>> source,
Func<T1, T2, bool> predicate)
{
return source.LastOrDefault(tuple => predicate(tuple.Item1, tuple.Item2));
}

public static IEnumerable<TResult> Select<T1, T2, TResult>(this IEnumerable<Tuple<T1, T2>> source,
Func<T1, T2, TResult> selector)
{
return source.Select(tuple => selector(tuple.Item1, tuple.Item2));
}

public static IEnumerable<TResult> SelectMany<T1, T2, TResult>(this IEnumerable<Tuple<T1, T2>> source,
Func<T1, T2, IEnumerable<TResult>> selector)
{
return source.SelectMany(tuple => selector(tuple.Item1, tuple.Item2));
}

public static Tuple<T1, T2> Single<T1, T2>(this IEnumerable<Tuple<T1, T2>> source,
Func<T1, T2, bool> predicate)
{
return source.Single(tuple => predicate(tuple.Item1, tuple.Item2));
}

public static Tuple<T1, T2> SingleOrDefault<T1, T2>(this IEnumerable<Tuple<T1, T2>> source,
Func<T1, T2, bool> predicate)
{
return source.SingleOrDefault(tuple => predicate(tuple.Item1, tuple.Item2));
}
}
}
5 changes: 5 additions & 0 deletions NExtLib/NExtLib.csproj
Expand Up @@ -39,7 +39,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Linq\EnumerableExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tuple.cs" />
<Compile Include="Linq\TupleEnumerableExtensions.cs" />
<Compile Include="TupleExtensions.cs" />
<Compile Include="Tuple`2.cs" />
<Compile Include="Xml\Linq\XElementExtensions.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
15 changes: 15 additions & 0 deletions NExtLib/Tuple.cs
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NExtLib
{
public static class Tuple
{
public static Tuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2)
{
return new Tuple<T1, T2>(item1, item2);
}
}
}
23 changes: 23 additions & 0 deletions NExtLib/TupleExtensions.cs
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NExtLib
{
public static class TupleExtensions
{
public static void Run<T1, T2>(this Tuple<T1, T2> tuple, Action<T1, T2> action)
{
action(tuple.Item1, tuple.Item2);
}

public static void IfGood<T>(this Tuple<bool, T> tuple, Action<T> action)
{
if (tuple.Item1)
{
action(tuple.Item2);
}
}
}
}
30 changes: 30 additions & 0 deletions NExtLib/Tuple`2.cs
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NExtLib
{
public sealed class Tuple<T1,T2>
{
private readonly T1 _item1;
private readonly T2 _item2;

public Tuple(T1 item1, T2 item2)
{
_item1 = item1;
_item2 = item2;
}

public T1 Item1
{
get { return _item1; }
}


public T2 Item2
{
get { return _item2; }
}
}
}

0 comments on commit 9079ed3

Please sign in to comment.