Skip to content

Commit c5af483

Browse files
pauljervispivovarit
authored andcommitted
Kotlin Lambda Article Code (eugenp#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.
1 parent e50dab5 commit c5af483

3 files changed

Lines changed: 215 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.baeldung.lambda
2+
3+
fun inferredType(input: Int): Int {
4+
val square = { number: Int -> number * number }
5+
6+
return square(input)
7+
}
8+
9+
fun intToBiggerString(argument: Int): String {
10+
11+
val magnitude100String = { input: Int ->
12+
val magnitude = input * 100
13+
magnitude.toString()
14+
}
15+
16+
return magnitude100String(argument)
17+
}
18+
19+
fun manyLambda(nums: Array<Int>): List<String> {
20+
val newList = nums.map { intToBiggerString(it) }
21+
22+
return newList
23+
}
24+
25+
fun empty() {
26+
val noReturn: (Int) -> Unit = { num -> println(num) }
27+
28+
noReturn(5)
29+
}
30+
31+
fun invokeLambda(lambda: (Double) -> Boolean): Boolean {
32+
return lambda(4.329)
33+
}
34+
35+
fun extendString(arg: String, num: Int): String {
36+
val another: String.(Int) -> String = { this + it }
37+
38+
return arg.another(num)
39+
}
40+
41+
fun getCalculationLambda(): (Int) -> Any {
42+
val calculateGrade = { grade: Int ->
43+
when (grade) {
44+
in 0..40 -> "Fail"
45+
in 41..70 -> "Pass"
46+
in 71..100 -> "Distinction"
47+
else -> false
48+
}
49+
}
50+
51+
return calculateGrade
52+
}
53+
54+
fun getCalculationLambdaWithReturn(): (Int) -> String {
55+
val calculateGrade: Int.() -> String = lambda@{
56+
if (this < 0 || this > 100) {
57+
return@lambda "Error"
58+
} else if (this < 40) {
59+
return@lambda "Fail"
60+
} else if (this < 70) {
61+
return@lambda "Pass"
62+
}
63+
64+
"Distinction"
65+
}
66+
67+
return calculateGrade
68+
}
69+
70+
fun getCalculationAnonymousFunction(): (Int) -> String {
71+
val calculateGrade = fun(grade: Int): String {
72+
if (grade < 0 || grade > 100) {
73+
return "Error"
74+
} else if (grade < 40) {
75+
return "Fail"
76+
} else if (grade < 70) {
77+
return "Pass"
78+
}
79+
80+
return "Distinction"
81+
}
82+
83+
return calculateGrade
84+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.lambda;
2+
3+
import kotlin.jvm.functions.Function1;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
9+
/**
10+
* Created by Paul Jervis on 24/04/2018.
11+
*/
12+
class LambdaKotlinTest {
13+
14+
@Test
15+
void givenJava6_whenUsingAnonnymousClass_thenReturnLambdaResult() {
16+
assertTrue(LambdaKt.invokeLambda(new Function1<Double, Boolean>() {
17+
@Override
18+
public Boolean invoke(Double c) {
19+
return c >= 0;
20+
}
21+
}));
22+
}
23+
24+
@Test
25+
void givenJava8_whenUsingLambda_thenReturnLambdaResult() {
26+
assertTrue(LambdaKt.invokeLambda(c -> c >= 0));
27+
}
28+
29+
@Test
30+
void givenJava8_whenCallingMethodWithStringExtension_thenImplementExtension() {
31+
String actual = LambdaKt.extendString("Word", 90);
32+
String expected = "Word90";
33+
34+
assertEquals(expected, actual);
35+
}
36+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.baeldung.lambda
2+
3+
import org.junit.jupiter.api.Assertions.assertFalse
4+
import org.junit.jupiter.api.Assertions.assertTrue
5+
import org.junit.jupiter.api.Test
6+
import kotlin.test.assertEquals
7+
8+
class LambdaTest {
9+
@Test
10+
fun whenCallingALambda_thenPerformTheAction() {
11+
assertEquals(9, inferredType(3))
12+
}
13+
14+
@Test
15+
fun whenCallingAMoreComplicatedLambda_thenPerformTheAction() {
16+
assertEquals("500", intToBiggerString(5))
17+
}
18+
19+
@Test
20+
fun whenPassingALambdaObject_thenCallTriggerLambda() {
21+
val lambda = { arg: Double ->
22+
arg == 4.329
23+
}
24+
25+
val result = invokeLambda(lambda)
26+
27+
assertTrue(result)
28+
}
29+
30+
@Test
31+
fun whenPassingALambdaLiteral_thenCallTriggerLambda() {
32+
val result = invokeLambda({
33+
true
34+
})
35+
36+
assertTrue(result)
37+
}
38+
39+
@Test
40+
fun whenPassingALambdaLiteralOutsideBrackets_thenCallTriggerLambda() {
41+
val result = invokeLambda { arg -> arg.isNaN() }
42+
43+
assertFalse(result)
44+
}
45+
46+
@Test
47+
fun whenPassingAnAnonymousFunction_thenCallTriggerLambda() {
48+
val result = invokeLambda(fun(arg: Double): Boolean {
49+
return arg >= 0
50+
})
51+
52+
assertTrue(result)
53+
}
54+
55+
@Test
56+
fun whenUsingLambda_thenCalculateGrade() {
57+
val gradeCalculation = getCalculationLambda()
58+
59+
assertEquals(false, gradeCalculation(-40))
60+
assertEquals("Pass", gradeCalculation(50))
61+
}
62+
63+
@Test
64+
fun whenUsingReturnStatementLambda_thenCalculateGrade() {
65+
val gradeCalculation: Int.() -> String = getCalculationLambdaWithReturn()
66+
67+
assertEquals("Distinction", 80.gradeCalculation())
68+
assertEquals("Error", 244_234_324.gradeCalculation())
69+
}
70+
71+
@Test
72+
fun whenUsingAnonymousFunction_thenCalculateGrade() {
73+
val gradeCalculation = getCalculationAnonymousFunction()
74+
75+
assertEquals("Error", gradeCalculation(244_234_324))
76+
assertEquals("Pass", gradeCalculation(50))
77+
}
78+
79+
@Test
80+
fun whenPassingAFunctionReference_thenCallTriggerLambda() {
81+
val reference = Double::isFinite
82+
val result = invokeLambda(reference)
83+
84+
assertTrue(result)
85+
}
86+
87+
@Test
88+
fun givenArray_whenMappingArray_thenPerformCalculationOnAllElements() {
89+
val expected = listOf("100", "200", "300", "400", "500")
90+
val actual = manyLambda(arrayOf(1, 2, 3, 4, 5))
91+
92+
assertEquals(expected, actual)
93+
}
94+
95+
}

0 commit comments

Comments
 (0)