Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions code/hof-parameter.kt
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)
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.

// HELLO

// Trailing lambda can be placed outside the parentheses
val result2 = transform("hello") { x -> x.toUpperCase() }
val result2 = transform("hello") { it.uppercase() }
7 changes: 4 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,14 @@

// MakeIncrementer can also be written in a shorter way:
Func<int, int> MakeIncrementer() => i => 1 + i;
</code></pre></div></div></div><div class="case"><div class="name">HOF - Function as Parameter</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code>fun transform(initial: String, f: (String) -> String) = f(initial)
</code></pre></div></div></div><div class="case"><div class="name">HOF - Function as Parameter</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code>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() }</code></pre></div><div class="card"><div class="lang">C#</div><pre class="code"><code>string Transform(string initial, Func&lt;string, string> f) => f(initial);
val result2 = transform("hello") { it.uppercase() }</code></pre></div><div class="card"><div class="lang">C#</div><pre class="code"><code>string Transform(string initial, Func&lt;string, string> f) => f(initial);

var result = Transform("hello", x => x.ToUpper());
// HELLO</code></pre></div></div></div><div class="case"><div class="name">Tuple Return</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code>// Kotlin doesn't have tuples, use data classes
Expand Down