Exemplo n.º 1
0
        public Option <TResult> Match(TValue value)
        {
            if (value == null)
            {
                return(Option.None <TResult>());
            }

            var arr           = (TElem[])(object)value;
            var actualCount   = arr.Length;
            var expectedCount = _func.Method.GetParameters().Length;

            if (actualCount < expectedCount - 1)
            {
                return(Option.None <TResult>());
            }

            var skip    = typeof(Enumerable).GetMethod("Skip").MakeGenericMethod(typeof(TElem));
            var toArray = typeof(Enumerable).GetMethod("ToArray").MakeGenericMethod(typeof(TElem));

            var argExprs = arr.Take(expectedCount - 1).Select(x => Expression.Constant(x)).Cast <Expression>().ToList();

            argExprs.Add(
                Expression.Call(
                    null,
                    toArray,
                    Expression.Call(null, skip, Expression.Constant(arr), Expression.Constant(expectedCount - 1))
                    )
                );

            return(MatchCaseHelper.InvokeWithArgs <TResult>(_func, argExprs));
        }
Exemplo n.º 2
0
        public Option <TResult> Match(TValue value)
        {
            if (value == null)
            {
                return(Option.None <TResult>());
            }

            var seq           = (IEnumerable <TElem>)value;
            var expectedCount = _func.Method.GetParameters().Length;

            var args = new List <ConstantExpression>();

            foreach (var elem in seq)
            {
                args.Add(Expression.Constant(elem));
                if (args.Count > expectedCount)
                {
                    return(Option.None <TResult>());
                }
            }

            if (args.Count < expectedCount)
            {
                return(Option.None <TResult>());
            }

            return(MatchCaseHelper.InvokeWithArgs <TResult>(_func, args));
        }
Exemplo n.º 3
0
        public Option <TResult> Match(TValue value)
        {
            var type = typeof(TValue);

            if (value == null || !type.IsGenericType)
            {
                return(Option.None <TResult>());
            }

            var typeName = type.GetGenericTypeDefinition().FullName;

            if (!typeName.StartsWith("System.Tuple`") && !typeName.StartsWith("System.ValueTuple`"))
            {
                return(Option.None <TResult>());
            }

            var argCount   = _func.Method.GetParameters().Length;
            var valueCount = type.GetGenericArguments().Length;

            if (argCount != valueCount)
            {
                return(Option.None <TResult>());
            }

            var tupleExpr = Expression.Constant(value);
            var argExprs  = Enumerable.Range(1, argCount).Select(x => Expression.PropertyOrField(tupleExpr, "Item" + x));

            return(MatchCaseHelper.InvokeWithArgs <TResult>(_func, argExprs));
        }
Exemplo n.º 4
0
        public Option <TResult> Match(TValue value)
        {
            if (value == null)
            {
                return(Option.None <TResult>());
            }

            var seq           = (IEnumerable <TElem>)value;
            var expectedCount = _func.Method.GetParameters().Length;

            var args = new List <Expression>();

            using (var iter = seq.GetEnumerator())
            {
                for (var i = 0; i < expectedCount - 1; i++)
                {
                    if (!iter.MoveNext())
                    {
                        return(Option.None <TResult>());
                    }

                    args.Add(Expression.Constant(iter.Current));
                }
            }

            var skip = typeof(Enumerable).GetMethod("Skip").MakeGenericMethod(typeof(TElem));

            args.Add(
                Expression.Call(null, skip, Expression.Constant(seq), Expression.Constant(expectedCount - 1))
                );

            return(MatchCaseHelper.InvokeWithArgs <TResult>(_func, args));
        }
Exemplo n.º 5
0
        public Option <TResult> Match(string value)
        {
            var match = _regex.Match(value);

            if (!match.Success)
            {
                return(Option.None <TResult>());
            }

            var argCount = _func.Method.GetParameters().Length;

            if (argCount > match.Groups.Count)
            {
                return(Option.None <TResult>());
            }

            var argExprs = Enumerable.Range(0, argCount).Select(x => Expression.Constant(match.Groups[x].Value));

            return(MatchCaseHelper.InvokeWithArgs <TResult>(_func, argExprs));
        }
Exemplo n.º 6
0
        public Option <TResult> Match(TValue value)
        {
            if (value == null)
            {
                return(Option.None <TResult>());
            }

            var arr           = (TElem[])(object)value;
            var actualCount   = arr.Length;
            var expectedCount = _func.Method.GetParameters().Length;

            if (actualCount != expectedCount)
            {
                return(Option.None <TResult>());
            }

            var args = arr.Select(x => Expression.Constant(x));

            return(MatchCaseHelper.InvokeWithArgs <TResult>(_func, args));
        }