1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/**
* Nested
/**
* Comments
*/
*/
/**
* This is a straightforward implementation of The Game of Life
* See http://en.wikipedia.org/wiki/Conway's_Game_of_Life
*/
package life
/*
* A field where cells live. Effectively immutable
*/
class Field(
val width: Int,
val height: Int,
// This function tells the constructor which cells are alive
// if init(i, j) is true, the cell (i, j) is alive
init: (Int, Int) -> Boolean
) {
private val live: Array<Array<Boolean>> = Array(height) { i -> Array(width) { j -> init(i, j) } }
private fun liveCount(i: Int, j: Int)
= if (i in 0..height - 1 &&
j in 0..width - 1 &&
live[i][j]) 1 else 0
// How many neighbors of (i, j) are alive?
fun liveNeighbors(i: Int, j: Int) =
liveCount(i - 1, j - 1) +
liveCount(i - 1, j) +
liveCount(i - 1, j + 1) +
liveCount(i, j - 1) +
liveCount(i, j + 1) +
liveCount(i + 1, j - 1) +
liveCount(i + 1, j) +
liveCount(i + 1, j + 1)
// You can say field[i, j], and this function gets called
operator fun get(i: Int, j: Int) = live[i][j]
}
/**
* This function takes the present state of the field
* and returns a new field representing the next moment of time
*/
fun next(field: Field): Field {
return Field(field.width, field.height) { i, j ->
val n = field.liveNeighbors(i, j)
if (field[i, j])
// (i, j) is alive
n in 2..3 // It remains alive iff it has 2 or 3 neighbors
else
// (i, j) is dead
n == 3 // A new cell is born if there are 3 neighbors alive
}
}
`maven-publish-plugin`
/** A few colony examples here */
fun main(args: Array<String>) {
// Simplistic demo
runGameOfLife("***", 3)
// "Star burst"
runGameOfLife("""
_______
___*___
__***__
___*___
_______
""", 10)
// Stable colony
runGameOfLife("""
_____
__*__
_*_*_
__*__
_____
""", 3)
// Stable from the step 2
runGameOfLife("""
__**__
__**__
__**__
""", 3)
// Oscillating colony
runGameOfLife("""
__**____
__**____
____**__
____**__
""", 6)
// A fancier oscillating colony
runGameOfLife("""
-------------------
-------***---***---
-------------------
-----*----*-*----*-
-----*----*-*----*-
-----*----*-*----*-
-------***---***---
-------------------
-------***---***---
-----*----*-*----*-
-----*----*-*----*-
-----*----*-*----*-
-------------------
-------***---***---
-------------------
""", 10)
}
data class Point(x: Float, y: Float)
class ExampleChild(x: Int): Parent(x) {
fun something(): Thing<MyType> {
val thisThing = OtherThing(x)
return Thing<MyType>()
}
}
// UTILITIES
fun runGameOfLife(fieldText: String, steps: Int) {
var field = makeField(fieldText)
for (step in 1..steps) {
println("Step: $step")
for (i in 0..field.height - 1) {
for (j in 0..field.width - 1) {
print(if (field[i, j]) "*" else " ")
}
println("")
}
field = next(field)
}
}
fun makeField(s: String): Field {
val lines = s.replace(" ", "").split('\n').filter({ it.isNotEmpty() })
val longestLine = lines.toList().maxBy { it.length } ?: ""
return Field(longestLine.length, lines.size) { i, j -> lines[i][j] == '*' }
}
fun `backtickedFunction`() {
return
}
fun <T, V> genericFunction() {
return
}
fun String.extensionFunction() {
return
}
fun `String`.`backTickedExtensionFunction`() {
return
}
fun <T, V : Comparable> String.genericExtensionFunction() {
return
}
fun foo(supersedingStuff: List<String>): List<String> {
return supersedingStuff.map { it }
}
inline fun <reified T> membersOf() = T::class.members
public interface List<out E> : Collection<E> {}
public interface Comparable<in T> {}
@Deprecated("This is an annotation")
fun anAnnotatedFunction() = {
}
val (a, b) = pair
loop@ for (i in 1..100) {
for (j in 1..100) {
if (...) break@loop
}
}
val ints = listOf(1, 4_20, 100L, 123_345_678U)
val bins = listOf(0b1, 0B0, 0b10_10_01, 0b10U, 0b10uL)
val hexes = listOf(0xFF, 0Xa, 0x10L, 0X0UL)
val exponents = listOf(1e10, -1E10F, 1.5e10, 1e-10)
val doublesAndFloats = listOf(3.14, .99, 22f, 1_000.000_1, 19.95F)
// The ? is not an error #1760
fun <T : Any> foo(a: Array<T?>): Int = a.size
// The < is not an error #1760
val <T> T.exhaustive get() = this
// The . in Map.Entry is not an error #1760
fun foo(props: Iterable<Map.Entry<String, JsonNode>>) =
TODO()
// comment at EOF (#797)
|