This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T : Number> oneHalf(value: T): Double { | |
return value.toDouble() / 2.0 | |
} | |
>>> println(oneHalf(3)) | |
1.5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface List<T> { | |
operator fun get(index: Int): T | |
// ... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> val <T> x: T = TODO() | |
ERROR: type parameter of a property must be used in its receiver type |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val authors = listOf("Dmitry", "Svetlana") | |
val readers = mutableListOf<String>(/* ... */) | |
fun <T> List<T>.filter(predicate: (T) -> Boolean): List<T> | |
>>> readers.filter { it !in authors } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val letters = ('a'..'z').toList() | |
println(letters.slice<Char>(0..2)) | |
println(letters.slice(10..13)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Error:(12, 34) Kotlin: Illegal usage of inline-parameter 'predicate' in 'public inline fun inlinedFilter(list: List<Int>, predicate: (Int) -> Boolean): List<Int> defined in functiontypes in file Inline.kt'. Add 'noinline' modifier to the parameter declaration |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun inlinedFilter(list : List<Int>, predicate : (Int) -> Boolean) : List<Int>{ | |
val function = predicate | |
return list.filter(predicate) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static final boolean filterLessThanTwo(int input) { | |
return input < 2; | |
} | |
public static final void lambdaInstanceTest() { | |
List list$iv = CollectionsKt.listOf(new Integer[]{1, 2, 3}); | |
Iterable $receiver$iv$iv$iv = (Iterable)list$iv; | |
Collection destination$iv$iv$iv$iv = (Collection)(new ArrayList()); | |
Iterator var4 = $receiver$iv$iv$iv.iterator(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static final boolean filterLessThanTwo(int input) { | |
return input < 2; | |
} | |
public static final void lambdaInstanceTest() { | |
lambdaInstance((Function1) new Function1<Integer, Boolean>() { | |
@Override | |
public Boolean invoke(Integer integer) { | |
return filterLessThanTwo(integer); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun inlinedFilter(list : List<Int>, predicate : (Int) -> Boolean) : List<Int>{ | |
return list.filter(predicate) | |
} | |
fun filterLessThanTwo(input: Int) = input < 2 | |
fun lambdaInstance(predicate: (Int) -> Boolean) { | |
val list = listOf(1,2,3) | |
val newList = inlinedFilter(list, predicate) | |
println(newList) |