re: Explain Lambda expressions like I'm five VIEW POST


A lambda expression is an anonymous function (meaning a function without a name) which takes some parameters and a "function" (actually it's a statement or expression).
This is a simple exmaple from C# Programming Guide

(x, y) => x == y

On the left side you see two variables x and y in parenthesis. These are your parameters, on the right side you see the comparison x==y (the "function"). You could rewrite this to:

bool isEqual(int x, int y){
   return x == y

So you cold say lambda expression contain only the import stuff and drop the part of your code that doesn't contain the logic.

Lambda expressions can make your code easier to read and by that make it look cleaner.
A quick example (in Java, but that shouldn't be a problem):

public void lambdas(ArrayList<ArrayList<Integer>> nestedList){
                .flatMap(List::stream) //<-- (These colon things are lambdas too ;))
                .map(x -> x*2)

Even if you dont't know all the methods it's pretty easy to guess what this code does isn't it? (the flatMap thing just makes one list out of a nested one ;))

Feel free to ask if anything is unclear :)

code of conduct - report abuse