A concept derived from a functional language. A functional language is a language that "treats a function like a value".
A "value" is a number, a string, or a list. To be able to treat it like a value
--Can be assigned to a variable --Pass it as a function argument --Can be the return value of a function
That is.
For example, a function that takes a value as an argument, adds 10 to it, and returns it.
F(x) = x + 10
Functions like. In functional languages, this function can be handled as a value.
So how do you assign the above function to a variable? This "how to write a function that can be handled as a value" is a lambda expression.
For example, in the functional language Haskell, the above function
f = x -> x + 10
It can be described like this.
Think of it as "[a function that receives the number x and returns the value obtained by adding 10] to a variable called f."
This writing style varies from language to language, but they are all lambda expressions. All of them define "how to write a function that can be handled as a value". It doesn't matter if there is a small difference in writing style such as-> or =>.
So far, it's a lambda expression, but the situation is a little different in Java and C #.
Because these are classes-based languages, functions can only exist in classes, that is, in the form of methods.
So, I made a function (Func in C #) interface so that it can be handled as a pseudo function.
For example in Java
Function<Integer, Integer>f = new Funciton<>(){
public Integer apply(Integer x){
return x + 10;
}
}
It's just an abbreviation for.
Since it is just an instance, it can be assigned to a variable, and it can be used as both an argument and a return destination.
The above is the explanation of the lambda expression.
―― "What is a lambda expression in the first place?" ―― "How to incorporate a lambda expression (a concept derived from a functional language) into an object-oriented language?"
If you get confused, you won't be able to tell the difference at once, so it's better to think of these two separately.
Recommended Posts