-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.kt
223 lines (184 loc) · 5.31 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// DECLARATION
//declaration in standard way
fun tripple(): Int {
return 3*10
}
//return Unit type
fun printText() {
print("Hello world")
}
//declaration as expression
fun tripple() = 3*10 //note that return type declaration is not needed here
//infix
//usage
var result = tripple()
SomeClass().tripple() //if function is a member class
//some A class body
infix fun function(arg: Int) {} //declared as member function
//usage
var object = A()
A function 5 //infix function
A.function(5) //the same result as infix function above
// DECLARATION POSITION
//top-level declaration
//some class declaration { ... }
fun function() {
//do something
}
//local function declaration
fun calculate(): Int {
fun local1(): Int {
return 3*10
}
fun local2(): Int {
return 2*10
}
return local1() * local2()
}
// PARAMETERS
fun functionWithArgs(arg1: Int, arg2: String) {
//some body
print(arg2 + " " + arg1)
}
fun functionWithDefaultArgs(arg1: Int = 5, arg2: String) {
//some body
print(arg2 + " " + arg1)
}
//execution
functionWithArgs(10, "text") //text 10
functionWithDefaultArgs(10, "text") //text 10
functionWithDefaultArgs("text") //text 5
// parameters
//some A class
fun function(arg1: Int = 5, arg2: String) {}
//other B class extending A class
override fun function(arg1: Int = 10, arg2: String) {} //compiler error
override fun function(arg1: Int, arg2: String) {} //correct overriding
//declaration
fun <T> functionGenericType(item: T): List<T> {
var list: List<T> = listOf(item, item, item)
return list
}
//execution
var someList = functionGenericType<Int>(10)
someList = functionGenericType(10) //type can be missing if known from context
// NAMED ARGUMENTS
fun function(arg1: Int = 10, arg2: String) {}
function("text") //compile error
function(arg2 = "text") //correct executing using named arg
fun functionWithManyArgs(id: Int, number: Int, name: String, citizen: Boolean, adult: Boolean)
functionWithManyArgs(100, 1, "Jack", true, false) //proper but not easy to understand
functionWithManyArgs(id=100, number=1, name="Jack", citizen=true, adult=false) //more cleaner way
fun function(arg1: Int, arg2: String) {}
function(10, arg2="text") //correct
function(arg2="text", 10) //compile error
//lambda
fun function(arg1: Int = 10, arg2: Int = 5, lambda: () -> Unit) {}
function(3) { print("lambda action") } //arg1=3, arg2=5
function() { print("lambda action") } //arg1=10, arg2=5
// vararg
fun function(vararg args: Int, text: String) {} //vararg should be the last one arg
function(1,2,3, "text") //compile error
function(1,2,3, text="text") //correct execution with named arg
//vararg could be passed as list with * modifier
var array = arrayOf(1,2,3)
function(*array, text="text")
// high order
//take function as argument
fun functionWithFunArg(arg: Int = 10, lambda: (a: Int) -> Int): Int {
//do some work and use lambda function
return lambda(arg)
}
//execute function with function as argument
fun reflectionFun(a: Int) = a*a //just function
functionWithFunArg(3, ::reflectionFun) //passing reference to function by :: operator
var lambdaArg = { a: Int -> a*a } //function stored as variable
functionWithFunArg(3, lambdaArg) //passing function stored in variable
functionWithFunArg(3) { a: Int -> a*a } //passing anonymous function
//return function from function
fun functionWithFunReturn(arg: Int) : (a: Int) -> String {
return { a -> "value=" + (arg*a) }
}
//execute function with function type returned
var lambdaReturn = functionWithFunReturn(10)
lambdaReturn(2)
// inline
@PublishedApi
internal var internalMember = "internal"
private var privateMember = "private"
inline functionInline(arg: Int = 10, lambda: (a: Int) -> Unit) {
privateMember.toString() //compile error - not allowed
internalMember.toString() //it's okay
lambda(arg)
print("This is unreachable") //because of return statement in executing
}
//execution
functionInline {
//do something
return //it is possible because function is inline
}
// tail recursion
tailrec fun fibonacci(n: Int, a: Long, b: Long): Long {
return if (n == 0) b else fibonacci(n-1, a+b, a)
}
//instead of loop
fun fibonacciLoop(n: Int, a: Long, b: Long): Long {
var counter = 1
while(counter <= n) {
var sum=a+b
a=b
b=sum
counter++
}
}
// SCOPE FUNCTIONS
//this vs it
var employee = Employee().apply {
name = "Jack"
salary = 3000
}
var employee = Employee().also {
newEmployee ->
newEmployee.name = "Jack"
newEmployee.salary = 3000
}
//instead of
var employee = Employee()
employee.name = "Jack"
employee.salary = 3000
//return self or other type
var employee = Employee("Jack", 3000)
var tax = employee.let {
print(it)
it
}.let {
it.salary
}.let {
it * 0.2
}
var tax = employee.also {
print(it)
it //don't need to be here
}.also {
it.salary //it skips, employee object is passed
}.also {
it * 0.2 //compiler error - Employee instances is here, not Int
}
//instead of
print(employee)
var tax = employee.salary * 0.2
//normal vs extension function
var employee: Employee? = Employee("Jack", 3000)
with(employee) {
print(this?.name)
this?.salary = salary + 1000
}
employee?.run {
print(name)
salary = salary + 1000
}
//instead of
if(employee != null) {
print(employee.name)
employee.salary = employee.salary + 1000
}