Skip to content

Commit 68087ba

Browse files
committed
[fun-as-parm] examples simplified
1 parent cb2a815 commit 68087ba

File tree

1 file changed

+37
-40
lines changed

1 file changed

+37
-40
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
package com.baeldung.functionAsParameter
22

3-
import com.baeldung.functionAsParameter.Sender.*
43
import org.junit.jupiter.api.Test
54
import kotlin.test.assertEquals
65

76
fun joinByOperation(theList: List<String>, operation: (List<String>) -> String): String {
87
return operation(theList)
98
}
109

11-
12-
data class TheMessage(val sender: Sender, val segments: List<String>) {
13-
fun parse(operation: (List<String>) -> String): String {
14-
return "Message From $sender is: ${operation(segments)}"
15-
}
16-
}
17-
18-
enum class Sender {
19-
User, RemoteAPI, PartnerApp, CsvSender
20-
}
21-
22-
2310
class MessageParser {
2411
fun joinWithoutPlaceholder(segments: List<String>): String {
2512
return segments.joinToString(separator = " ").replace(" [SPACE] ", " ")
@@ -32,7 +19,6 @@ class MessageParser {
3219
}
3320
}
3421

35-
3622
object ParserInObject {
3723
fun joinWithoutComma(segments: List<String>): String {
3824
return segments.joinToString(separator = " ") { it.replace(",", "") }
@@ -43,51 +29,62 @@ fun decrypt(segments: List<String>): String {
4329
return segments.reversed().joinToString(separator = " ") { it.reversed() }
4430
}
4531

46-
val messageParser = MessageParser()
47-
fun parseMessage(message: TheMessage): String {
48-
return when (message.sender) {
49-
User -> message.parse(MessageParser::simplyJoin)
50-
PartnerApp -> message.parse(messageParser::joinWithoutPlaceholder)
51-
CsvSender -> message.parse(ParserInObject::joinWithoutComma)
52-
RemoteAPI -> message.parse(::decrypt)
53-
}
54-
}
55-
5632
class FunctionAsParameterUnitTest {
5733

5834
@Test
5935
fun `when passing lambda as parameters then get expected result`() {
60-
val input = listOf("a b c", "x y z", "kotlin")
36+
val input = listOf("a b c", "d e f", "x y z")
6137
val result1 = joinByOperation(input) { theList ->
6238
theList.joinToString(separator = " ") { str -> str.reversed() }.replace(" ", ", ")
6339
}
64-
assertEquals("c, b, a, z, y, x, niltok", result1)
40+
assertEquals("c, b, a, f, e, d, z, y, x", result1)
6541

6642
val result2 = joinByOperation(input) { theList ->
6743
theList.reversed().joinToString(separator = " ") { str -> str }.uppercase()
6844
}
69-
assertEquals("KOTLIN X Y Z A B C", result2)
45+
assertEquals("X Y Z D E F A B C", result2)
7046

7147
}
7248

7349
@Test
74-
fun `when passing existing fun as parameters then get expected result`() {
50+
fun `when passing instance function ref as parameters then get expected result`() {
51+
val messageParser = MessageParser()
52+
val input = listOf("a [SPACE] b [SPACE] c", "d [SPACE] e [SPACE] f", "x [SPACE] y [SPACE] z")
53+
val result = joinByOperation(input, messageParser::joinWithoutPlaceholder)
54+
assertEquals("a b c d e f x y z", result)
55+
}
56+
57+
@Test
58+
fun `when passing companion object function ref as parameters then get expected result`() {
59+
val input = listOf("a b c", "d e f", "x y z")
60+
val result = joinByOperation(input, MessageParser::simplyJoin)
61+
assertEquals("a b c d e f x y z", result)
62+
}
7563

64+
@Test
65+
fun `when passing object function ref as parameters then get expected result`() {
66+
val input = listOf("a, b, c", "d, e, f", "x, y, z")
67+
val result = joinByOperation(input, ParserInObject::joinWithoutComma)
68+
assertEquals("a b c d e f x y z", result)
69+
}
7670

77-
val msgFromUser = TheMessage(User, listOf("a b c", "d e f", "x y z"))
78-
val resultUser = parseMessage(msgFromUser)
79-
assertEquals("Message From User is: a b c d e f x y z", resultUser)
71+
@Test
72+
fun `when passing top-level function ref as parameters then get expected result`() {
73+
val input = listOf("z y x", "f e d", "c b a")
74+
val result = joinByOperation(input, ::decrypt)
75+
assertEquals("a b c d e f x y z", result)
76+
}
8077

81-
val msgFromPartner = TheMessage(PartnerApp, listOf("a [SPACE] b [SPACE] c", "d [SPACE] e [SPACE] f", "x [SPACE] y [SPACE] z"))
82-
val resultPartner = parseMessage(msgFromPartner)
83-
assertEquals("Message From PartnerApp is: a b c d e f x y z", resultPartner)
78+
@Test
79+
fun `when passing variable with function type as parameter then get expected result`() {
80+
val input = listOf("a, b, c", "d, e, f", "x, y, z")
8481

85-
val msgFromCsv = TheMessage(CsvSender, listOf("a, b, c", "d, e, f", "x, y, z"))
86-
val resultCsv = parseMessage(msgFromCsv)
87-
assertEquals("Message From CsvSender is: a b c d e f x y z", resultCsv)
82+
val funRef = ParserInObject::joinWithoutComma
83+
val resultFunRef = joinByOperation(input, funRef)
84+
assertEquals("a b c d e f x y z", resultFunRef)
8885

89-
val msgFromAPI = TheMessage(RemoteAPI, listOf("z y x", "f e d", "c b a"))
90-
val resultApi = parseMessage(msgFromAPI)
91-
assertEquals("Message From RemoteAPI is: a b c d e f x y z", resultApi)
86+
val funLambda = { theList: List<String> -> theList.reversed().joinToString(separator = ", ") { str -> str }.uppercase() }
87+
val resultFunLambda = joinByOperation(input, funLambda)
88+
assertEquals("X, Y, Z, D, E, F, A, B, C", resultFunLambda)
9289
}
9390
}

0 commit comments

Comments
 (0)