This file contains 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 ValueHolder<V> { | |
val value: V | |
} | |
class IntHolder : ValueHolder<Int> { | |
override val value: Int | |
get() = Random().nextInt() | |
} | |
fun main() { | |
val sample = IntHolder() | |
println(sample.value) //260078462 |
This file contains 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 main() { | |
smallList() | |
smallSequence() | |
} | |
fun smallList() = (0..5) | |
.filter { print("list filter($it) "); it % 2 == 0 } | |
.map { print("list map($it) "); it * it } | |
.first() |
This file contains 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 <reified T> printTypeName() { | |
println(T::class.simpleName) | |
} | |
fun main() { | |
printTypeName<String>() | |
println(String::class.simpleName) | |
} |
This file contains 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 <T, R : Comparable<R>> Iterable<T>.sortedBy(crossinline selector: (T) -> R?): List<T> { | |
return sortedWith(compareBy(selector)) | |
} |
This file contains 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 computeValues( | |
number: Int, doubleValue: (number: Int) -> Unit, | |
noinline tripleValue: (number: Int) -> Unit) { | |
doubleValue.invoke(number) | |
tripleValue.invoke(number) | |
} | |
fun main() { | |
val number = 7; |
This file contains 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
var priceBooks = 0.0 | |
val prefixPriceDetails = "The current sum is " | |
books.forEach { | |
priceBooks += it.price | |
println("$prefixPriceDetails $priceBooks") | |
} |
This file contains 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 titleStartsWithS(book: Book) = book.title.startsWith("S") | |
fun lengthOfTitleGraterThan5(book: Book) = book.title.length > 5 | |
fun authorStartsWithB(book: Book) = book.author.startsWith("B") | |
val book1 = Book("Start with why", "Simon Sinek") | |
val book2 = Book("Dare to lead", "Brene Brown") | |
val books = listOf(book1, book2) | |
// this code should be improved | |
val filteredBooks = books |
This file contains 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
class Book(val title: String, val author: String) { | |
// impure function | |
fun getBookDetails() = "$title - $author" | |
} | |
// pure function | |
fun getBookDetails(title: String, author: String) = "$title - $author" |
This file contains 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> T.exhaustive: T | |
get() = this | |
fun displayMovieResult(movieResult: MovieSearchResult) { | |
when(movieResult) { | |
is MovieFound -> println("yey, we found the movie") | |
is MovieNotFound -> TODO() | |
}.exhaustive | |
} |
This file contains 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
sealed class MovieSearchResult | |
data class MovieFound(val movie: Movie) : MovieSearchResult() | |
object MovieNotFound : MovieSearchResult() | |
object DatabaseOffline : MovieSearchResult() | |
fun displayMovieResult(movieResult: MovieSearchResult) { | |
when(movieResult) { | |
is MovieFound -> println("yey, we found the movie") | |
is MovieNotFound -> TODO() | |
} |