Created
October 15, 2021 06:47
-
-
Save eutkin/ecd249d7c3db30e3c0c04377fab330f6 to your computer and use it in GitHub Desktop.
example kotlin (for students)
This file contains 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(args: Array<String>) { | |
val sum: (Int, Int) -> Int = { x: Int, y: Int -> | |
x + y | |
} | |
val print: (String) -> Unit = { println(it) } | |
print("hello") | |
/* | |
1) Функция, принимающая два дробных числа (Double) и перемножающая их | |
2) Функция convert, которая принимает на вход StringConverter и преобразует значения всех атрибутов события в строки | |
3) В предыдущем задании использовать MutableMap | |
4) Написать и вызвать с выводом результата на экран функции, которые: принимают один, два, три аргумента. | |
5) Написать функцию, которая принимает два аргумента, но использует только один | |
*/ | |
} | |
fun convert(event: Map<String, Any>, converter: IntConverter): Map<String, Int> { | |
val convertedEvent: HashMap<String, Int> = HashMap<String, Int>(event.size); | |
for (attribute: String in event.keys) { | |
val rawValue: Any? = event.get(attribute); | |
val convertedValue: Int = converter.convert(rawValue); | |
convertedEvent.put(attribute, convertedValue); | |
} | |
return convertedEvent; | |
} | |
interface Converter<T> { | |
// функция convert принимает аргумент rawValue с типом Any? и возвращает обобщенный тип T | |
fun convert(rawValue: Any?): T | |
} | |
class IntConverter : Converter<Int> { | |
override fun convert(rawValue: Any?): Int { | |
return rawValue.toString().toInt(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment