-
Notifications
You must be signed in to change notification settings - Fork 1
/
Functions.kt
92 lines (75 loc) · 2.21 KB
/
Functions.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class Functions {
fun test() {
println("Basic functions")
println(double(10))
println(triple(10))
println(isPrimeNumber(17))
println("")
println("Default arguments")
val s = "Function parameters can have default values, which are"
read(s.toByteArray())
println("")
println("Override functions")
val b = B()
b.foo()
println("")
println("lambda")
foo(0, 2) { println("Fill in all arguments") }
foo(0) { println("Fill in some arguments") }
foo(baz = 2) { println("Fill in specific arguments") }
foo(qux = { println("Fill in the last argument") })
foo { println("Fill in the last argument") }
println("")
println("variable number of arguments (vararg)")
foo(strings = *arrayOf("a", "b", "c"))
println("")
println("Unit-returning functions")
println("fun bar(): Unit {} is equivalent to fun bar() {}")
println("")
println("infix functions")
println("aaa".add(3))
println("aaa" add 3)
this bar "bbb"
bar("bbb")
println("")
println("Local functions")
f()
}
private fun double(x: Int): Int = 2 * x
private fun triple(x: Int) = 3 * x
private fun isPrimeNumber(x: Int): Boolean {
var factors = 0
for (i in 2..x / 2) {
if (x % i == 0) {
factors++
}
}
return factors == 0
}
private fun read(bytes: ByteArray, off: Int = 0, len: Int = bytes.size) {
val s = String(bytes, off, len)
println(s)
}
open class A {
open fun foo() {}
}
class B : A() {
override fun foo() {}
}
private fun foo(bar: Int = 0, baz: Int = 1, qux: () -> Unit) {
qux()
}
// In Java -> public void println(String.. args) { }
private fun foo(vararg strings: String) {
println(strings[0])
}
private infix fun String.add(x: Int) = "$this$x"
private infix fun bar(s: String) = println(s)
private fun f() {
fun f() {
println("call f() in f()")
}
print("call f() -> ")
f()
}
}