Kiprop Victor
09/25/2021, 10:36 AMCLOVIS
09/25/2021, 10:43 AMisGreaterThanThisInt
or similar, because a lambda cannot be greater than an intKiprop Victor
09/25/2021, 11:08 AMJoffrey
09/25/2021, 11:34 AMis
, I really expect that the thing that "is" is the receiver. 42.isGreaterThan(12)
reads like english, while 42.isThatNumberGreaterThanThis(12)
is really confusing.
2. I agree with @CLOVIS that the lambda cannot be greater or smaller than an int, but its result can. So the name is also a bit confusing in this respect.
3. The name of the function reference that is passed is kinda weird, compareInts
should compare but it's unclear why it has a return value and what the return value is. maxOf
would be more appropriate... and it exists already by the way 🙂
4. there is no lambda here. Only a function reference. A lambda is a { ... }
block that acts as a function literalKiprop Victor
09/25/2021, 1:47 PMMichael Böiers
09/25/2021, 3:32 PMKiprop Victor
09/25/2021, 3:36 PMMichael Böiers
09/25/2021, 3:40 PMKiprop Victor
09/25/2021, 3:48 PMTomasz Krakowiak
09/25/2021, 3:48 PMmap
and filter
functions on list ^^Klitos Kyriacou
09/27/2021, 9:18 AMsomeFunctionName { a, b -> if (a > b) a else b }
.
(This is the Kotlin equivalent of the Python expression lambda a, b: a if (a > b) else b
.)Joffrey
09/27/2021, 9:39 AM1. How would you have specified it as?Reverse the name so that the "is" part applies to the receiver, so something like
Int.isLessThanXxxx()
instead of Int.isThatThingGreaterThanThis
.
2. How can I make it better in your opinion?It's difficult to just improve the name here, because the example doesn't really represent a compelling use case. Normally you would find a name that make it obvious you talk about being smaller than the result of the function, but the name should be based on what the function argument means - here it's just "lambda" so we don't know what it's supposed to do nor where its arguments will come from. Similar to what @Michael Böiers wrote, in this convoluted case I'd rather name it something like
isLessThanThatFunctionCalledWithOneAndTwo
(because there is no real meaning to these values here).
3. I know it exists, I was trying to teach someone else. Should I have used the existing function in this case?I'm sorry I was unclear here. I didn't mean you should have used the existing
maxOf
function, I also believe that when you teach it's sometimes better to show stuff written by hand. What I meant is that you should take inspiration in the names that the stdlib actually use. Using compareInts
is too vague to be a meaningful example, so I suggested using a name that actually reflects what the function returns: max
or maxOf
. I just mentioned the existence of the stdlib function to back up my argument that the name would be more expressive this way.
4. An example would help me and my brother learn further.@Michael Böiers and @Klitos Kyriacou might have already answered that. You seem to be confusing lambdas and functions.
isLambdaGreaterThanThisInt
doesn't take a "lambda" as argument per se. It takes an instance of a function. The argument called lambda
is just an argument, the type of which is a function type. Higher-order functions are functions that take functions as arguments.
Now, when you need to pass a function instance you can do it in several ways. Here are some examples using the map
higher-order function, and passing functions to it in 4 different ways:
val list = listOf(1, 2, 3)
// using a lambda expression, which is a function literal "{...}"
list.map { it * 2 }
// or to understand better that the lambda expression is actually an argument
list.map({it * 2})
// using an anonymous function
list.map(fun(value: Int): Int = value * 2)
// using a function reference
// I first declare a local function here, and then use :: and the function name to refer to it
fun multiplyBy2(value: Int): Int = value * 2
list.map(::multiplyBy2)
// using a function stored in a variable (the variable is initialized using a lambda expression)
val multiplyBy2: (Int) -> Int = { it * 2 }
list.map(multiplyBy2)
Here you can clearly see the difference between:
• a lambda expression (a block {...}
creating a function instance in a compact way)
• an anonymous function (fun(value: Int): Int = value * 2
) which uses the fun
syntax and allows to you to create a function instance like a lambda but also specifying a return type. Note that return
behaves differently in anonymous functions and in lambdas.
• a function reference (using ::funName
)
To learn more, you can have a look at this page in the doc, which clearly distinguishes these related concepts:
https://kotlinlang.org/docs/lambdas.html#higher-order-functions