Skip to content

Commit 2f4d5d1

Browse files
committed
doc(): clear notes and add examples
1 parent d4ef509 commit 2f4d5d1

File tree

11 files changed

+209
-10
lines changed

11 files changed

+209
-10
lines changed

README.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
# Kotlin website
2-
[![Official project][project-badge]][project-url]
3-
[![Qodana Code Quality Check](https://github.com/JetBrains/kotlin-web-site/actions/workflows/qodana-code-quality-check.yml/badge.svg)](https://github.com/JetBrains/kotlin-web-site/actions/workflows/qodana-code-quality-check.yml)
42

5-
This repository is the source for [https://kotlinlang.org](https://kotlinlang.org).
6-
7-
* [Website structure](#website-structure)
8-
* [Contribution](#contribution)
9-
* [Local deployment](#local-deployment)
10-
* [Feedback and issues](#feedback-and-issues)
11-
12-
<a id="project-structure"></a>
3+
* == [https://kotlinlang.org website](https://kotlinlang.org)
134

145
## Website structure
156

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// vs Java
2+
// 30% fewer lines
3+
4+
data class Book (
5+
val title: String,
6+
val year: Int
7+
// AUTOMATICALLY generate
8+
// 1. equals(),
9+
// 2. hashCode(),
10+
// 3. toString(),
11+
// 4. copy()
12+
)
13+
14+
// 1!-expression body
15+
fun century(year: Int) = (year - 1) / 100 + 1 // Top-level function,
16+
17+
fun main() {
18+
// construct a list -- WITHOUT -- `new` keyword
19+
val books = listOf(
20+
Book("Don Quixote", 1605),
21+
Book("The Lord of the Rings", 1955)
22+
)
23+
val classics = books.filter { century(it.year) < 20 } // Trailing 1!-argument lambda
24+
println("Classic books: $classics") // AUTOMATICALLY call Book.toString()
25+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import kotlin.math.absoluteValue
2+
3+
fun main() {
4+
val dates = listOf(1 to "January", 13 to "May", 22 to "September", 23 to "December")
5+
6+
// 1. destructure declarations
7+
dates.forEach { (day, month) ->
8+
println("${day.ordinal()} of $month")
9+
}
10+
11+
// 3. scope functions (apply, also, let, takeIf)
12+
Window(300, 200, true).also(::showWindow)
13+
createEmptyWindow()
14+
.apply {
15+
width = 300
16+
height = 200
17+
isVisible = true
18+
}.also { w ->
19+
showWindow(w)
20+
}
21+
22+
// 4. safe calls & null handling
23+
issueById["13456"]
24+
?.takeIf { it.status == Status.FIXED }
25+
?.let {
26+
println("We've fixed this: $it")
27+
}
28+
}
29+
30+
// 2. Extension function
31+
fun Int.ordinal() = this.absoluteValue.let { iAbs ->
32+
val suffix = if (iAbs % 100 in 11..13) "th" else
33+
// 5. smart cast & when expressions
34+
when (iAbs % 10) {
35+
1 -> "st"
36+
2 -> "nd"
37+
3 -> "rd"
38+
else -> "th"
39+
}
40+
"$this$suffix"
41+
}
42+
43+
data class Window(var width: Int, var height: Int, var isVisible: Boolean)
44+
45+
fun createEmptyWindow() = Window(0, 0, false)
46+
47+
fun showWindow(window: Window) {
48+
println("Showing $window")
49+
}
50+
51+
enum class Status { OPEN, FIXED, IN_PROGRESS }
52+
53+
data class Issue(val status: Status)
54+
55+
val issueById = mutableMapOf(
56+
"13456" to Issue(Status.FIXED)
57+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Use any existing JVM library or framework
2+
// Call Kotlin code from Java without an issue
3+
4+
@SpringBootApplication
5+
class DemoApplication
6+
7+
fun main(args: Array<String>) {
8+
runApplication<DemoApplication>(*args)
9+
}
10+
11+
@RestController
12+
class MessageResource {
13+
@GetMapping
14+
fun index(): List&lt;Message> = listOf(
15+
Message("1", "Hello!"),
16+
Message("2", "Bonjour!"),
17+
Message("3", "Privet!"),
18+
)
19+
}
20+
21+
data class Message(val id: String?, val text: String)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Kotlin values Examples
2+
3+
## Prerequisites
4+
* [Install the compiler locally](https://kotlinlang.org/docs/command-line.html#install-the-compiler)
5+
6+
## How to create & run an application?
7+
* application
8+
* == 1! `.kt` file
9+
* `kotlinc NameOfTheFile.kt -include-runtime -d outputPathOrName.jar`
10+
* create the java application
11+
* `java -jar outputPathOrName.jar`
12+
* run the application
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Apps built with Kotlin are 20% less likely to crash
2+
// (based on Google's internal data)
3+
4+
fun printMessagesUppercased(messages: List&lt;String?>) { // List elements can be nulls
5+
// messages.add(Message("Java")) // ERROR: List is read-only
6+
messages.onEachIndexed { index, msg ->
7+
print("\nMessage #$index: ")
8+
// print(msg.uppercase()) // ERROR: `msg` can be null
9+
msg?.let { // Print only if `msg` is not null
10+
print(it.uppercase()) // OK, `it` is String
11+
}
12+
}
13+
}
14+
fun main() {
15+
val messages = mutableListOf("hello", null, "world")
16+
// messages = mutableListOf("!!!") // ERROR: can't reassign a value
17+
messages.add("Kotlin") // OK: the list is mutable
18+
printMessagesUppercased(messages) // Pass the list as read-only
19+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import kotlinx.coroutines.*
2+
import kotlinx.coroutines.flow.*
3+
4+
//sampleStart
5+
// More than 50% of professional developers who use coroutines
6+
// report increased productivity
7+
// (based on Google's internal data)
8+
9+
fun main() = runBlocking {
10+
val start = System.currentTimeMillis()
11+
coroutineScope { // Create a scope for coroutines
12+
val waitingJob = launch { // Launching a coroutine
13+
waiting(start, 150)
14+
}
15+
countdownSignals(10, 300).collect { value -> // Collecting flow elements
16+
log(start, "Countdown: $value")
17+
}
18+
waitingJob.cancel() // Cancelling a coroutine
19+
}
20+
log(start, "Liftoff!") // Execution continues when all
21+
} // coroutines have finished
22+
//sampleEnd
23+
fun countdownSignals(n: Int, delayMillis: Long): Flow&lt;Int> = flow { // Flow builder
24+
for (i in (1..n).reversed()) {
25+
delay(delayMillis) // Delay in emitting signals
26+
emit(i) // Emit the flow element
27+
}
28+
}
29+
30+
// A function that can be suspended and resumed later
31+
suspend fun waiting(start: Long, delayMillis: Long) {
32+
while (currentCoroutineContext().isActive) { // Check coroutine's context
33+
log(start, "Waiting...")
34+
delay(delayMillis) // Waiting concurrently
35+
}
36+
}
37+
38+
fun log(start: Long, msg: String) {
39+
println("$msg after ${(System.currentTimeMillis() - start)/1000F}s")
40+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [here](CodeExamples)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [here](KotlinValues)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://blog.jetbrains.com/kotlin/?posts-count=24
2+
3+
* TODO:

0 commit comments

Comments
 (0)