1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
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;
}
}
}
|