1. apply

1
2
3
4
5
/**
* Calls the specified function [block] with `this` value as its receiver and returns `this` value.
*/
@kotlin.internal.InlineOnly
public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }
1
2
3
4
5
6
AddEditTaskFragment.newInstance().apply {
arguments = Bundle().apply {
putString(AddEditTaskFragment.ARGUMENT_EDIT_TASK_ID,
intent.getStringExtra(AddEditTaskFragment.ARGUMENT_EDIT_TASK_ID))
}
}

2. with

1
2
3
4
5
/**
* Calls the specified function [block] with the given [receiver] as its receiver and returns its result.
*/
@kotlin.internal.InlineOnly
public inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()
1
2
3
4
5
with(items) {
clear()
addAll(tasksToShow)
empty.set(isEmpty())
}

3. let

1
2
3
4
5
/**
* Calls the specified function [block] with `this` value as its argument and returns its result.
*/
@kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R = block(this)
1
2
3
taskId?.let {
tasksRepository.getTask(it, this)
}

4. also (V1.1)

1
2
3
4
5
6
/**
* Calls the specified function [block] with `this` value as its argument and returns `this` value.
*/
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this }
1
2
3
4
supportFragmentManager.findFragmentById(R.id.contentFrame) ?:
StatisticsFragment.newInstance().also {
replaceFragmentInActivity(it, R.id.contentFrame)
}

5. run

1
2
3
4
5
6
7
8
9
10
11
/**
* Calls the specified function [block] and returns its result.
*/
@kotlin.internal.InlineOnly
public inline fun <R> run(block: () -> R): R = block()
/**
* Calls the specified function [block] with `this` value as its receiver and returns its result.
*/
@kotlin.internal.InlineOnly
public inline fun <T, R> T.run(block: T.() -> R): R = block()
1
2
3
4
5
6
activity.findViewById<FloatingActionButton>(R.id.fab_add_task).run {
setImageResource(R.drawable.ic_add)
setOnClickListener {
viewDataBinding.viewmodel?.addNewTask()
}
}

6. repeat

1
2
3
4
5
6
7
8
9
10
11
/**
* Executes the given function [action] specified number of [times].
*
* A zero-based index of current iteration is passed as a parameter to [action].
*/
@kotlin.internal.InlineOnly
public inline fun repeat(times: Int, action: (Int) -> Unit) {
for (index in 0..times - 1) {
action(index)
}
}

7. takeIf (V1.1)

1
2
3
4
5
6
7
8
9
10
11
/**
* Executes the given function [action] specified number of [times].
*
* A zero-based index of current iteration is passed as a parameter to [action].
*/
@kotlin.internal.InlineOnly
public inline fun repeat(times: Int, action: (Int) -> Unit) {
for (index in 0..times - 1) {
action(index)
}
}

8. takeUnless (V1.1)

1
2
3
4
5
6
/**
* Returns `this` value if it _does not_ satisfy the given [predicate] or `null`, if it does.
*/
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.takeUnless(predicate: (T) -> Boolean): T? = if (!predicate(this)) this else null