Why doesn't it embed a method that is explicitly equivalent to a declaration?

Do you have a blind spot in programming?

I mean, is there a common technique or language feature that you can't get used to. Well, I have one (or maybe more than one), and mine is use delegate. Hands up! Who else does not feel comfortable with delegates? Be honest!

So what is a delegate?

Since my university courses introduced me to C, I know about function pointers. Function pointers are useful if you want to pass methods as arguments. Therefore, in my opinion, a delegate is something like a pointer to a function. Eureka! I understood. I do not have!

Specific scenario?

I would like to remove any line from a text file that matches the regular expression . Assuming I have a set of strings, there List<T>is a method RemoveAllthat seems to be perfect for this purpose.  RemoveAllexpects an evaluation method as an argument for deciding whether to remove or leave a list item. And here it is: a function pointer!

Any code here?

public static int RemoveLinesFromFile(string path, string pattern)
{
  List<string> lines = new List<string>(File.ReadAllLines(path));
  int result = lines.RemoveAll(DoesLineMatch);
  File.WriteAllLines(path, lines.ToArray());
  return result;
}

So, I'm looking for a function DoesLineMatchthat evaluates a string to match a pattern.

Do you see the problem?

RemoveAllexpects a delegate Predicate<string> matchas an argument. I would encode this as follows:

private static bool DoesLineMatch(string line, string pattern)
{
  return Regex.IsMatch(line, pattern);
}

But then I get the error message "Expected method with 'bool DoesLineMatch (string)' signature '. What am I missing here?

Does it work at all?

Here is how I finally got the job:

public static int RemoveLinesFromFile(string path, string pattern)
{
  List<string> lines = new List<string>(File.ReadAllLines(path));
  int result = lines.RemoveAll(delegate(string line)
    {
      return Regex.IsMatch(line, pattern);
    });
  File.WriteAllLines(path, lines.ToArray());
  return result;
}

, , .

?

, , , - .   , - ---destroy.   , , .

? ?

PS: , .

PPS: , 2.0 3.0 lambdas.

PPPS: Regex.IsMatch(string, string) :

  int result = lines.RemoveAll(delegate(string line)
    {
      Regex regex = new Regex(pattern);
      return regex.IsMatch(line);
    });

. ReSharper Regex :

  Regex regex = new Regex(pattern);
  int result = lines.RemoveAll(delegate(string line)
    {
      return regex.IsMatch(line);
    });

ReSharper :

  Regex regex = new Regex(pattern);
  int result = lines.RemoveAll(regex.IsMatch);

, . , , , ReSharper ( , ) .

+3
7

:

bool DoesLineMatch(string line, string pattern)

:

bool Predicate(string value)

() ?

:

public sealed class RegexHolder
{
    private readonly string pattern;

    public RegexHolder(string pattern)
    {
        this.pattern = pattern;
    }

    public bool DoesLineMatch(string line)
    {
        return Regex.IsMatch(line, pattern);
    }
}

:

public static int RemoveLinesFromFile(string path, string pattern)
{
    List<string> lines = new List<string>(File.ReadAllLines(path));
    RegexHolder holder = new RegexHolder(pattern);
    int result = lines.RemoveAll(holder.DoesLineMatch);
    File.WriteAllLines(path, lines.ToArray());
    return result;
}

, - (pattern ).

( , Regex.Match(string, string), Regex... .)

+8

, : , "" , . :

class Matcher {
    public string Pattern;
    bool IsMatch(string value){
       return Regex.IsMatch(Pattern, value);
    }
}

, .

public static int RemoveLinesFromFile(string path, string pattern)
{
  List<string> lines = new List<string>(File.ReadAllLines(path));
  Matcher matcher = new Matcher(pattern);
  int result = lines.RemoveAll(matcher.IsMatch);
  File.WriteAllLines(path, lines.ToArray());
  return result;
}

, . , . CS. , , .

, .

+2

, currying #:

public static class DelegateUtils
{
    public static Predicate<T> ToPredicate<T>(this Func<T, Boolean> func)
    {
        return value => func(value);
    }

    public static Func<TResult> Curry<T1, TResult>(
        this Func<T1, TResult> func, T1 firstValue)
    {
        return () => func(firstValue);
    }

    public static Func<T2, TResult> Curry<T1, T2, TResult>(
        this Func<T1, T2, TResult> func, T1 firstValue)
    {
        return p2 => func(firstValue, p2);
    }

    public static Func<T2, T3, TResult> Curry<T1, T2, T3, TResult>(
        this Func<T1, T2, T3, TResult> func, T1 firstValue)
    {
        return (p2, p3) => func(firstValue, p2, p3);
    }

    // if you need more, follow the examples
}

, , , , :

private static bool DoesLineMatch(string pattern, string line)
{
    return Regex.IsMatch(line, pattern);
}

currying , , :

Func<String, String, Boolean> func = DoesLineMatch;
Func<String, Boolean> predicateCandidate = func.Curry("yourPattern");
Predicate<String> predicate = predicateCandidate.ToPredicate();
lines.RemoveAll(predicate);

, :

lines.RemoveAll(new Func<String, String, Boolean>(DoesLineMatch)
    .Curry("yourPattern")
    .ToPredicate());
+2

# 2.0 , :

        int result = lines.RemoveAll( delegate (string s) {return DoesLineMatch(s, pattern);});
+1

, , - , C - , , , , , , 68.

C : . , C , , , , (void *) .

C .

+1

Wot Jon Says.

, # 3 , , pattern :

int result = lines.RemoveAll(l => DoesLineMatch(l, pattern));
0

:

bool DoesLineMatch(string line)
{
  return Regex.IsMatch(line, pattern);
}

Where template is a private variable in your class. But this is a little ugly, so you can declare an inline error and use closure for the template variable that is declared locally in your RemoveLinesFromFile method.

0
source

Source: https://habr.com/ru/post/1719084/


All Articles