private static int?GetTuitionNumber(IOption <string> tuitionNumber)
        {
            if (!tuitionNumber.IsDefined || tuitionNumber.Get() == "0")
            {
                return(null);
            }

            return(int.Parse(tuitionNumber.Get()));
        }
Example #2
0
        /// <summary>
        /// Build an expression from operators. * and / take precedence over +,-.
        /// </summary>
        /// <param name="t"></param>
        /// <param name="rest"></param>
        /// <returns></returns>
        private static IExpression BuildExpressionTree(IExpression t, IOption <IEnumerable <Tuple <string, IExpression> > > rest)
        {
            // Put everything into a single list for later processing. And take care of the simplest case.
            if (rest.IsEmpty)
            {
                return(t);
            }
            List <Tuple <string, IExpression> > lst = new List <Tuple <string, IExpression> >()
            {
                Tuple.Create("", t)
            };

            lst.AddRange(rest.Get());

            // Look for each set of operators in turn.
            CombineForOperators(lst, new string[] { "." }, (obj, opName, funcCall) => new MethodCallExpression(obj, funcCall as FunctionExpression));
            CombineForOperators(lst, new string[] { ":", "=>" }, (key, opName, val) => new DictionaryValue(new Tuple <IExpression, IExpression>[] { Tuple.Create(key, val) }));
            CombineForOperators(lst, new string[] { "*", "/" }, (left, opName, right) => new FunctionExpression(opName, new IExpression[] { left, right }));

            // Next, just combine the left overs. We could use Combine, but then we'd have to add funny logic to it.

            var lastexpr = lst[0].Item2;

            foreach (var item in lst.Skip(1))
            {
                lastexpr = new FunctionExpression(item.Item1, new IExpression[] { lastexpr, item.Item2 });
            }

            return(lastexpr);
        }
Example #3
0
 public static IEnumerable <T> ToArray <T>(this IOption <IEnumerable <T> > option)
 {
     if (!option.IsEmpty)
     {
         return(option.Get());
     }
     return(new T[0]);
 }
Example #4
0
 private ExpressionSyntax FlatIfEmptyOrNull(ExpressionSyntax exp, IOption <ExpressionSyntax> option, string op)
 {
     if (option.IsDefined)
     {
         return(new BinaryExpressionSyntax(exp, option.Get(), op));
     }
     return(exp);
 }
Example #5
0
 public static T GetOrDefault <T>(this IOption <T> option, Func <T> func)
 {
     if (!option.IsEmpty)
     {
         return(option.Get());
     }
     return(func());
 }
Example #6
0
 /// <summary>
 /// Gets the value or else returns a default value.
 /// </summary>
 /// <typeparam name="T">The result type.</typeparam>
 /// <param name="option"></param>
 /// <param name="defaultValue">The default value.</param>
 /// <returns></returns>
 public static T GetOrElse <T>(this IOption <T> option, T defaultValue)
 {
     if (option == null)
     {
         throw new ArgumentNullException("option");
     }
     return(option.IsEmpty ? defaultValue : option.Get());
 }
Example #7
0
 public static T?Unwrap <T>(this IOption <T> t) where T : struct
 {
     if (t.IsDefined)
     {
         return(t.Get());
     }
     return(null);
 }
Example #8
0
 public static Option <object> AsOption(this IOption value)
 {
     if (value == null || value.IsNone)
     {
         return(Option.None);
     }
     return(Option.GetSome(value.Get()));
 }
Example #9
0
 /// <summary>
 /// Maps a function over the value or else returns an empty option.
 /// </summary>
 /// <typeparam name="T">The input type.</typeparam>
 /// <typeparam name="U">The output type.</typeparam>
 /// <param name="option">The option containing the value to apply <paramref name="map" /> to.</param>
 /// <param name="map">The function to apply to the value of <paramref name="option" />.</param>
 /// <returns>An options result containing the result if there was an input value.</returns>
 public static IOption <U> Select <T, U>(this IOption <T> option, Func <T, U> map)
 {
     if (option == null)
     {
         throw new ArgumentNullException(nameof(option));
     }
     return(option.IsDefined ? (IOption <U>) new Some <U>(map(option.Get())) : new None <U>());
 }
Example #10
0
 public static IEnumerable <T> ToArray <T>(this IOption <T> option)
 {
     if (!option.IsEmpty)
     {
         return new T[] { option.Get() }
     }
     ;
     return(new T[0]);
 }
        private static int?FromOptionalNumber(IOption <string> number)
        {
            if (number.IsDefined)
            {
                return(int.Parse(number.Get()));
            }

            return(null);
        }
Example #12
0
        public static Option <T> ToOption <T>(this IOption <T> o)
        {
            if (o.IsDefined)
            {
                return(new Some <T>(o.Get()));
            }

            return(None <T> .Instance);
        }
        private static string GetStringOrNull(IOption <string> option)
        {
            if (option.IsDefined)
            {
                return(option.Get());
            }

            return(null);
        }
Example #14
0
        /// <summary>
        /// We have parsed an individual term, and perhaps a [] after it.
        /// </summary>
        /// <param name="t"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private static IExpression BuildTermFromParts(IExpression t, IOption <IExpression> e)
        {
            if (e.IsEmpty)
            {
                return(t);
            }

            return(new IndexerRefExpression(t, e.Get()));
        }
        private static string GetStringOrNull(IOption <string> value)
        {
            if (value.IsDefined)
            {
                return(value.Get());
            }

            return(null);
        }
        private static ExpandoObject GetExpandoObject(
            IOption<Member> member,
            IEnumerable<Member> rest)
        {
            var expandoObject = new ExpandoObject();
            var dictionary = (IDictionary<string, object>)expandoObject;

            if (member.IsDefined)
            {
                dictionary.Add(member.Get().Name, member.Get().Value);

                foreach (var m in rest)
                {
                    dictionary.Add(m.Name, m.Value);
                }
            }

            return expandoObject;
        }
        private static List <string> GetStringListOrEmptyList(IOption <string> option)
        {
            if (option.IsDefined)
            {
                return(new List <string> {
                    option.Get()
                });
            }

            return(new List <string>());
        }
Example #18
0
 public static Option <T> AsOption <T>(this IOption <T> value)
 {
     if (value is Option <T> )
     {
         return((Option <T>)value);
     }
     if (value == null || value.IsNone)
     {
         return(Option <T> .None);
     }
     return(Option.GetSome(value.Get()));
 }
Example #19
0
        /// <summary>
        ///     Create sequence that contains the optional item if it is defined.
        /// </summary>
        /// <typeparam name="T">
        ///     The item type.
        /// </typeparam>
        /// <param name="optionalItem">
        ///     The optional item.
        /// </param>
        /// <returns>
        ///     If <see cref="IOption{T}.IsDefined"/> is <c>true</c>, a single-element sequence containing the item; otherwise, <c>false</c>.
        /// </returns>
        private static IEnumerable <T> ToSequenceIfDefined <T>(this IOption <T> optionalItem)
        {
            if (optionalItem == null)
            {
                throw new ArgumentNullException(nameof(optionalItem));
            }

            if (optionalItem.IsDefined)
            {
                yield return(optionalItem.Get());
            }
        }
Example #20
0
        private static DayInformation CreateDay(DateTime dateTime, IOption <DayInformation> additionalInfo)
        {
            var day = new DayInformation();

            if (additionalInfo.IsDefined)
            {
                day = additionalInfo.Get();
            }

            day.Date = dateTime;

            return(day);
        }
Example #21
0
        /// <summary>
        /// Binds the value to a function with optional result and flattens the result to a single optional.
        /// A result projection is applied aftherwards.
        /// </summary>
        /// <typeparam name="T">The input type.</typeparam>
        /// <typeparam name="U">The output type of <paramref name="bind" />.</typeparam>
        /// <typeparam name="V">The final output type.</typeparam>
        /// <param name="option">The option containing the value to bind to.</param>
        /// <param name="bind">The function that receives the input values and returns an optional value.</param>
        /// <param name="project">The function that is projects the result of <paramref name="bind" />.</param>
        /// <returns>An option result containing the result if there were was an input value and bind result.</returns>
        public static IOption <V> SelectMany <T, U, V>(this IOption <T> option, Func <T, IOption <U> > bind, Func <T, U, V> project)
        {
            if (option == null)
            {
                throw new ArgumentNullException(nameof(option));
            }
            if (option.IsEmpty)
            {
                return(new None <V>());
            }

            var t = option.Get();

            return(bind(t).Select(u => project(t, u)));
        }
Example #22
0
        private static string GetStringOrNull(IOption <string> option)
        {
            if (option.IsDefined)
            {
                var value = option.Get();

                if (string.IsNullOrEmpty(value))
                {
                    return(null);
                }

                return(value);
            }

            return(null);
        }
Example #23
0
        /// <summary>
        ///     Create a sequence that contains the optional items if they defined.
        /// </summary>
        /// <typeparam name="T">
        ///     The item type.
        /// </typeparam>
        /// <param name="optionalItems">
        ///     The optional items.
        /// </param>
        /// <returns>
        ///     If <see cref="IOption{T}.IsDefined"/> is <c>true</c>, a single-element sequence containing the item; otherwise, an empty sequence.
        /// </returns>
        private static IEnumerable <T> ToSequenceIfDefined <T>(this IOption <IEnumerable <T> > optionalItems)
        {
            if (optionalItems == null)
            {
                throw new ArgumentNullException(nameof(optionalItems));
            }

            if (!optionalItems.IsDefined)
            {
                yield break;
            }

            foreach (T item in optionalItems.Get())
            {
                yield return(item);
            }
        }
Example #24
0
        private static HolidayType GetType(IOption <IEnumerable <char> > type)
        {
            if (!type.IsDefined)
            {
                return(HolidayType.Ferien);
            }

            var typeString = new string(type.Get().ToArray());

            switch (typeString)
            {
            case "F":
                return(HolidayType.Feiertag);
            }

            return(HolidayType.Ferien);
        }
        private static ExpandoObject GetExpandoObject(IOption<Member> firstMember, IEnumerable<Member> otherMembers)
        {
            var expandoObject = new ExpandoObject();
            var d = (IDictionary<string, object>)expandoObject;

            if (firstMember.IsDefined)
            {
                var member = firstMember.Get();
                d.Add(member.Name, member.Value);
            }

            foreach (var member in otherMembers)
            {
                d.Add(member.Name, member.Value);
            }

            return expandoObject;
        }
Example #26
0
        internal TokenInteger(IOption<IEnumerable<char>> minus, string number)
        {
            StringBuilder sb = new StringBuilder();
            if (minus.IsDefined && !minus.IsEmpty)
            {
                foreach (char c in minus.Get())
                {
                    sb.Append(c);
                }
            }

            sb.Append(number);

            Int64 v;
            if (!Int64.TryParse(sb.ToString(), out v))
            {
                throw new ArgumentException(string.Format("Bad Int64 format: {0}", sb));
            }
            Value = v;
        }
Example #27
0
        private static DayType FromType(IOption <IEnumerable <char> > type)
        {
            if (!type.IsDefined)
            {
                return(DayType.Normal);
            }

            var typeString = new string(type.Get().ToArray());

            switch (typeString)
            {
            case "U":
                return(DayType.Unterrichtsfrei);

            case "F":
                return(DayType.Feiertag);
            }

            return(DayType.Normal);
        }
Example #28
0
        public TokenFloat(IOption<IEnumerable<char>> minus, string number1, string number2)
        {
            StringBuilder sb = new StringBuilder();
            if (minus.IsDefined && !minus.IsEmpty)
            {
                foreach (char c in minus.Get())
                {
                    sb.Append(c);
                }
            }

            sb.Append(number1).Append('.').Append(number2);

            double f;
            if (!Double.TryParse(sb.ToString(), out f))
            {
                throw new ArgumentException(string.Format("Bad float format: {0}", sb));
            }
            Value = f;
        }
Example #29
0
 public static T GetOrElse <T> (this IOption <T> opt, Func <T> defaultValue)
 {
     return(opt.IsEmpty() ? defaultValue() : opt.Get());
 }
Example #30
0
 private static T?ToNullable <T>(IOption <T> opt) where T : struct =>
 opt.IsDefined ? opt.Get() : (T?)null;
Example #31
0
 private void AssertSome <T>(IOption <T> option, T expected) => Assert.True(option.IsDefined && option.Get().Equals(expected));
 private static Pattern DependingPattern(string depending, IOption <string> optionalException)
 {
     return(optionalException.IsDefined ? Pattern.WithExclusion(depending, optionalException.Get()) : Pattern.WithoutExclusion(depending));
 }
        private static object[] GetObjectArray(IOption<object> item, IEnumerable<object> rest)
        {
            if (!item.IsDefined)
            {
                return new object[0];
            }

            return new[] { item.Get() }.Concat(rest).ToArray();
        }
Example #34
0
 public static T?GetAssigned <T>(this IOption <T> @this) where T : struct => @this.IsDefined ? @this.Get() : (T?)null;
Example #35
0
        /// <summary>
        /// Creates a query tree for the given query elements
        /// </summary>
        /// <param name="match"><see cref="MatchNode"/> for matching log items</param>
        /// <param name="parser"><see cref="IParser"/> for parsing log items</param>
        /// <param name="filter">An <see cref="Expression"/> for filtering log items</param>
        /// <param name="grouping">An enumerable of tuples of group names and grouping <see cref="Expression"/>s for grouping the log items</param>
        /// <param name="selects">An enumerable of tuples of projection labels and <see cref="Expression"/>s for projecting log items</param>
        /// <param name="order">An enumerable of tuples indicating sorting fields and sort order</param>
        /// <param name="limit">The number of items to return</param>
        /// <returns>A query tree for executing the query</returns>
        private static Node CreateQueryTree(
            MatchNode match,
            IOption <Tuple <string, IParser> > parser,
            IOption <Expression> filter,
            IOption <IEnumerable <Tuple <string, Expression> > > grouping,
            IOption <IEnumerable <Tuple <string, IAggregate> > > selects,
            IOption <IEnumerable <Tuple <string, bool> > > order,
            IOption <int> limit)
        {
            Node query = match;

            if (!parser.IsEmpty)
            {
                query = new ParseNode(query, parser.Get().Item2, parser.Get().Item1);
            }

            if (!filter.IsEmpty)
            {
                query = new PredicateNode(query, filter.Get());
            }

            if (!grouping.IsEmpty)
            {
                var keyNames       = grouping.Get().Select(g => g.Item1).ToArray();
                var keyExpressions = grouping.Get().Select(g => g.Item2).ToArray();
                if (selects.IsEmpty)
                {
                    query = new GroupByNode(query, keyNames, keyExpressions, new string[0], new IAggregate[0]);
                }
                else
                {
                    var aggregateNames = selects.Get().Select(s => s.Item1).ToArray();
                    var aggregates     = selects.Get().Select(s => s.Item2).ToArray();
                    query = new GroupByNode(query, keyNames, keyExpressions, aggregateNames, aggregates);
                }
            }
            else if (!selects.IsEmpty)
            {
                var aggregateNames = selects.Get().Select(s => s.Item1).ToArray();
                var aggregates     = selects.Get().Select(s => s.Item2).ToArray();
                if (aggregates.All(a => a is ListAggregate))
                {
                    query = new ProjectNode(query, aggregateNames, aggregates.Cast <ListAggregate>().Select(a => a.Expression).ToArray());
                }
                else
                {
                    query = new AggregateNode(query, aggregateNames, aggregates);
                }
            }

            if (!order.IsEmpty)
            {
                query = new OrderByNode(query, order.Get().Select(a => a.Item1).ToArray(), order.Get().Select(a => a.Item2).ToArray());
            }

            if (!limit.IsEmpty)
            {
                query = new LimitNode(query, limit.Get());
            }

            return(query);
        }
        private static object[] GetObjectArray(IOption<object> firstItem, IEnumerable<object> otherItems)
        {
            var list = new List<object>();

            if (firstItem.IsDefined)
            {
                list.Add(firstItem.Get());
            }

            foreach (var item in otherItems)
            {
                list.Add(item);
            }

            return list.ToArray();
        }