diff --git a/src/main/kotlin/ru/otus/homework/functions.kt b/src/main/kotlin/ru/otus/homework/functions.kt index 4a7fe1e..388b3b2 100644 --- a/src/main/kotlin/ru/otus/homework/functions.kt +++ b/src/main/kotlin/ru/otus/homework/functions.kt @@ -1,5 +1,6 @@ package ru.otus.homework +import java.lang.System.* import java.time.LocalDate fun main() { @@ -27,6 +28,12 @@ fun main() { val product = 2 by 2 println("Произведение: $product") + + val sum = variableAdd(1, 2, 3, 4) + println("1 + 2 + 3 + 4 = $sum") + + val ms = measure(::longLoop) + println("longLoop takes $ms milliseconds") } infix fun Int.by(other: Int): Int = this * other @@ -78,3 +85,41 @@ fun calculate(n1: Int, n2: Int, op: (Int, Int) -> Int): String { fun add(a: Int, b: Int): Int = a + b fun subtract(a: Int, b: Int): Int = a - b + +fun variableAdd(a: Int, b: Int, vararg values: Int): Int { + var result = a + b + + for(i in values) { + result += i + } + + return result +} + +fun sumStrings(vararg strings: String, separator: Char = ' '): String { + var result: String = "" + + for(i in strings) { + if(result.isEmpty()) + result += i + else + result += ("$separator" + i) + } + + return result +} + +fun longLoop() { + for(i in 1..100) { + println("i = $i") + Thread.sleep(10) + } +} + +fun measure( oper: () -> Unit ): Long { + val start = currentTimeMillis() + + oper() + + return (currentTimeMillis() - start) +} \ No newline at end of file diff --git a/src/test/kotlin/ru/otus/homework/FunctionsTest.kt b/src/test/kotlin/ru/otus/homework/FunctionsTest.kt index 93a36cf..edb5a6d 100644 --- a/src/test/kotlin/ru/otus/homework/FunctionsTest.kt +++ b/src/test/kotlin/ru/otus/homework/FunctionsTest.kt @@ -11,4 +11,16 @@ class FunctionsTest { calculate(1, 2) ) } + fun sumStringsTest() { + Assertions.assertEquals( + "str1 str2 str3", + sumStrings("str1", "str2", "str3") + ) + } + fun sumStringsTest2() { + Assertions.assertEquals( + "str1,str2,str3", + sumStrings("str1", "str2", "str3", separator = ',') + ) + } } \ No newline at end of file