Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Kotlin function reference in HOF example #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

russellbanks
Copy link
Contributor

Having a lambda inside a function's parantheses isn't hugely idiomatic in Kotlin.

Therefore, you can either use a function reference or have it outside the parantheses:

  • transform(String::uppercase)
  • transform { it.uppercase() }

You don't need to specify a variable name when using a lambda as it is implicitly provided.

toUpperCase() was deprecated in Kotlin 1.5.0 in favour of uppercase().

Before:

fun transform(initial: String, f: (String) -> String) = f(initial)

val result = transform("hello", { x -> x.toUpperCase() })
// HELLO

// Trailing lambda can be placed outside the parentheses
val result2 = transform("hello") { x -> x.toUpperCase() }

After:

fun transform(initial: String, transformer: (String) -> String) = transformer(initial)

// Function reference
val result = transform("hello", String::uppercase)
// HELLO

// Trailing lambda can be placed outside the parentheses
val result2 = transform("hello") { it.uppercase() }

You can test this code here on the Kotlin Playground.

Copy link
Owner

@ttu ttu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update! We could make small changes to example functions.

@@ -1,7 +1,8 @@
fun transform(initial: String, f: (String) -> String) = f(initial)
fun transform(initial: String, transformer: (String) -> String) = transformer(initial)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming function with a single letter (f) is not the best practice. However, it was used to ensure the code would fit to a single view.


val result = transform("hello", { x -> x.toUpperCase() })
// Function reference
val result = transform("hello", String::uppercase)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, from the example's perspective, changing the functionality slightly, e.g. to { "PREFIX_${it.uppercase()}" }, would make more sense. The point is not to showcase the shortest example, but to demonstrate how functions are used. I can make the corresponding change to the C# example later to match with this modification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants