linux-packaging-mono/mcs/tests/gtest-iter-06.cs
Jo Shields a575963da9 Imported Upstream version 3.6.0
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
2014-08-13 10:39:27 +01:00

51 lines
1.1 KiB
C#

using System;
using System.Collections.Generic;
namespace Mono.Rocks
{
public static class KeyValuePair
{
public static KeyValuePair<TKey, TValue>? Just<TKey, TValue> (TKey key, TValue value)
{
return new KeyValuePair<TKey, TValue> (key, value);
}
}
public static class Sequence
{
public static IEnumerable<TResult> Unfoldr<TSource, TResult> (TSource value, Func<TSource, KeyValuePair<TResult, TSource>?> func)
{
return CreateUnfoldrIterator (value, func);
}
private static IEnumerable<TResult> CreateUnfoldrIterator<TSource, TResult> (TSource value, Func<TSource, KeyValuePair<TResult, TSource>?> func)
{
KeyValuePair<TResult, TSource>? r;
while ((r = func (value)).HasValue) {
KeyValuePair<TResult, TSource> v = r ?? new KeyValuePair<TResult, TSource> ();
yield return v.Key;
value = v.Value;
}
}
}
class Test
{
public static int Main ()
{
IEnumerable<int> x = Sequence.Unfoldr (10, b => b == 0
? null
: KeyValuePair.Just (b, b - 1));
int i = 10;
foreach (int e in x) {
Console.WriteLine (e);
if (i-- != e)
return 1;
}
return 0;
}
}
}