Skip to content

Commit 492a598

Browse files
committed
More Kotlin
1 parent b191be5 commit 492a598

File tree

7 files changed

+89
-23
lines changed

7 files changed

+89
-23
lines changed

cpp/scope_operator.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <cassert>
2+
int x = 1;
3+
4+
namespace N {
5+
int x = 2;
6+
};
7+
8+
struct C {
9+
int x = 3;
10+
void f() {
11+
int x = 4;
12+
assert(::x == 1 && N::x == 2 && C::x == 3 && x == 4);
13+
}
14+
};
15+
16+
int main() {
17+
C().f();
18+
}

js/strings.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from "assert/strict"
22

3-
const s = "\u{1f363}\u{2669}"
4-
assert(s.length === 3) // this is honestly rather silly
3+
const s = "\u{1f363}\u{2669}" // SUSHI, QUARTER NOTE
4+
assert(s.length === 3) // based on internal storage units
55
assert(Array.from(s).length === 2) // ok, 2 code points
66
assert(Buffer.from(s, "utf8").length === 7) // ok, 7 bytes utf-8

kotlin/binding.kts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fun display(displayer: () -> Unit) {
2+
var name = "new"
3+
displayer() // what if "name" was free in this function?
4+
}
5+
6+
fun main() {
7+
var name = "old"
8+
fun printName() { print(name) }
9+
display(::printName)
10+
}
11+
12+
main()

kotlin/closures.kts

-20
This file was deleted.

kotlin/coroutines.main.kts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1")
2+
import kotlinx.coroutines.*
3+
4+
runBlocking {
5+
repeat(5) {
6+
launch {
7+
println("Coroutine $it running on ${Thread.currentThread().name}.")
8+
delay((1000L..5000L).random())
9+
println("Coroutine $it finished on ${Thread.currentThread().name}.")
10+
}
11+
}
12+
println("All coroutines launched")
13+
}

kotlin/poke_api.main.kts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@file:DependsOn(
2+
"org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1",
3+
"com.squareup.okhttp3:okhttp:4.11.0",
4+
"org.json:json:20230618",
5+
)
6+
7+
import kotlinx.coroutines.Dispatchers
8+
import kotlinx.coroutines.launch
9+
import kotlinx.coroutines.runBlocking
10+
import okhttp3.OkHttpClient
11+
import okhttp3.Request
12+
import okhttp3.Response
13+
import org.json.JSONObject
14+
15+
val baseUrl = "https://pokeapi.co/api/v2/pokemon/"
16+
val pokemonNames = listOf("ditto", "pikachu", "mew", "weedle", "eevee")
17+
18+
data class PokeInfo(val name: String, val height: Int, val weight: Int)
19+
20+
suspend fun fetchPokemon(name: String) {
21+
println("Fetching $name on ${Thread.currentThread().name}")
22+
val request = Request.Builder().url("$baseUrl$name").build()
23+
val response = OkHttpClient().newCall(request).execute()
24+
if (!response.isSuccessful) {
25+
println("Cannot fetch $name: HTTP ${response.code}")
26+
return
27+
}
28+
val jsonObject = JSONObject(response.body?.string() ?: return)
29+
println(PokeInfo(
30+
jsonObject.getString("name"),
31+
jsonObject.getInt("height"),
32+
jsonObject.getInt("weight")))
33+
}
34+
35+
runBlocking {
36+
for (name in pokemonNames) {
37+
launch(Dispatchers.IO) {
38+
fetchPokemon(name)
39+
}
40+
}
41+
println("The fetches are underway")
42+
}
43+
println("The fetches are done")
44+

kotlin/test.sh

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
kotlin -J-ea animals.kts && \
33
kotlin -J-ea binary_tree.kts && \
44
kotlin circle.kts && \
5-
kotlin closures.kts && \
65
kotlin codepoints.kts && \
76
kotlin collections.kts && \
87
kotlin companions.kts && \

0 commit comments

Comments
 (0)