-
Notifications
You must be signed in to change notification settings - Fork 15
Use Kotlin function reference in HOF example #11
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| fun transform(initial: String, f: (String) -> String) = f(initial) | ||
| fun transform(initial: String, transformer: (String) -> String) = transformer(initial) | ||
|
|
||
| val result = transform("hello", { x -> x.toUpperCase() }) | ||
| // Function reference | ||
| val result = transform("hello", String::uppercase) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| // HELLO | ||
|
|
||
| // Trailing lambda can be placed outside the parentheses | ||
| val result2 = transform("hello") { x -> x.toUpperCase() } | ||
| val result2 = transform("hello") { it.uppercase() } | ||
There was a problem hiding this comment.
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.