C
C#15mo ago
J.

❔ Lambda expressions in C#

Hi, I'm afraid I'm not used to lambdas enough and am struggling whenever they pop up. I need a place to practice lambda expressions (writing my own, as well as using existing ones). I searched on google but w3schools only has py and java examples
8 Replies
Angius
Angius15mo ago
There are no existing ones Lambdas are there specifically as throwaway single-use functions If you want to practice them, start some project, and use them instead of normal methods So if you would otherwise have
public int Sum(int a, int b)
{
return a + b;
}

var c = Sum(1, 2);
public int Sum(int a, int b)
{
return a + b;
}

var c = Sum(1, 2);
replace it with a
var sum = int (int a, int b) => a + b;
var c = sum(1, 2);
var sum = int (int a, int b) => a + b;
var c = sum(1, 2);
and so on Also, use LINQ Archival Advent of Code tasks are really cool for learning LINQ, since they're mostly all about pulling needed data from a huge dataset So instead of using loops and the like, force yourself to use LINQ
J.
J.OP15mo ago
I've practiced with LINQ and Java .stream() functional programming methods before, but they aren't the same as doing something like
IEnumerator StopConditionCheck(System.Func<bool> condition)
{
yield return new WaitUntil(condition);

StopAnimation();
}
IEnumerator StopConditionCheck(System.Func<bool> condition)
{
yield return new WaitUntil(condition);

StopAnimation();
}
as well as calling this type of method with lambdas While I'm aware that this also involves coroutines (which are probably confusing me further), I feel really stumped whenever I see syntax like this. I'd like to be able to read/write this type of stuff on snap reaction rather than taking 20 seconds to brain
Angius
Angius15mo ago
There isn't really much to it, though? Func if it returns a value Action if it doesn't Order of type params for Func is Func<TReturn, TParam1, TParam2, ...> You could try taking existing methods and writing out their signatures Then see if the method fits it
J.
J.OP15mo ago
i want to practice writing them, i just don't have exercises to start writing if it maeks sense
Angius
Angius15mo ago
Here you go, I asked ChatGPT to generate 20 random method signatures. Try to write them out as Funcs and Actions:
void DoSomething()
int Add(int a, int b)
string Concatenate(string str1, string str2)
bool IsPrime(int number)
double CalculateAverage(params double[] numbers)
void PrintNumbers(params int[] numbers)
DateTime GetNextWeekday(DateTime date, DayOfWeek dayOfWeek)
void SendMessage(string message, string recipient)
void SaveDataToFile(string fileName, string data)
List<int> FindEvenNumbers(List<int> numbers)
void DisplayMessage(string message, ConsoleColor color)
int Divide(int dividend, int divisor, out int remainder)
void Swap(ref int a, ref int b)
void UpdateEmployee(Employee emp, string newName, decimal newSalary)
void ProcessData(string input, Func<string, string> dataProcessor)
void SendEmail(string subject, string body, params string[] recipients)
bool IsPalindrome(string word)
void GenerateReport(string reportName, DateTime startDate, DateTime endDate)
List<string> SearchDatabase(string query, int maxResults)
void EncryptData(byte[] data, string encryptionKey)
void DoSomething()
int Add(int a, int b)
string Concatenate(string str1, string str2)
bool IsPrime(int number)
double CalculateAverage(params double[] numbers)
void PrintNumbers(params int[] numbers)
DateTime GetNextWeekday(DateTime date, DayOfWeek dayOfWeek)
void SendMessage(string message, string recipient)
void SaveDataToFile(string fileName, string data)
List<int> FindEvenNumbers(List<int> numbers)
void DisplayMessage(string message, ConsoleColor color)
int Divide(int dividend, int divisor, out int remainder)
void Swap(ref int a, ref int b)
void UpdateEmployee(Employee emp, string newName, decimal newSalary)
void ProcessData(string input, Func<string, string> dataProcessor)
void SendEmail(string subject, string body, params string[] recipients)
bool IsPalindrome(string word)
void GenerateReport(string reportName, DateTime startDate, DateTime endDate)
List<string> SearchDatabase(string query, int maxResults)
void EncryptData(byte[] data, string encryptionKey)
You can ask it for more if you need And then, as an added exercise, try writing a lambda that also fits it
J.
J.OP15mo ago
oh, I didn't think of GPT I can probably can ask it for the answers too to check my practice
Angius
Angius15mo ago
So, take
int Add(int a, int b)
int Add(int a, int b)
try writing out it's signature as
Func<int, int, int> func = Add;
Func<int, int, int> func = Add;
and assign the method to see if it works, then try writing a lambda
func = (a, b) => a + b;
func = (a, b) => a + b;
that also satisfies the signature I can't really think of a better exercise, this covers everything
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server