2 using System
.Collections
.Generic
;
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
> ();
34 public static int Main ()
36 IEnumerable
<int> x
= Sequence
.Unfoldr (10, b
=> b
== 0
38 : KeyValuePair
.Just (b
, b
- 1));
41 foreach (int e
in x
) {
42 Console
.WriteLine (e
);