"disable_omit_fp" can now be included in MONO_DEBUG
[mono-project.git] / mcs / tests / gtest-linq-20.cs
blob122a49304507630c95b846ebacb5805a0240935a
1 using System;
3 class Maybe<T>
5 public readonly static Maybe<T> Nothing = new Maybe<T> ();
6 public T Value { get; private set; }
7 public bool HasValue { get; private set; }
8 Maybe ()
10 HasValue = false;
12 public Maybe (T value)
14 Value = value;
15 HasValue = true;
18 public override string ToString ()
20 if (HasValue)
21 return Value.ToString ();
22 return string.Empty;
25 public Maybe<U> SelectMany<U> (Func<T, Maybe<U>> k)
27 if (!HasValue)
28 return Maybe<U>.Nothing;
29 return k (Value);
32 public Maybe<V> SelectMany<U, V> (
33 Func<T, Maybe<U>> selector,
34 Func<T, U, V> resultSelector)
36 if (!HasValue)
37 return Maybe<V>.Nothing;
38 Maybe<U> n = selector (Value);
39 if (!n.HasValue)
40 return Maybe<V>.Nothing;
41 return resultSelector (Value, n.Value).ToMaybe ();
45 static class MaybeExtensions
47 public static Maybe<T> ToMaybe<T> (this T value)
49 return new Maybe<T> (value);
53 class Test
55 public static void Main ()
57 Console.WriteLine (
58 from x in 1.ToMaybe ()
59 from y in 2.ToMaybe ()
60 from z in 3.ToMaybe ()
61 select x + y + z);