add ISafeSerializationData
[mcs.git] / tests / gtest-iter-06.cs
blob6c17278ff810891786f53a847129e58e724e8dc7
1 using System;
2 using System.Collections.Generic;
4 namespace Mono.Rocks
6 public static class KeyValuePair
8 public static KeyValuePair<TKey, TValue>? Just<TKey, TValue> (TKey key, TValue value)
10 return new KeyValuePair<TKey, TValue> (key, value);
14 public static class Sequence
16 public static IEnumerable<TResult> Unfoldr<TSource, TResult> (TSource value, Func<TSource, KeyValuePair<TResult, TSource>?> func)
18 return CreateUnfoldrIterator (value, func);
21 private static IEnumerable<TResult> CreateUnfoldrIterator<TSource, TResult> (TSource value, Func<TSource, KeyValuePair<TResult, TSource>?> func)
23 KeyValuePair<TResult, TSource>? r;
24 while ((r = func (value)).HasValue) {
25 KeyValuePair<TResult, TSource> v = r ?? new KeyValuePair<TResult, TSource> ();
26 yield return v.Key;
27 value = v.Value;
32 class Test
34 public static int Main ()
36 IEnumerable<int> x = Sequence.Unfoldr (10, b => b == 0
37 ? null
38 : KeyValuePair.Just (b, b - 1));
40 int i = 10;
41 foreach (int e in x) {
42 Console.WriteLine (e);
43 if (i-- != e)
44 return 1;
47 return 0;