随笔分类 - Kotlin
摘要:Let's see following code: println(colors.reduce { acc, curr -> "$acc, $curr" }) // red, blue, green, black val myMap = mapOf(1 to "one", 2 to "two", 3
阅读全文
摘要:In kotlin, "fold" is same in Javascript's reduce. "fold" has initial value "reduce" in kotlin, you don't give initial value val colors = listOf("red",
阅读全文
摘要:Java way: package com.rsk.java; import java.util.function.Function; public class FPExample { public static Function<String, String> toSentenceCase = x
阅读全文
摘要:For example you have the Java class: package com.rsk.java; import com.rsk.kotlin.Customer; import com.rsk.kotlin.CustomerDatabase; import java.util.*;
阅读全文
摘要:buildscript { ext.kotlin_version = '1.1-M01' ext.kotlin_gradle_version = '1.1-M01' ext.mokito_kotlin_version = '0.6.0' ext.spek_version = '1.0.89' ext
阅读全文
摘要:Create a Kotlin class: package com.rsk.kotlin class Meeting(val title: String) { // in Java, you can use getLocation and setLocation var location = ""
阅读全文
摘要:Create a Java class: package com.rsk.java; import org.jetbrains.annotations.Nullable; public class Person { private String name; private int age; priv
阅读全文
摘要:package com.rsk interface Signatory { fun sign() } open class Person(val name: String, var age: Int, var isMarried: Boolean = false): Signatory { over
阅读全文
摘要:If your method is just a single line, it is good to use method expressions: interface Signatory { fun sign() } class Person: Signatory { override fun
阅读全文
摘要:In Kotlin, there is no static methods, but we can use companion object which works the same as static methods. For example, a class: package com.rsk i
阅读全文
摘要:First version: fun main(args: Array<String>) { val providers = Providers() val details = providers.getAllProviders() details.forEach { detail -> print
阅读全文
摘要:fun getAllProviders(fn: (String, String) -> Unit) { // Unit is just like void val allProviders = Providers.getProviders() val it = allProviders.iterat
阅读全文
摘要:fun main(args: Array<String>) { val ipToCount = mutableMapOf<String, Int>() File("src/ips.txt").forEachLine { val previous = ipToCount.getOrDefault(it
阅读全文
摘要:fun main(args: Array<String>) { val namesToAges = mapOf(Pair("Peter", 24), Pair("Roger", 42)) val namesToAges2 = mapOf( "Peter" to 24, "Roger" to 42 )
阅读全文
摘要:import java.io.File fun main(args: Array<String>) { File("src/inputFile.txt").forEachLine { print(it) // it refers to each line's content } }
阅读全文
摘要:var letters = str.toLowerCase().toCharArray().toHashSet()
阅读全文
摘要:import java.util.* fun main(args: Array<String>) { val number = Random().nextInt(100) + 1 var input: String? var guess = -1 while(guess!=number) { pri
阅读全文
摘要://vararg: just like ...args in js class Stack<T>(vararg val items: T) { val elements = items.toMutableList() fun push(element: T) { elements.add(eleme
阅读全文
摘要://vararg: just like ...args in js class Stack<T>(vararg val items: T) { val elements = items.toMutableList() fun push(element: T) { elements.add(eleme
阅读全文
摘要:private protected internal: accessable for the project / module public https://kotlinlang.org/docs/reference/visibility-modifiers.html
阅读全文