An error occurs in lambda expression

patrick

Well-known member
Joined
Dec 5, 2021
Messages
305
Programming Experience
1-3
hello

this is Ramda Code..

An error occurs in lambda expression.

Please Help me

C#:
 public class Service : IService
    {
        private readonly Func<string, string> SelectTarget;

        private class RequestTypeParser
        {
            public readonly Func<string, RequestType> Parse;

            public RequestTypeParser(Func<string, Func<string, bool>> regexCreator)
            {
                var IsOld = regexCreator(@"A\d{8}");
                var IsNew = regexCreator(@"B\d{4}");

                Parse = (Number) =>
                {
                    RequestType type = RequestType.Invalid;

                    if (IsOld(Number))
                        type = RequestType.Old;
                    else if (IsNew(Number))
                        type = RequestType.New;

                    return type;
                };
            }
        }

        private enum RequestType
        {
            Old,
            New
        }


        public Service()
        {
            RequestTypeParser parser = new RequestTypeParser
                ((pattern) =>
                {
                    Regex regex = new Regex(pattern);
                    return (input) => regex.IsMatch(input);
                });

            SelectTarget = (string Number) => parser.Parse(Number); <==== Error Error Error ㅠㅠ
        }
}
 
Last edited:
That said, there does seem to be one fairly obvious discrepancy. You declare your delegate variable here:
C#:
private readonly Func<string, string> SelectTarget;
You have declared it as type Func<string, string>, which means that the referenced function should accept a string argument and return a string. Here:
C#:
SelectTarget = (string Number) => parser.Parse(Number);
you assign a lambda to that variable that accepts a string as an argument and returns the result of parser.Parse. Here's that function:
C#:
Parse = (Number) =>
{
    RequestType type = RequestType.Invalid;

    if (IsOld(Number))
        type = RequestType.Old;
    else if (IsNew(Number))
        type = RequestType.New;

    return type;
};
It looks to me rather like that returns a RequestType and not a string.
 
Based on @patrick 's other thread, I think that line was meant to be:
C#:
SelectTarget = (string Number) => parser.Parse(Number) == RequestType.New ? "SO" : "YS";

But also based on his other threads, he tends to do things the most convoluted way possible, along with mysterious "requirements" that he never states, or arbitrary "requirements" which don't seem to make sense.
 
I don't understand why there is even a need for all the lambda expressions, nor the need for the inversion of control just to match a regular expression.

Something like this seems much more straightforward:
C#:
public class Service : IService
{
    enum RequestType
    {
        Invalid,
        Old,
        New
    }

    class RequestTypeParser
    {
        static readonly Dictionary<RequestType, string> _requestPatternMap = new Dictionary<RequestType, string>
        {
            [RequestType.Old] = @"A\d{8}",
            [RequestType.New] = @"B\d{4}"
        }

        public RequestType Parse(string number)
        {
            _requestPatternMap.Keys
                              .Where(key => Regex.IsMatch(number, _requestPatternMap[key]))
                              .FirstOrDefault();
        }
    }

    string SelectTarget(string number)
    {
        var parser = new RequestTypeParser();
        return parser.Parse(number) == RequestType.New ? "SO" : "YS";
    }
}
 
And trimming away the single method class which doesn't seem to really encapsulate anything:
C#:
public class Service : IService
{
    enum RequestType
    {
        Invalid,
        Old,
        New
    }

    static readonly Dictionary<RequestType, string> _requestPatternMap = new Dictionary<RequestType, string>
    {
        [RequestType.Old] = @"A\d{8}",
        [RequestType.New] = @"B\d{4}"
    }

    RequestType Parse(string number)
    {
        _requestPatternMap.Keys
                            .Where(key => Regex.IsMatch(number, _requestPatternMap[key]))
                            .FirstOrDefault();
    }

    string SelectTarget(string number)
        => Parse(number) == RequestType.New ? "SO" : "YS";
}
 

Latest posts

Back
Top Bottom