Kotlin Lambda Article Code (#4100)
* Add Kotlin lambdas and tests from Java and Kotlin * Add the actual code under test * Add different types of lambdas performing the same action.
This commit is contained in:
parent
e50dab578f
commit
c5af483a8d
84
core-kotlin/src/main/kotlin/com/baeldung/lambda/Lambda.kt
Normal file
84
core-kotlin/src/main/kotlin/com/baeldung/lambda/Lambda.kt
Normal file
@ -0,0 +1,84 @@
|
||||
package com.baeldung.lambda
|
||||
|
||||
fun inferredType(input: Int): Int {
|
||||
val square = { number: Int -> number * number }
|
||||
|
||||
return square(input)
|
||||
}
|
||||
|
||||
fun intToBiggerString(argument: Int): String {
|
||||
|
||||
val magnitude100String = { input: Int ->
|
||||
val magnitude = input * 100
|
||||
magnitude.toString()
|
||||
}
|
||||
|
||||
return magnitude100String(argument)
|
||||
}
|
||||
|
||||
fun manyLambda(nums: Array<Int>): List<String> {
|
||||
val newList = nums.map { intToBiggerString(it) }
|
||||
|
||||
return newList
|
||||
}
|
||||
|
||||
fun empty() {
|
||||
val noReturn: (Int) -> Unit = { num -> println(num) }
|
||||
|
||||
noReturn(5)
|
||||
}
|
||||
|
||||
fun invokeLambda(lambda: (Double) -> Boolean): Boolean {
|
||||
return lambda(4.329)
|
||||
}
|
||||
|
||||
fun extendString(arg: String, num: Int): String {
|
||||
val another: String.(Int) -> String = { this + it }
|
||||
|
||||
return arg.another(num)
|
||||
}
|
||||
|
||||
fun getCalculationLambda(): (Int) -> Any {
|
||||
val calculateGrade = { grade: Int ->
|
||||
when (grade) {
|
||||
in 0..40 -> "Fail"
|
||||
in 41..70 -> "Pass"
|
||||
in 71..100 -> "Distinction"
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
return calculateGrade
|
||||
}
|
||||
|
||||
fun getCalculationLambdaWithReturn(): (Int) -> String {
|
||||
val calculateGrade: Int.() -> String = lambda@{
|
||||
if (this < 0 || this > 100) {
|
||||
return@lambda "Error"
|
||||
} else if (this < 40) {
|
||||
return@lambda "Fail"
|
||||
} else if (this < 70) {
|
||||
return@lambda "Pass"
|
||||
}
|
||||
|
||||
"Distinction"
|
||||
}
|
||||
|
||||
return calculateGrade
|
||||
}
|
||||
|
||||
fun getCalculationAnonymousFunction(): (Int) -> String {
|
||||
val calculateGrade = fun(grade: Int): String {
|
||||
if (grade < 0 || grade > 100) {
|
||||
return "Error"
|
||||
} else if (grade < 40) {
|
||||
return "Fail"
|
||||
} else if (grade < 70) {
|
||||
return "Pass"
|
||||
}
|
||||
|
||||
return "Distinction"
|
||||
}
|
||||
|
||||
return calculateGrade
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.baeldung.lambda;
|
||||
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Created by Paul Jervis on 24/04/2018.
|
||||
*/
|
||||
class LambdaKotlinTest {
|
||||
|
||||
@Test
|
||||
void givenJava6_whenUsingAnonnymousClass_thenReturnLambdaResult() {
|
||||
assertTrue(LambdaKt.invokeLambda(new Function1<Double, Boolean>() {
|
||||
@Override
|
||||
public Boolean invoke(Double c) {
|
||||
return c >= 0;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJava8_whenUsingLambda_thenReturnLambdaResult() {
|
||||
assertTrue(LambdaKt.invokeLambda(c -> c >= 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenJava8_whenCallingMethodWithStringExtension_thenImplementExtension() {
|
||||
String actual = LambdaKt.extendString("Word", 90);
|
||||
String expected = "Word90";
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
package com.baeldung.lambda
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertFalse
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class LambdaTest {
|
||||
@Test
|
||||
fun whenCallingALambda_thenPerformTheAction() {
|
||||
assertEquals(9, inferredType(3))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenCallingAMoreComplicatedLambda_thenPerformTheAction() {
|
||||
assertEquals("500", intToBiggerString(5))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenPassingALambdaObject_thenCallTriggerLambda() {
|
||||
val lambda = { arg: Double ->
|
||||
arg == 4.329
|
||||
}
|
||||
|
||||
val result = invokeLambda(lambda)
|
||||
|
||||
assertTrue(result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenPassingALambdaLiteral_thenCallTriggerLambda() {
|
||||
val result = invokeLambda({
|
||||
true
|
||||
})
|
||||
|
||||
assertTrue(result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenPassingALambdaLiteralOutsideBrackets_thenCallTriggerLambda() {
|
||||
val result = invokeLambda { arg -> arg.isNaN() }
|
||||
|
||||
assertFalse(result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenPassingAnAnonymousFunction_thenCallTriggerLambda() {
|
||||
val result = invokeLambda(fun(arg: Double): Boolean {
|
||||
return arg >= 0
|
||||
})
|
||||
|
||||
assertTrue(result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenUsingLambda_thenCalculateGrade() {
|
||||
val gradeCalculation = getCalculationLambda()
|
||||
|
||||
assertEquals(false, gradeCalculation(-40))
|
||||
assertEquals("Pass", gradeCalculation(50))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenUsingReturnStatementLambda_thenCalculateGrade() {
|
||||
val gradeCalculation: Int.() -> String = getCalculationLambdaWithReturn()
|
||||
|
||||
assertEquals("Distinction", 80.gradeCalculation())
|
||||
assertEquals("Error", 244_234_324.gradeCalculation())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenUsingAnonymousFunction_thenCalculateGrade() {
|
||||
val gradeCalculation = getCalculationAnonymousFunction()
|
||||
|
||||
assertEquals("Error", gradeCalculation(244_234_324))
|
||||
assertEquals("Pass", gradeCalculation(50))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenPassingAFunctionReference_thenCallTriggerLambda() {
|
||||
val reference = Double::isFinite
|
||||
val result = invokeLambda(reference)
|
||||
|
||||
assertTrue(result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun givenArray_whenMappingArray_thenPerformCalculationOnAllElements() {
|
||||
val expected = listOf("100", "200", "300", "400", "500")
|
||||
val actual = manyLambda(arrayOf(1, 2, 3, 4, 5))
|
||||
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user