0
0
Kotlinprogramming~10 mins

Infix functions in DSLs in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an infix function named to.

Kotlin
infix fun String.[1](other: String): Pair<String, String> = Pair(this, other)
Drag options to blanks, or click blank then click option'
Afrom
Band
Cto
Dwith
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that is not declared as infix.
Choosing a name that is not descriptive or conventional.
2fill in blank
medium

Complete the code to call the infix function to to create a pair.

Kotlin
val pair = "key" [1] "value"
Drag options to blanks, or click blank then click option'
Ato
Bwith
Cfrom
Dand
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation instead of infix call.
Using a wrong function name.
3fill in blank
hard

Fix the error in the infix function declaration by completing the code.

Kotlin
infix fun [1].to(other: String): Pair<String, String> = Pair(this, other)
Drag options to blanks, or click blank then click option'
AInt
BAny
CPair
DString
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong receiver type like Int or Pair.
Forgetting the receiver type.
4fill in blank
hard

Fill both blanks to create an infix function connect that joins two strings with a dash.

Kotlin
infix fun String.[1](other: String): String = this [2] "-" + other
Drag options to blanks, or click blank then click option'
Aconnect
B+
C*
Djoin
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong operator like * which is invalid for strings.
Choosing a function name that is not descriptive.
5fill in blank
hard

Fill all three blanks to create a DSL that uses infix functions to build a map.

Kotlin
val map = mapOf(
    "one" [1] 1,
    "two" [2] 2,
    "three" [3] 3
)
Drag options to blanks, or click blank then click option'
Ato
Bfrom
C->
Dwith
Attempts:
3 left
💡 Hint
Common Mistakes
Using operators like -> which is not an infix function here.
Mixing different infix functions in the same map.