Last active
May 7, 2024 01:02
-
-
Save GabrielBrasileiro/abfc450db11a2beee4212eb9c8910aaa to your computer and use it in GitHub Desktop.
HighOrderFunctionsInline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun executeOperation(crossinline operationTwo: () -> Unit) { | |
operationOne { | |
operationTwo() | |
} | |
} | |
@PublishedAPI | |
internal fun operationOne(block: () -> Unit) { | |
block() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static final void main() { | |
final String description = "Inline giving you magical powers"; | |
executeOperation(new Function() { | |
@Override | |
public void invoke() { | |
System.out.print(description); | |
} | |
}); | |
} | |
public static final void executeOperation(@NotNull Function operationTwo) { | |
operationOne(); | |
operationTwo.invoke(); | |
operationThree(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val description = "Inline giving you magical powers" | |
executeOperation { | |
print(description) | |
} | |
} | |
fun executeOperation(operationTwo: () -> Unit) { | |
operationOne() | |
operationTwo() | |
operationThree() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val description = "Inline giving you magical powers" | |
executeOperation { | |
print(description) | |
return // Not allowed return | |
} | |
} | |
inline fun executeOperation(crossinline operationTwo: () -> Unit) { | |
operationOne { | |
operationTwo() | |
} | |
} | |
fun operationOne(block: () -> Unit) { | |
block() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val description = "Inline giving you magical powers" | |
val descriptionInMainScope = "Inline in the main scope" | |
executeOperation({ | |
print(description) | |
return // Allowed return | |
}, { | |
print(descriptionInMainScope) | |
return // Not allowed return | |
}) | |
} | |
inline fun executeOperation(operationTwo: () -> Unit, noinline operationFour: () -> Unit) { | |
operationOne() | |
operationTwo() | |
operationThree() | |
operationFour() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static final void main() { | |
String description = "Inline giving you magical powers"; | |
operationOne(); | |
System.out.print(description); | |
operationThree(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val description = "Inline giving you magical powers" | |
executeOperation { | |
print(description) | |
} | |
} | |
inline fun executeOperation(operationTwo: () -> Unit) { | |
operationOne() | |
operationTwo() | |
operationThree() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
println(filterString<String>()) | |
println(requireMethods<String>()) | |
} | |
inline fun <reified T : Any> requireMethods(): List<String> { | |
return T::class.java.methods.map { it.toString() } | |
} | |
inline fun <reified T : Any> filterString(): T? { | |
itens().forEach { | |
if (it is T) { | |
return it | |
} | |
} | |
return null | |
} | |
fun itens() = listOf<Any>(0, "Reified", 1f) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
println(filterString(String::class.java)) | |
println(requireMethods(String::class.java)) | |
} | |
fun <T : Any> requireMethods(clazz: Class<T>): List<String> { | |
return clazz::class.java.methods.map { it.toString() } | |
} | |
fun <T : Any> filterString(clazz: Class<T>): T? { | |
itens().forEach { | |
if (clazz.isInstance(it)) { | |
@Suppress("UNCHECKED_CAST") | |
return it as T | |
} | |
} | |
return null | |
} | |
fun itens() = listOf<Any>(0, "Reified", 1f) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static final void main() { | |
// Function to acquire methods | |
Method[] methods = String.class.getMethods(); | |
// Validation of the String in the list | |
if (element instanceof String) { | |
// Assignments | |
} | |
/** | |
* Codes related to the map and foreach methods, which are also inline. o/ | |
* They will appear according to the order of method calls, but it's just a detail that can be ignored in the example. | |
*/ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val description = "Inline giving you magical powers" | |
executeOperation { | |
print(description) | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment