-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Tuple2 and extension methods for Tuple2 and IEnumerable of Tuple2
- Loading branch information
unknown
authored and
unknown
committed
Feb 13, 2010
1 parent
204eff3
commit 9079ed3
Showing
8 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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++; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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++)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} | ||
} |