Exemple #1
0
        public void Match_Theory(IMaybe <int> m1)
        {
            // arrnage - theory
            // act
            var result = m1.Match(
                j =>
            {
                Assert.True(m1 is Just <int>);
                return(1);
            },
                () =>
            {
                Assert.True(m1 is Nothing <int>);
                return(2);
            }
                );

            if (m1 is Just <int> )
            {
                Assert.Equal(1, result);
            }
            if (m1 is Nothing <int> )
            {
                Assert.Equal(2, result);
            }
        }
Exemple #2
0
        private static void HandleCore <TSubject, TExpectation>(IMaybe <TSubject> subject, IMaybe <TExpectation> expectation, IEquivalencyValidationContext context, IEquivalencyValidator parent)
        {
            expectation.Match(none, some)();

            void none()
            {
                subject.Match(none: () => { },
                              some: _ => new Action(() => AssertionScope.Current.FailWith("Expected subject to be empty, but it was filled.")))();
            }

            void some(TExpectation e)
            {
                subject.Match(none: () => AssertionScope.Current.FailWith("Expected {context:subject} to be filled, but it was empty."),
                              some: (s) => new Action(() => parent.AssertEqualityUsing(context.CreateForMaybeValue(s, e))))();
            }
        }
Exemple #3
0
 public static IMaybe <TResult> Select <T, TResult>(
     this IMaybe <T> source,
     Func <T, TResult> selector) where TResult : class
 {
     return(source.Match <IMaybe <TResult> >(
                nothing: new Nothing <TResult>(),
                just: x => new Just <TResult>(selector(x))));
 }
Exemple #4
0
        /// <summary>
        /// Returns the value of the specified maybe, or the specified value if the maybe is empty.
        /// </summary>
        /// <typeparam name="T">The type of the object contained by <paramref name="source"/>.</typeparam>
        /// <param name="source">The maybe to return the specified value for.</param>
        /// <param name="none">The value to return if the maybe is empty.</param>
        /// <returns></returns>
        public static T Match <T>(this IMaybe <T> source, T none)
        {
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            return(source.Match(none: none, some: v => v));
        }
Exemple #5
0
        public static Action Match <T>(this IMaybe <T> source, Action none, Action <T> some)
        {
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            return(source.Match(none: none,
                                some: (v) => () => some(v)));
        }
Exemple #6
0
 public void Match_Void_Theory(IMaybe <int> m1)
 {
     m1.Match(
         just: _ => {
         Assert.True(m1 is Just <int>);
     },
         nothing: () => {
         Assert.True(m1 is Nothing <int>);
     }
         );
 }
Exemple #7
0
        public static IMaybe <Q> bind <P, Q>
            (this IMaybe <P> subject
            , Func <P, IMaybe <Q> > transform)
        {
            return(subject.Match(

                       has_value: transform,

                       nothing: () => new Nothing <Q>()
                       ));
        }
Exemple #8
0
        public static void Match <T>(this IMaybe <T> maybe, Action <T> has_value, Action nothing)
        {
            maybe
            .Match(

                has_value:
                value => { has_value(value); return(new Unit()); },

                nothing:
                () => { nothing(); return(new Unit()); }

                );
        }
Exemple #9
0
        public static IMaybe <TResult> Bind <T, TResult>(this IMaybe <T> source, Func <T, TResult> selector)
        {
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (selector is null)
            {
                throw new ArgumentNullException(nameof(selector));
            }

            return(source.Match <IMaybe <TResult> >(none: new None <TResult>(),
                                                    some: v => new Some <TResult>(selector(v))));
        }
Exemple #10
0
        /// <summary>
        /// The bind method for the <see cref="IMaybe{T}"/> monad, which transforms an <see cref="IMaybe{T}"/> into an <see cref="IMaybe{TResult}"/>.
        /// </summary>
        /// <param name="value">
        /// The <see cref="IMaybe{T}"/> value to transform.
        /// </param>
        /// <param name="transformation">
        /// The transformation function, which takes a value of type <typeparamref name="T"/> contained in the monad and transforms it
        /// into a new monad of type <see cref="IMaybe{TResult}"/>.
        /// </param>
        /// <typeparam name="T">
        /// The type of the value contained in the monad to transform.
        /// </typeparam>
        /// <typeparam name="TResult">
        /// The type of the value contained in the resulting monad.
        /// </typeparam>
        /// <returns>
        /// The <see cref="IMaybe{TResult}"/> which results from transforming <paramref name="value"/> using <paramref name="transformation"/>.
        /// </returns>
        public static IMaybe <TResult> Bind <T, TResult>(this IMaybe <T> value, Func <T, IMaybe <TResult> > transformation)
        {
            IMaybe <TResult> result = value.Match(v =>
            {
                IMaybe <TResult> intermediateResult = transformation(v);
                if (intermediateResult == null)
                {
                    throw new InvalidOperationException("The transformation function may not return null.");
                }

                return(intermediateResult);
            }, Maybe.Nothing <TResult>);

            if (result == null)
            {
                throw new InvalidOperationException("Result may not be null.");
            }

            return(result);
        }
Exemple #11
0
        /// <summary>
        ///     A timer that fires at the specified time.
        /// </summary>
        /// <param name="t">The time to fire at.</param>
        /// <returns>A stream which fires at the specified time.</returns>
        public Stream <T> At(Cell <IMaybe <T> > t)
        {
            StreamSink <T>  alarm        = new StreamSink <T>();
            IMaybe <ITimer> currentTimer = Maybe.Nothing <ITimer>();
            IListener       l            = t.Listen(m =>
            {
                currentTimer.Match(timer => timer.Cancel(), () => { });
                currentTimer = m.Match(
                    time => Maybe.Just(this.implementation.SetTimer(time, () =>
                {
                    lock (this.eventQueue)
                    {
                        this.eventQueue.Enqueue(new Event(time, alarm));
                    }
                    // Open and close a transaction to trigger queued
                    // events to run.
                    Transaction.RunVoid(() => { });
                })),
                    Maybe.Nothing <ITimer>);
            });

            return(alarm.AddCleanup(l));
        }
Exemple #12
0
 public static Io <IMaybe <T> > GetOrLog <T>(this IMaybe <T> m, Func <Io <Unit> > logger)
 {
     return(m.Match(
                just: v => Io.Apply(() => v.ToMaybe()),
                nothing: () => logger().Select(u => Nothing <T>())));
 }
Exemple #13
0
 public static IEither <TErr, TVal> AsEither <TErr, TVal>(this IMaybe <TVal> m, Func <TErr> error)
 {
     return(m.Match(
                just: val => val.AsRight <TErr, TVal>(),
                nothing: () => error().AsLeft <TErr, TVal>()));
 }
Exemple #14
0
 public static T GetOrError <T>(this IMaybe <T> m, Func <Exception> errorToThrow)
 {
     return(m.Match(
                just: val => val,
                nothing: () => { throw errorToThrow(); }));
 }
Exemple #15
0
 public static T GetOrElse <T>(this IMaybe <T> m, Func <T> defaultValue)
 {
     return(m.Match(
                just: arg => arg,
                nothing: defaultValue));
 }
Exemple #16
0
 public static IMaybe <TResult> SelectMany <TValue, TResult>(this IMaybe <TValue> m, Func <TValue, IMaybe <TResult> > f)
 {
     return(m.Match(
                just: f,
                nothing: Nothing <TResult>));
 }
Exemple #17
0
 public static IMaybe <T> Where <T>(this IMaybe <T> m, Func <T, bool> predicate)
 {
     return(m.Match(
                just: value => predicate(value) ? new Just <T>(value) : Nothing <T>(),
                nothing: Nothing <T>));
 }
Exemple #18
0
 public static bool IsJust <T>(this IMaybe <T> m)
 {
     return(m.Match <bool>(
                nothing: false,
                just: _ => true));
 }
Exemple #19
0
 public static IChurchBoolean IsNothing <T>(this IMaybe <T> m)
 => m.Match <IChurchBoolean>(
     nothing: new ChurchTrue(),
     just: _ => new ChurchFalse());
Exemple #20
0
 public static A GetOrElse <A>(this IMaybe <A> m, Func <A> f)
 => m.Match(Id, () => f());
Exemple #21
0
 public static Try <TVal> AsTry <TVal, TErr>(this IMaybe <TVal> m, Func <TErr> error) where TErr : Exception
 {
     return(m.Match(
                just: v => Try.Attempt(() => v),
                nothing: () => Try.Attempt <TVal>(() => { throw error(); })));
 }
Exemple #22
0
 public static A GetOrDefault <A>(this IMaybe <A> m, A a)
 => m.Match(Id, () => a);
Exemple #23
0
 public static IMaybe <T> Or <T>(this IMaybe <T> left, IMaybe <T> right)
 {
     return(left.Match(
                just: v => v.ToMaybe(),
                nothing: () => right));
 }
Exemple #24
0
 /// <summary>
 /// Helper that, given a potential value, will perform a side-effect if that value is not present
 /// </summary>
 /// <typeparam name="T">The type of value we potentially have</typeparam>
 /// <param name="m">A potential value</param>
 /// <param name="logger">A function that performs a side-effect</param>
 /// <returns>A value representing an effectual computation that is only useful for its side-effects</returns>
 public static Io <Unit> LogEmpty <T>(this IMaybe <T> m, Func <Io <Unit> > logger)
 {
     return(m.Match(
                just: _ => IoUnit,
                nothing: logger));
 }
Exemple #25
0
 /// <summary>
 /// Helper that, given a potential value, will perform a side-effect if that value is present, or
 /// a different side-effect if that value is not present.
 /// </summary>
 /// <typeparam name="T">The type of value we potentially have</typeparam>
 /// <param name="m">A potential value</param>
 /// <param name="justLogger">A function that performs a side-effect given a value of type 'T</param>
 /// <param name="nothingLogger">A function that performs a side-effect</param>
 /// <returns>A value representing an effectual computation that is only useful for its side-effects</returns>
 public static Io <Unit> Log <T>(this IMaybe <T> m, Func <T, Io <Unit> > justLogger, Func <Io <Unit> > nothingLogger)
 {
     return(m.Match(
                just: justLogger,
                nothing: nothingLogger));
 }
Exemple #26
0
 public static IResult <TVal, TErr> From <TVal, TErr>(IMaybe <TVal> maybe, TErr error) =>
 maybe.Match(val => new Ok <TVal, TErr>(val) as IResult <TVal, TErr>,
             () => new Err <TVal, TErr>(error) as IResult <TVal, TErr>);
Exemple #27
0
 public static bool IsNothing <T>(this IMaybe <T> m)
 {
     return(m.Match <bool>(
                nothing: true,
                just: _ => false));
 }