サポートプラットフォーム Kotlin/JVM on Linux(x86_64 and arm64) Kotlin/JVM on Windows(x86_64) Kotlin/JVM on macOS(x86_64 and arm64) Kotlin/JVM on Android(x86_64 and arm64), starting with API version 24 Kotlin/JS + WebAssembly in browsers Kotlin/Native on iOS(arm64 and x64) Kotlin/Native on macOS (arm64 and x64)
fun main(){ val l = (1..1000000).toList() val s = System.currentTimeMillis() val m = l.filter { it in 3..500000 } val s2 = System.currentTimeMillis() val n = l.filter { it >= 3 && it <= 500000 } val s3 = System.currentTimeMillis() println(s2-s) println(s3-s2) println(m.size) println(n.size) } 0089デフォルトの名無しさん2023/07/21(金) 01:25:51.58ID:esLQ8StN>>88 ほぼ同じになるのではないかな。 it in 3..500000 は IntRange(3, 500000).contains(it) って書いたのと同じだし。 contains() メソッドのソースを見るとこう書いてある。
override fun contains(value: Int): Boolean = first <= value && value <= last
初心者がここまで追加の問いをできるとは思えんが 0105982023/08/07(月) 15:30:19.77ID:bKCC1GJD kotlinc -classpath /home/hoge/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlinx/kotlinx-coroutines-core-jvm/1.6.4/2c997cd1c0ef33f3e751d3831929aeff1390cb30/kotlinx-coroutines-core-jvm-1.6.4.jar c001.kt -include-runtime -d c001.jar コンパイルは出来ましたが Exception in thread "main" java.lang.NoClassDefFoundError: kotlinx/coroutines/GlobalScope at C001Kt.main(c001.kt:4) at C001Kt.main(c001.kt) Caused by: java.lang.ClassNotFoundException: kotlinx.coroutines.GlobalScope ...
chatgptに聞いたりググったりして gradle init --dsl kotlin 2 enter 4 enter ... などとしてみてapp/build.gradle.ktsのdependenciesに implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3") を書いて./gradlew runしてみましたが > Task :app:compileKotlin w: file:///hoge/kt/c001/app/src/main/kotlin/c001/App.kt:4:5 This is a delicate API and its use requires care. Make sure you fully read and understand documentation of the declaration that is marked as a delicate API.
import kotlin.reflect.KProperty public interface A{ fun test() : Unit operator fun getValue(b : Nothing?, c:KProperty<*>) : String } public val impl = object : A{ override fun test() = println("hello world") override fun getValue(b: Nothing?, c: KProperty<*>) = "str" } fun main(){ val da by impl da.test() } 0123デフォルトの名無しさん2023/09/04(月) 10:58:04.18ID:3ylJB9rm>>122 移譲の形式でval da by implとした場合、da.test()はdaのgetValue(....)した結果をレシーバとしてtest()メソッドを呼ぶという意味になるよね?つまり"str".test()という意味になってエラーになるだろう
移譲でないval da = implとすればda.test()は呼べるはず 01241182023/09/04(月) 14:21:13.43ID:FcZAE9nJ>>123 ありがとうございます。一人では絶対にわからなかったと思います getValueにはそういう意味があるのですね 以下のように書き換えてhello worldが表示できました
import kotlin.reflect.KProperty public interface A{ fun test() : Unit operator fun getValue(b : Nothing?, c:KProperty<*>) : A } public val impl = object : A{ override fun test() = println("hello world") override fun getValue(b: Nothing?, c: KProperty<*>) = this