class Sample() {
fun foo() { print("Foo") }
}
fun recursiveFactorial(n: Long) : Long {
return if (n <= 1) {
n
} else {
n * recursiveFactorial(n - 1)
}
}
fun <T> singletonList(item: T): List<T> {
/*...*/
}
fun <T> MutableList<T>.swap(index1: Int, index2: Int) {
val tmp = this[index1] // 'this' corresponds to the list
this[index1] = this[index2]
this[index2] = tmp
}
fun runTransformation(f: (String, Int) -> String): String {
return f("hello", 3)
}
public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T> {
return filterTo(ArrayList<T>(), predicate)
}
val people = listOf(Person("Alice", 29), Person("Bob", 31))
people.foreach{
print(it.name) // output : Alice Bob
}
val people = listOf(Person("Alice", 29), Person("Bob", 31))
val pList = people.filter{
it.age > 30
}
print(pList) // output: [Person(name=Bob, age=31)]
val people = listOf(Person("Alice", 29), Person("Bob", 31))
val pList = people.map{
it.name
}
print(pList) // output: [Alice, Bob]
val people = listOf(Person("Alice", 29), Person("Bob", 31))
val pList = people.map{
it.name
}.reduce{ acc, s->
"$acc,$s"
}
print(pList) // output: Alice,Bob
,
,
,
val people = listOf(
Person("Alice", 29, arrayListOf("A", "B")),
Person("Bob", 31, arrayListOf("C", "D")))
val books = people.flatmap{
it.bookList
}
print(books) // output: [A, B, C, D]
[
[
[
]
]
]
people.map(People::name).fliter{ it.startWith("A")}
people.asSequence()
.map(Person::name)
.filter { it.startsWith("A") }
.toList()
people.filter{ it.name.startWith("A")}.map(People::name)
//better than
people.map(People::name).filter{ it.name.startWith("A")}
/* Java */
public interface OnClickListener{
void onClick(View v);
}
/* Java */
public class Button {
public void setOnClickListener(OnClickListener l) { ... }
}
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
...
}
}
/* kotlin */
button.setOnClickListener { view -> ... }
fun sum(a: Int, b: Int, lambda: (result: Int) -> Unit): Int {
val r = a + b
lambda.invoke(r)
return r
}
fun main(args: Array<String>) {
sum(1, 2) { println("Result is: $it") }
}
//decompile
public static final void main(@NotNull String[] args) {
//...
sum(1, 2, (Function1)null.INSTANCE);
}
inline fun sum(a: Int, b: Int, lambda: (result: Int) -> Unit): Int {
val r = a + b
lambda.invoke(r)
return r
}
fun main(args: Array<String>) {
sum(1, 2) { println("Result is: $it") }
}
//decompile
public static final void main(@NotNull String[] args) {
//...
byte a$iv = 1;
int b$iv = 2;
int r$iv = a$iv + b$iv;
String var9 = "Result is: " + r$iv;
System.out.println(var9);
}
loop@ for (i in 1..100) {
for (j in 1..100) {
if (...) break@loop
}
}
loop@ for (i in 1..100) {
for (j in 1..100) {
if (...) continue@loop
}
}
fun foo() {
listOf(1, 2, 3, 4, 5).forEach {
// non-local return directly to the caller of foo()
if (it == 3) return
print(it)
}
println("this point is unreachable")
}
output: 12
fun foo() {
listOf(1, 2, 3, 4, 5).forEach lit@{
// local return to the caller of the lambda
if (it == 3) return@lit
print(it)
}
print(" done with explicit label")
}
output: 1245 done with explicit label
fun foo() {
listOf(1, 2, 3, 4, 5).forEach {
//// local return to the caller of the lambda
if (it == 3) return@forEach
print(it)
}
print(" done with implicit label")
}
output: 1245 done with explicit label
fun foo() {
listOf(1, 2, 3, 4, 5).forEach(fun(value: Int) {
// local return to the caller of the anonymous fun
if (value == 3) return
print(value)
})
print(" done with anonymous function")
}
output: 1245 done with anonymous function
fun foo() {
run loop@{
listOf(1, 2, 3, 4, 5).forEach {
// non-local return from the lambda passed to run
if (it == 3) return@loop
print(it)
}
}
print(" done with nested loop")
}
output: 12 done with nested loop
return@a 1