public TryMaybe <TResult> Bind <TResult>(Func <T, TryMaybe <TResult> > f)
 {
     return(new TryMaybe <TResult>(_self.Match(
                                       failure: ex => ex.Fail <IMaybe <TResult> >(),
                                       success: maybe => maybe.Match(
                                           just: val => f(val).Out(),
                                           nothing: () => Try.Attempt(() => Maybe.Nothing <TResult>())))));
 }
Exemple #2
0
        public void TestTryEnumerable()
        {
            var expected = new[] { 2, 3, 4, 5, 6 };
            var initital = new[] { 1, 2, 3, 4, 5 };
            var @try     = Try.Attempt(() => initital.AsEnumerable()).ToTryEnumerable();
            var temp     =
                (from num in @try
                 select num + 1)
                .Out();
            var result = temp.Match(
                success: BasicFunctions.Identity,
                failure: ex => Enumerable.Empty <int>());

            Assert.IsTrue(TestUtils.AreEqual(result, expected));
        }
Exemple #3
0
        public void TestStateTryEnumerable()
        {
            var expected = new[] { 2, 3, 4, 5, 6, 7 };
            var xs       = new[] { 1, 2, 3, 4, 5, 6 };
            var @try     = xs.Traverse(x => Try.Attempt(() => x));
            var state    = @try.Insert <int, Try <IEnumerable <int> > >();
            var initial  = state.ToStateTryEnumerable();
            var program  =
                (from x in initial
                 from addend in State.Get <int, int>(BasicFunctions.Identity).ToStateTryEnumerable()
                 select x + addend)
                .Out();

            var actual = program.Eval(1).Match(
                success: BasicFunctions.Identity,
                failure: exception => Enumerable.Empty <int>());

            Assert.IsTrue(TestUtils.AreEqual(expected, actual));
        }
 public IoTryMaybe(IMaybe <T> m)
     : this(Io.Apply(() => Try.Attempt(() => m)))
 {
 }
 public IoTryMaybe(T t)
     : this(Io.Apply(() => Try.Attempt(() => t.ToMaybe())))
 {
 }
Exemple #6
0
 public TryEnumerable(IEnumerable <T> enumerable)
     : this(Try.Attempt(() => enumerable))
 {
 }
Exemple #7
0
        public void Test1()
        {
            var xs = Io.Apply(() => Enumerable.Range(0, 10000).Select(n => n % 8).Select(n => Try.Attempt(() =>
            {
                if (n == 0)
                {
                    throw new Exception("Ruh roh");
                }
                return(n);
            }))).ToIoEnumerableTry();

            var pgm = (from x in xs
                       from _ in PutStrLn(x).ToIoEnumerableTry()
                       select x + 3).Out;

            var res = pgm.UnsafePerformIo();
        }
 public StateTryEnumerable(IEnumerable <TValue> enumerable)
     : this(Try.Attempt(() => enumerable))
 {
 }
 public StateTry(TValue t) : this(Try.Attempt(() => t))
 {
 }
Exemple #10
0
 /// <summary>
 /// Type-safe version of LINQs 'First' and 'FirstOrDefault' function that accepts a predicate, returning
 /// a Maybe instead of possibly throwing an exception or returning null
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="items">An enumerable from which we want the first element that satisfies the provided predicate</param>
 /// <param name="predicate">A predicate for which the first element to satisfy will be the return value</param>
 /// <returns>Just the first value in the enumerable that satisfies the predicate, or nothing if no such element exists</returns>
 public static IMaybe <T> MaybeFirst <T>(this IEnumerable <T> items, Func <T, bool> predicate)
 {
     return(Try.Attempt(() => typeof(T).IsPrimitive ? items.First(predicate).ToMaybe() : items.FirstOrDefault(predicate).ToMaybe()).GetOrElse(Maybe.Nothing <T>));
 }
Exemple #11
0
 public IoStateTry(TValue val)
     : this(Try.Attempt(() => val))
 {
 }
Exemple #12
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(); })));
 }
 public IoTryMaybe <TResult> Bind <TResult>(Func <T, IoTryMaybe <TResult> > f)
 {
     return(new IoTryMaybe <TResult>(Out.SelectMany(t => t.Match(
                                                        success: m => m.Match(
                                                            just: v => f(v).Out,
                                                            nothing: () => Io.Apply(() => Try.Attempt(() => Maybe.Nothing <TResult>()))),
                                                        failure: ex => Io.Apply(() => ex.Fail <IMaybe <TResult> >())))));
 }
Exemple #14
0
 /// <summary>
 /// A replacement for a 'hard' cast for value types that is a total function. Rather than throwing an expection on
 /// a failed cast, this will return a value indicating that we may or may not have been able to compute a result.
 /// </summary>
 /// <typeparam name="T">The type of value we think 'o' is</typeparam>
 /// <param name="o">An object we intend to attempt to prove is a T</param>
 /// <returns>Potentially a T (or nothing if we cannot prove at runtime that 'o' is a T)</returns>
 public static IMaybe <T> CastV <T>(this object o) where T : struct
 {
     return(Try.Attempt(() => (T)o).AsMaybe());
 }
Exemple #15
0
 /// <summary>
 /// Type-safe version of LINQs 'Single' and 'SingleOrDefault' function that accepts a predicate, returning
 /// a Maybe instead of possibly throwing an exception or returning null
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="items">A queryable from which we want the only element that satisfies the provided predicate</param>
 /// <param name="predicate">A predicate for which only one element will satisfy to be the return value</param>
 /// <returns>The only value in the queryable that satisfies the predicate, or nothing if no such element exists or multiple elements satisifying the predicate exist</returns>
 public static IMaybe <T> MaybeSingle <T>(this IQueryable <T> items, Expression <Func <T, bool> > predicate)
 {
     return(Try.Attempt(() => typeof(T).IsPrimitive ? items.Single(predicate).ToMaybe() : items.SingleOrDefault(predicate).ToMaybe()).GetOrElse(Maybe.Nothing <T>));
 }
 public TryMaybe(IMaybe <T> maybe) : this(Try.Attempt(() => maybe))
 {
 }
 public IoTry(T t) : this(Try.Attempt(() => t))
 {
 }
Exemple #18
0
 public IoEnumerableTry(Func <T> toAttempt) : this(Io.Apply(() => Try.Attempt(toAttempt).LiftEnumerable()))
 {
 }
 public ReaderTry(TValue value) : this(Try.Attempt(() => value))
 {
 }
Exemple #20
0
        /// <summary>
        /// Sequence takes a list of computations and builds from them a computation which will
        /// run each in turn and produce a list of the results.
        ///
        /// Note that due to C#s lack of higher kinded types, this must be specified for every type of computation
        /// This is the sequence for Try computations
        /// </summary>
        /// <typeparam name="T">Type of value yielded by each computation</typeparam>
        /// <param name="tries">The list of computations</param>
        /// <returns>A computation thqt yields a sequence of values of type 'T</returns>
        public static Try <IEnumerable <T> > Sequence <T>(this IEnumerable <Try <T> > tries)
        {
            var initial = Try.Attempt(ConsList.Nil <T>);

            return(tries.Reverse().Aggregate(initial, (current, aTry) => current.SelectMany(ts => aTry.Select(t => t.Cons(ts)))).Select(ts => ts.AsEnumerable()));
        }