diff --git a/code/hof-parameter.kt b/code/hof-parameter.kt index 56d6011..598f8a1 100644 --- a/code/hof-parameter.kt +++ b/code/hof-parameter.kt @@ -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) // HELLO // Trailing lambda can be placed outside the parentheses -val result2 = transform("hello") { x -> x.toUpperCase() } \ No newline at end of file +val result2 = transform("hello") { it.uppercase() } \ No newline at end of file diff --git a/index.html b/index.html index 48b4a49..d8e5f10 100644 --- a/index.html +++ b/index.html @@ -242,13 +242,14 @@ // MakeIncrementer can also be written in a shorter way: Func<int, int> MakeIncrementer() => i => 1 + i; -
HOF - Function as Parameter
Kotlin
fun transform(initial: String, f: (String) -> String) = f(initial)
+
HOF - Function as Parameter
Kotlin
fun transform(initial: String, transformer: (String) -> String) = transformer(initial)
 
-val result = transform("hello", { x -> x.toUpperCase() })
+// Function reference
+val result = transform("hello", String::uppercase)
 // HELLO
 
 // Trailing lambda can be placed outside the parentheses
-val result2 = transform("hello") { x -> x.toUpperCase() }
C#
string Transform(string initial, Func<string, string> f) => f(initial);
+val result2 = transform("hello") { it.uppercase() }
C#
string Transform(string initial, Func<string, string> f) => f(initial);
 
 var result = Transform("hello", x => x.ToUpper());
 // HELLO
Tuple Return
Kotlin
// Kotlin doesn't have tuples, use data classes