Just come across the following line of code and having a hard time finding documentation for it, is it a lambda expression
? What does this do?
temp = Regex.Replace(url, REGEX_COOKIE_REPLACE,match => cookie.Values[match.Groups["CookieVar"].Value]);
Specifically interested in the =>
.
What is a Lambda Function in Python? A lambda function is an anonymous function (i.e., defined without a name) that can take any number of arguments but, unlike normal functions, evaluates and returns only one expression. A lambda function in Python has the following syntax: lambda parameters: expression.
C++ Lambda expression allows us to define anonymous function objects (functors) which can either be used inline or passed as an argument. Lambda expression was introduced in C++11 for creating anonymous functors in a more convenient and concise way.
In lambda expressions, the lambda operator => separates the input parameters on the left side from the lambda body on the right side. The following example uses the LINQ feature with method syntax to demonstrate the usage of lambda expressions: C# Copy.
Lambda expressions basically express instances of functional interfaces (An interface with single abstract method is called functional interface. An example is java.lang.Runnable).
If you look at the documentation for Replace, the 3rd argument is a MatchEvaluator
:
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator.aspx
This is a delegate that takes a Match
as an argument and returns the string to replace it with. Your code is defining a MatchEvaluator
using a lambda expression:
match => cookie.Values[match.Groups["CookieVar"].Value]
Here, for each match that the Regex finds, a value is being looked up in the cookie.Values
dictionary and the result is being used as the replacement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With