c# - Can someone break this lambda expression down for me? -


i'm looking @ solution token replacement , identification:

string result = regex.replace(     text,     @"\[%rc:(\d+)%\]",     match => dict[int.parse(match.groups[1].value)]); 

and i'm not understanding how matchevaluator overloaded.

i understand of lambda expression. takes input match , looks integer dictionary?

but value match come from? , value returned match => dict[int.parse(match.groups[1].value)]); go?

edit: of have mentioned delegate. surprisingly after 3 years in university cs, haven't come across term. delegate , in specific situation?

last edit: tried writing own delegate following code tokens in form of [@sometokenname]

public void createscript(dictionary<string,string> dictionary, string path)     {         //used in regex identify string replace         var pattern = @"\[@[0-9a-fa-f]+\]";         //read in file found @ path         var lines = file.readalllines(path);         int linecounter = 0;         foreach (string line in lines)         {             line = regex.replace(line, pattern, match => dictionary.trygetvalue((match.groups[0].value)));         } 

but keep getting `cannot convert lambda expression type 'int' because not delegate type. what's difference between line wrote , 1 found in solution?

this regular expression matched lambda, bit more complicated regular old lambda. here go:

given:

string result = regex.replace(     text,     @"\[%rc:(\d+)%\]",     match => dict[int.parse(match.groups[1].value)]); 

(1) text - text we're searching through.

(2) @"\[%rc:(\d+)%\]" - means find looks "[%rc:{number}%]" {number} number (since \d means "a number", , \d+ means "one or more numbers in succession"). notice {number} or \d+ surrounded ( ) in (\d+). important because means number "group", has bearing our explanation below. groups way of extracting 'useful' part regular expression. is, don't want whole match, just number value.

(3) when finds match, executes this: match => dict[int.parse(match.groups[1].value)]);

let's start part: match => ..., same as:

public string matcheval(match match) {  } 

remember lambda expression short hand regular function (except compiler infers type match , it's return type based on delegate it's standing in - here matchevaluator - more on in moment). here, input match passed lambda expression input. have => begins function body similar { } see in our matcheval function above. result, each time match found, code equivalent block run:

public string matcheval(match match) {     // here grab value group (1) (the number in parentasis our regex)     return dict[int.parse(match.groups[1].value)]; } 

in short, remember lambda shorthand notation function. if @ documentation regex.replace, you'll see lambda standing in matchevaluator defined as:

public delegate string matchevaluator(match match); 

which lines our function expansion above. in fact, write:

string result = regex.replace(     text,     @"\[%rc:(\d+)%\]",     matcheval); 

(assuming dict accessible separate method) , function work same proving lambda shorter notation complete function.

edit: second part of question, "what delegate", delegate solves problem: "i don't know function want use, know signature has". consider:

// allows point math function signature, // namely, takes 2 int32 inputs, , returns int32. public static delegate int32 mathdelegate(int32 lhs, int32 rhs);  public static int32 add(int32 lhs, int32 rhs) {     return lhs + rhs; }  // note variable names aren't important, type public static int32 subtract(int32 a, int32 b) {     return - b; }  static void main() {     // can use delegate point "real" function     mathdelegate mathperformer = add;      console.writeline(mathperformer(2, 3)); // output : 5      // let's point "subtract"     mathperformer = subtract;      console.writeline(mathperformer(2, 3)); // output : -1      console.readline(); } 

this useful when don't know specific algorithm, or processing technique want use until the program running. delegate let's pick function want point to, , can execute while program running.

the way relates lambda discussion above, matchevaluator doesn't know how handle each of matches finds peruses through string. instead, supplying lambda/function telling algorithm want use when match found. delegate useful determining @ runtime how want perform action.

edit: if want expand lambda expressions include more 1 'line' of code, can use code block well. consider:

string result = regex.replace(     text,     @"\[%rc:(\d+)%\]",     match => {         return dict[int.parse(match.groups[1].value)]     }); 

you'll notice 2 things different here. (1) our => followed { } allows put in multiple lines of code. consequence though, compiler doesn't know value return value, , can't infer return type is. therefore, (2) insert explicit return command indicate value 1 should returned.

with simple code base, like:

string result = regex.replace(     text,     @"\[%rc:(\d+)%\]",     match => {         // same thing, more lines of code.        // of course, way more fancy well.        string numericvalueasstring = match.groups[1].value;        int32 numericvalue = int32.parse(numericvalueasstring);        string dictionaryvalue = dict[numericvalue];         // same above        return dictionaryvalue;     }); 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -