Exemple #1
0
 public static OptionStrict <D> M <A, B, C, D>(OptionStrict <A> ma, OptionStrict <B> mb, OptionStrict <C> mc, Func <A, B, C, D> liftFn)
 {
     return(from a in ma
            from b in mb
            from c in mc
            select liftFn(a, b, c));
 }
        public static OptionStrict <V> SelectMany <T, U, V>(
            this OptionStrict <T> self,
            Func <T, OptionStrict <U> > select,
            Func <T, U, V> project
            )
        {
            if (select == null)
            {
                throw new ArgumentNullException("select");
            }
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }

            if (!self.HasValue)
            {
                return(OptionStrict <V> .Nothing);
            }

            var res = select(self.Value);

            if (!res.HasValue)
            {
                return(OptionStrict <V> .Nothing);
            }

            return(new JustStrict <V>(project(self.Value, res.Value)));
        }
Exemple #3
0
 /// <summary>
 /// Monadic append
 /// If the lhs or rhs are in a Nothing state then Nothing propagates
 /// </summary>
 public override OptionStrict <T> Mappend(OptionStrict <T> rhs)
 {
     if (rhs == null)
     {
         throw new ArgumentNullException("rhs");
     }
     return(this);
 }
 public static OptionStrict <R> Select <T, R>(this OptionStrict <T> self, Func <T, R> map)
 {
     if (map == null)
     {
         throw new ArgumentNullException("map");
     }
     return(self.HasValue
         ? map(self.Value).ToOptionStrict()
         : OptionStrict <R> .Nothing);
 }
        public static OptionStrict <U> SelectMany <T, U>(this OptionStrict <T> self, Func <T, OptionStrict <U> > k)
        {
            if (k == null)
            {
                throw new ArgumentNullException("k");
            }

            return(self.HasValue
                ? k(self.Value)
                : OptionStrict <U> .Nothing);
        }
Exemple #6
0
        /// <summary>
        /// Monadic append
        /// If the lhs or rhs are in a Nothing state then Nothing propagates
        /// </summary>
        public override OptionStrict <T> Mappend(OptionStrict <T> rhs)
        {
            if (rhs == null)
            {
                throw new ArgumentNullException("rhs");
            }
            if (!rhs.HasValue)
            {
                return(rhs);
            }
            else
            {
                if (IsAppendable)
                {
                    var lhs = this.Value as IAppendable <T>;
                    return(new JustStrict <T>(lhs.Append(rhs.Value)));
                }
                else
                {
                    // TODO: Consider replacing this with a Reflection.Emit which does this job efficiently.
                    switch (TypeOfT)
                    {
                    case "System.Int64":
                        return(new JustStrict <T>((T)Convert.ChangeType((Convert.ToInt64(Value) + Convert.ToInt64(rhs.Value)), typeof(T))));

                    case "System.UInt64":
                        return(new JustStrict <T>((T)Convert.ChangeType((Convert.ToUInt64(Value) + Convert.ToUInt64(rhs.Value)), typeof(T))));

                    case "System.Int32":
                        return(new JustStrict <T>((T)Convert.ChangeType((Convert.ToInt32(Value) + Convert.ToInt32(rhs.Value)), typeof(T))));

                    case "System.UInt32":
                        return(new JustStrict <T>((T)Convert.ChangeType((Convert.ToUInt32(Value) + Convert.ToUInt32(rhs.Value)), typeof(T))));

                    case "System.Int16":
                        return(new JustStrict <T>((T)Convert.ChangeType((Convert.ToInt16(Value) + Convert.ToInt16(rhs.Value)), typeof(T))));

                    case "System.UInt16":
                        return(new JustStrict <T>((T)Convert.ChangeType((Convert.ToUInt16(Value) + Convert.ToUInt16(rhs.Value)), typeof(T))));

                    case "System.Decimal":
                        return(new JustStrict <T>((T)Convert.ChangeType((Convert.ToDecimal(Value) + Convert.ToDecimal(rhs.Value)), typeof(T))));

                    case "System.Double":
                        return(new JustStrict <T>((T)Convert.ChangeType((Convert.ToDouble(Value) + Convert.ToDouble(rhs.Value)), typeof(T))));

                    case "System.Single":
                        return(new JustStrict <T>((T)Convert.ChangeType((Convert.ToSingle(Value) + Convert.ToSingle(rhs.Value)), typeof(T))));

                    case "System.Char":
                        return(new JustStrict <T>((T)Convert.ChangeType((Convert.ToChar(Value) + Convert.ToChar(rhs.Value)), typeof(T))));

                    case "System.Byte":
                        return(new JustStrict <T>((T)Convert.ChangeType((Convert.ToByte(Value) + Convert.ToByte(rhs.Value)), typeof(T))));

                    case "System.String":
                        return(new JustStrict <T>((T)Convert.ChangeType((Convert.ToString(Value) + Convert.ToString(rhs.Value)), typeof(T))));

                    default:
                        throw new InvalidOperationException("Type " + typeof(T).Name + " is not appendable.  Consider implementing the IAppendable interface.");
                    }
                }
            }
        }
 /// <summary>
 /// Equals override
 /// Compares the underlying values
 /// </summary>
 public bool Equals(OptionStrict <T> rhs)
 {
     return(Equals((object)rhs));
 }
 /// <summary>
 /// Monadic append
 /// If the lhs or rhs are in a Nothing state then Nothing propagates
 /// </summary>
 public abstract OptionStrict <T> Mappend(OptionStrict <T> rhs);
Exemple #9
0
 public static OptionStrict <U> M <T, U>(OptionStrict <T> m, Func <T, U> liftFn)
 {
     return(from v in m select liftFn(v));
 }
Exemple #10
0
 public static OptionStrict <C> M <A, B, C>(OptionStrict <A> ma, OptionStrict <B> mb, Func <A, B, C> liftFn)
 {
     return(from a in ma
            from b in mb
            select liftFn(a, b));
 }