Hey thought i could share this here: <https://twit...
# getting-started
k
đŸ€” 1
c
You should probably name the function
isGreaterThanThisInt
or similar, because a lambda cannot be greater than an int
k
@CLOVIS Thanks for the feedback, will this affect the readers learning from the feed? If so, please update the play ground and share or let me know if i should do so.
j
I find it actually a bit confusing here for multiple reasons. 1. When an extension function starts with
is
, 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 literal
👍 3
k
@Joffrey Thanks alot, The play ground is shared on the tweet, help me understand this 1. How would you have specified it as? 2. How can I make it better in your opinion? 3. I know it exists, I was trying to teach someone else. Should I have used the existing function in this case? 4. An example would help me and my brother learn further.
@Joffrey Appreciate your feedback😊
@Joffrey From the Kotlin reference, I can see that a function can be passed as a variable, isn't this true? The function receiving it is a lambda so that is a lambda here. i.e Higher Order functions.
m
Really confusing, mostly because of the hard coded values 1 and 2. Seems really contrived, and should at least be reflected in the method name, e.g. isLambdaCalledWithOneAndTwoGreaterThanMyself.
➕ 1
✅ 1
k
Thanks @Michael Böiers It was a simple example, I will take in your contribution đŸ™ŒđŸŸ
@Michael Böiers Does the code block give an example of using a lambda in your opinion?
m
Depends on what you mean by lambda. If you mean in the idiomatic Kotlin way, then not really. What you’re actually demonstrating is that a method reference can be used instead of a lambda expression.
✔ 1
➕ 1
k
Aaah, ok, @Michael Böiers Do you have a simple example to show in the Kotlin Idiomatic way? I will be happy to re-share.
t
I would recommend
map
and
filter
functions on list ^^
k
A lambda expression is an anonymous function, and there is no lambda in your example code. It would be a lambda if you passed it like this:
someFunctionName { 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
.)
j
@Kiprop Victor Sorry for the delay. I'll try to answer your questions.
1. 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:
Copy code
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