Skip to content

Instantly share code, notes, and snippets.

@sagarpatel288
sagarpatel288 / flOneParam.kt
Last active May 5, 2020 09:40
Passing a function literal that has only one parameter (function literal syntax/ signature 2)
/**
* While calling the higher order function When the function type parameter is one and only parameter,
* and if the function type has also one and only parameter,
* in addition to function call parentheses, we can also omit the parameter of the function type and an arrow ```->```
* We just write core business logic between curly braces
* and we can access our single parameter through the keyword it like below:
*/
doSomething { "$it" }
@sagarpatel288
sagarpatel288 / hOfOneFtOneParam.kt
Last active May 5, 2020 09:31
A higher order function that has one and only function type parameter. This function type has also one and only parameter.
//region A function type that has only one parameter
private fun doSomething(ft: (Int) -> String) {
/*...*/
}
//endregion
@sagarpatel288
sagarpatel288 / hOfOneFt.kt
Created May 5, 2020 08:32
A higher order function with one and only function type parameter
//region A higher order function that has one and only function type parameter
private fun doSomething(ft: (Int, Int) -> Int) {
val result = ft(1, 2)
/*...*/
}
//endregion
@sagarpatel288
sagarpatel288 / hOfWithOnlyFtParameter.kt
Last active May 5, 2020 08:29
When a higher order function has only one parameter and the parameter type is function type
/**
* While calling the higher order function When the function type parameter is only parameter,
* we can simply write our business logic between curly braces after writing the function name
* and omit function call parentheses like below:
*/
doSomething { x: Int, y: Int -> x + y }
@sagarpatel288
sagarpatel288 / normalHoF.kt
Created May 4, 2020 11:13
normal higher order function
fun Int.doSomething(y: Int, ftOne: Int.(Int) -> Int) {
//passing a function type to another function
val funOne = someFunction(ftOne)
/*...*/
}
@sagarpatel288
sagarpatel288 / overheadInline.kt
Created May 4, 2020 11:12
overhead inline function
inline fun Int.doSomething(y: Int, noinline ftOne: Int.(Int) -> Int) {
//passing a function type to another function
val funOne = someFunction(ftOne)
/*...*/
}
@sagarpatel288
sagarpatel288 / inlineNoInline.kt
Created May 4, 2020 11:11
noinline function type in an inline function
inline fun Int.doSomething(y: Int, noinline ftOne: Int.(Int) -> Int, ftTwo: (Int) -> Int) {
//passing a function type to another function
val funOne = someFunction(ftOne)
/*...*/
}
@sagarpatel288
sagarpatel288 / invalidInline.kt
Created May 4, 2020 11:10
invalid inline function
//Compile time error… Illegal usage of inline function type [ftOne]...
inline fun Int.doSomething(y: Int, ftOne: Int.(Int) -> Int, ftTwo: (Int) -> Int) {
//passing a function type to another function
val funOne = someFunction(ftOne)
/*...*/
}
@sagarpatel288
sagarpatel288 / inlineFunction.kt
Created May 4, 2020 11:09
Making a simple inline function
inline fun Int.doSomething(y: Int, ftOne: Int.(Int) -> Int, ftTwo: (Int) -> Int) {
/*...*/
}
@sagarpatel288
sagarpatel288 / hOf.kt
Created May 4, 2020 11:08
A function that is eligible to be an inline function
fun Int.doSomething(y: Int, ftOne: Int.(Int) -> Int, ftTwo: (Int) -> Int) {
/*...*/
}