Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New samples from kotlin-web-site #721

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fun main() {
//sampleStart
// Creates an array of Int of size 5 with values
// Creates an array of Int of size 5 with the values initialized to zero
val exampleArray = IntArray(5)
println(exampleArray.joinToString())
// 0, 0, 0, 0, 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
fun main() {
//sampleStart
var x = 5 // `Int` type is inferred
// Declares the variable x and initializes it with the value of 5
var x: Int = 5
// Reassigns a new value of 6 to the variable x
x += 1
// 6
//sampleEnd
println("x = $x")
println(x)
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
fun main() {
//sampleStart
val PI = 3.14
var x = 0

fun incrementX() {
x += 1
}
// Declares the variable x with the value of 5;`Int` type is inferred
val x = 5
// 5
//sampleEnd

fun main() {
println("x = $x; PI = $PI")
incrementX()
println("incrementX()")
println("x = $x; PI = $PI")
println(x)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
class Rectangle(val height: Double, val length: Double) {
val perimeter = (height + length) * 2
}
fun main() {
//sampleStart
val rectangle = Rectangle(5.0, 2.0)
println("The perimeter is ${rectangle.perimeter}")
// Initializes the variable x at the moment of declaration; type is not required
val x = 5
// Declares the variable c without initialization; type is required
val c: Int
// Initializes the variable c after declaration
c = 3
// 5
// 3
//sampleEnd
println(x)
println(c)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
fun main() {
//sampleStart
var a = 1
// simple name in template:
val s1 = "a is $a"

a = 2
// arbitrary expression in template:
val s2 = "${s1.replace("is", "was")}, but now is $a"
val PI = 3.14
var x = 0

fun incrementX() {
x += 1
}
// x = 0; PI = 3.14
// incrementX()
// x = 1; PI = 3.14
//sampleEnd
println(s2)

fun main() {
println("x = $x; PI = $PI")
incrementX()
println("incrementX()")
println("x = $x; PI = $PI")
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
//sampleStart
fun maxOf(a: Int, b: Int): Int {
if (a > b) {
return a
} else {
return b
}
class Rectangle(val height: Double, val length: Double) {
val perimeter = (height + length) * 2
}
//sampleEnd

fun main() {
println("max of 0 and 42 is ${maxOf(0, 42)}")
val rectangle = Rectangle(5.0, 2.0)
println("The perimeter is ${rectangle.perimeter}")
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
fun main() {
//sampleStart
fun maxOf(a: Int, b: Int) = if (a > b) a else b
var a = 1
// simple name in template:
val s1 = "a is $a"

a = 2
// arbitrary expression in template:
val s2 = "${s1.replace("is", "was")}, but now is $a"
//sampleEnd

fun main() {
println("max of 0 and 42 is ${maxOf(0, 42)}")
println(s2)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
fun main() {
//sampleStart
val items = listOf("apple", "banana", "kiwifruit")
for (item in items) {
println(item)
fun maxOf(a: Int, b: Int): Int {
if (a > b) {
return a
} else {
return b
}
}
//sampleEnd

fun main() {
println("max of 0 and 42 is ${maxOf(0, 42)}")
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
fun main() {
//sampleStart
val items = listOf("apple", "banana", "kiwifruit")
for (index in items.indices) {
println("item at $index is ${items[index]}")
}
fun maxOf(a: Int, b: Int) = if (a > b) a else b
//sampleEnd

fun main() {
println("max of 0 and 42 is ${maxOf(0, 42)}")
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
fun main() {
//sampleStart
val items = listOf("apple", "banana", "kiwifruit")
var index = 0
while (index < items.size) {
println("item at $index is ${items[index]}")
index++
for (item in items) {
println(item)
}
//sampleEnd
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
fun main() {
//sampleStart
fun describe(obj: Any): String =
when (obj) {
1 -> "One"
"Hello" -> "Greeting"
is Long -> "Long"
!is String -> "Not a string"
else -> "Unknown"
val items = listOf("apple", "banana", "kiwifruit")
for (index in items.indices) {
println("item at $index is ${items[index]}")
}
//sampleEnd

fun main() {
println(describe(1))
println(describe("Hello"))
println(describe(1000L))
println(describe(2))
println(describe("other"))
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
fun main() {
//sampleStart
val x = 10
val y = 9
if (x in 1..y+1) {
println("fits in range")
val items = listOf("apple", "banana", "kiwifruit")
var index = 0
while (index < items.size) {
println("item at $index is ${items[index]}")
index++
}
//sampleEnd
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
fun main() {
//sampleStart
val list = listOf("a", "b", "c")

if (-1 !in 0..list.lastIndex) {
println("-1 is out of range")
}
if (list.size !in list.indices) {
println("list size is out of valid list indices range, too")
fun describe(obj: Any): String =
when (obj) {
1 -> "One"
"Hello" -> "Greeting"
is Long -> "Long"
!is String -> "Not a string"
else -> "Unknown"
}
//sampleEnd

fun main() {
println(describe(1))
println(describe("Hello"))
println(describe(1000L))
println(describe(2))
println(describe("other"))
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
fun main() {
//sampleStart
for (x in 1..5) {
print(x)
val x = 10
val y = 9
if (x in 1..y+1) {
println("fits in range")
}
//sampleEnd
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
fun main() {
//sampleStart
for (x in 1..10 step 2) {
print(x)
val list = listOf("a", "b", "c")

if (-1 !in 0..list.lastIndex) {
println("-1 is out of range")
}
println()
for (x in 9 downTo 0 step 3) {
print(x)
if (list.size !in list.indices) {
println("list size is out of valid list indices range, too")
}
//sampleEnd
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
fun main() {
val items = listOf("apple", "banana", "kiwifruit")
//sampleStart
for (item in items) {
println(item)
for (x in 1..5) {
print(x)
}
//sampleEnd
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
fun main() {
val items = setOf("apple", "banana", "kiwifruit")
//sampleStart
when {
"orange" in items -> println("juicy")
"apple" in items -> println("apple is fine too")
for (x in 1..10 step 2) {
print(x)
}
println()
for (x in 9 downTo 0 step 3) {
print(x)
}
//sampleEnd
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
fun main() {
val items = listOf("apple", "banana", "kiwifruit")
//sampleStart
val fruits = listOf("banana", "avocado", "apple", "kiwifruit")
fruits
.filter { it.startsWith("a") }
.sortedBy { it }
.map { it.uppercase() }
.forEach { println(it) }
for (item in items) {
println(item)
}
//sampleEnd
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
fun parseInt(str: String): Int? {
return str.toIntOrNull()
}

fun main() {
val items = setOf("apple", "banana", "kiwifruit")
//sampleStart
fun printProduct(arg1: String, arg2: String) {
val x = parseInt(arg1)
val y = parseInt(arg2)

// Using `x * y` yields error because they may hold nulls.
if (x != null && y != null) {
// x and y are automatically cast to non-nullable after null check
println(x * y)
when {
"orange" in items -> println("juicy")
"apple" in items -> println("apple is fine too")
}
else {
println("'$arg1' or '$arg2' is not a number")
}
}
//sampleEnd

fun main() {
printProduct("6", "7")
printProduct("a", "7")
printProduct("a", "b")
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,10 @@
fun parseInt(str: String): Int? {
return str.toIntOrNull()
}

fun printProduct(arg1: String, arg2: String) {
val x = parseInt(arg1)
val y = parseInt(arg2)

fun main() {
//sampleStart
// ...
if (x == null) {
println("Wrong number format in arg1: '$arg1'")
return
}
if (y == null) {
println("Wrong number format in arg2: '$arg2'")
return
}

// x and y are automatically cast to non-nullable after null check
println(x * y)
val fruits = listOf("banana", "avocado", "apple", "kiwifruit")
fruits
.filter { it.startsWith("a") }
.sortedBy { it }
.map { it.uppercase() }
.forEach { println(it) }
//sampleEnd
}

fun main() {
printProduct("6", "7")
printProduct("a", "7")
printProduct("99", "b")
}
Loading
Loading