Skip to content
This repository has been archived by the owner on Aug 5, 2021. It is now read-only.

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
svtk committed Feb 18, 2019
1 parent 59faf6c commit 36d3c33
Show file tree
Hide file tree
Showing 19 changed files with 65 additions and 60 deletions.
6 changes: 3 additions & 3 deletions src/i_introduction/_5_String_Templates/n05StringTemplates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import util.TODO
import util.doc5

fun example1(a: Any, b: Any) =
"This is some text in which variables ($a, $b) appear."
"This is some text in which variables ($a, $b) appear."

fun example2(a: Any, b: Any) =
"You can write it in a Java way as well. Like this: " + a + ", " + b + "!"
"You can write it in a Java way as well. Like this: " + a + ", " + b + "!"

fun example3(c: Boolean, x: Int, y: Int) = "Any expression can be used: ${if (c) x else y}"

fun example4() =
"""
"""
You can use raw strings to write multiline text.
There is no escaping here, so raw strings are useful for writing regex patterns,
you don't need to escape a backslash by a backslash.
Expand Down
6 changes: 3 additions & 3 deletions src/i_introduction/_7_Nullable_Types/n07NullableTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ fun todoTask7(client: Client?, message: String?, mailer: Mailer): Nothing = TODO
)

fun sendMessageToClient(
client: Client?, message: String?, mailer: Mailer
client: Client?, message: String?, mailer: Mailer
) {
todoTask7(client, message, mailer)
}

class Client (val personalInfo: PersonalInfo?)
class PersonalInfo (val email: String?)
class Client(val personalInfo: PersonalInfo?)
class PersonalInfo(val email: String?)

interface Mailer {
fun sendMessage(email: String, message: String)
Expand Down
9 changes: 5 additions & 4 deletions src/i_introduction/_8_Smart_Casts/n08SmartCasts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import util.doc8
// 'sealed' modifier restricts the type hierarchy:
// all the subclasses must be declared in the same file
sealed class Expr

class Num(val value: Int) : Expr()
class Sum(val left: Expr, val right: Expr) : Expr()

fun eval(e: Expr): Int =
when (e) {
is Num -> todoTask8(e)
is Sum -> todoTask8(e)
}
when (e) {
is Num -> todoTask8(e)
is Sum -> todoTask8(e)
}

fun todoTask8(expr: Expr): Nothing = TODO(
"""
Expand Down
18 changes: 10 additions & 8 deletions src/ii_collections/n16FlatMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ fun example() {
result == listOf('a', 'b', 'c', '1', '2')
}

val Customer.orderedProducts: Set<Product> get() {
// Return all products this customer has ordered
todoCollectionTask()
}
val Customer.orderedProducts: Set<Product>
get() {
// Return all products this customer has ordered
todoCollectionTask()
}

val Shop.allOrderedProducts: Set<Product> get() {
// Return all products that were ordered by at least one customer
todoCollectionTask()
}
val Shop.allOrderedProducts: Set<Product>
get() {
// Return all products that were ordered by at least one customer
todoCollectionTask()
}
5 changes: 2 additions & 3 deletions src/ii_collections/n22Fold.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ fun example9() {
// The same as
fun whatFoldDoes(): Int {
var result = 1
listOf(1, 2, 3, 4).forEach { element -> result = element * result}
listOf(1, 2, 3, 4).forEach { element -> result = element * result }
return result
}

fun Shop.getSetOfProductsOrderedByEachCustomer(): Set<Product> {
// Return the set of products that were ordered by each of the customers
return customers.fold(allOrderedProducts, {
orderedByAll, customer ->
return customers.fold(allOrderedProducts, { orderedByAll, customer ->
todoCollectionTask()
})
}
2 changes: 1 addition & 1 deletion src/ii_collections/n24ExtensionsOnCollections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fun todoTask24(): Nothing = TODO(
The function should do the same as '_24_JavaCode.doSomethingStrangeWithCollection'.
Replace all invocations of 'todoTask24()' with the appropriate code.
""",
references = { c: Collection<String> -> _24_JavaCode().doSomethingStrangeWithCollection(c) }
references = { c: Collection<String> -> _24_JavaCode().doSomethingStrangeWithCollection(c) }
)

fun doSomethingStrangeWithCollection(collection: Collection<String>): Collection<String>? {
Expand Down
4 changes: 2 additions & 2 deletions src/ii_collections/todoUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package ii_collections
import util.TODO

fun todoCollectionTask(): Nothing = TODO(
"""
"""
Common task for working with collections.
Look through the Shop API, which all the tasks are using.
Each individual task is described in the function name and the comment.
""",
references = { shop: Shop -> shop.customers }
references = { shop: Shop -> shop.customers }
)
2 changes: 1 addition & 1 deletion src/iii_conventions/MyDateUtil.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package iii_conventions

import iii_conventions.TimeInterval.*
import iii_conventions.TimeInterval.DAY
import java.util.*

fun MyDate.nextDay() = addTimeIntervals(DAY, 1)
Expand Down
12 changes: 8 additions & 4 deletions src/iii_conventions/n28ForLoop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ import util.TODO
import util.doc28

fun iterateOverCollection(collection: Collection<Int>) {
for (element in collection) {}
for (element in collection) {
}
}

fun iterateOverString() {
// You can iterate over anything that has an 'iterator' method, member or extension.
for (c in "abcd") {}
for (c in "abcd") {
}
"abcd".iterator() //library extension method
}

fun iterateOverRange() {
for (i in 1..10) {}
for (c in 'a'..'z') {}
for (i in 1..10) {
}
for (c in 'a'..'z') {
}
}

fun todoTask28(): Nothing = TODO(
Expand Down
2 changes: 1 addition & 1 deletion src/iii_conventions/n29OperatorsOverloading.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package iii_conventions

import util.TODO
import iii_conventions.TimeInterval.*
import util.TODO

fun todoTask29(): Nothing = TODO(
"""
Expand Down
2 changes: 1 addition & 1 deletion src/iv_properties/n35HowDelegatesWork.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fun todoTask35(): Nothing = TODO(
Store only the time in milliseconds in 'timeInMillis' property.
Use the extension functions 'MyDate.toMillis' and 'Long.toDate'.
""",
references = { date: MyDate -> date.toMillis().toDate()}
references = { date: MyDate -> date.toMillis().toDate() }
)

class D {
Expand Down
6 changes: 3 additions & 3 deletions src/util/kotlinUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package util

@Suppress("UNUSED_PARAMETER")
fun TODO(
task: String,
documentation: Unit = Unit,
references: Function<Any?> = {}
task: String,
documentation: Unit = Unit,
references: Function<Any?> = {}
): Nothing = throw NotImplementedException(task)

class NotImplementedException(message: String) : Exception(message)
2 changes: 1 addition & 1 deletion src/v_builders/data.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ val crocodile = Product("crocodile", 20000.2, 1)
val cushion = Product("cushion", 131.0, 0)

fun getProducts() = listOf(cactus, cake, camera, car, carrot, cellPhone, chimney, certificate, cigar, coffee, coffeeMaker,
cola, cranberry, crocs, crocodile, cushion)
cola, cranberry, crocs, crocodile, cushion)
28 changes: 14 additions & 14 deletions src/v_builders/html.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,40 @@ open class Tag(val name: String) {
}
}

class Attribute(val name : String, val value : String) {
class Attribute(val name: String, val value: String) {
override fun toString() = """$name="$value""""
}

fun <T: Tag> T.set(name: String, value: String?): T {
fun <T : Tag> T.set(name: String, value: String?): T {
if (value != null) {
attributes.add(Attribute(name, value))
}
return this
}

fun <T: Tag> Tag.doInit(tag: T, init: T.() -> Unit): T {
fun <T : Tag> Tag.doInit(tag: T, init: T.() -> Unit): T {
tag.init()
children.add(tag)
return tag
}

class Html: Tag("html")
class Table: Tag("table")
class Center: Tag("center")
class TR: Tag("tr")
class TD: Tag("td")
class Text(val text: String): Tag("b") {
class Html : Tag("html")
class Table : Tag("table")
class Center : Tag("center")
class TR : Tag("tr")
class TD : Tag("td")
class Text(val text: String) : Tag("b") {
override fun toString() = text
}

fun html(init: Html.() -> Unit): Html = Html().apply(init)

fun Html.table(init : Table.() -> Unit) = doInit(Table(), init)
fun Html.center(init : Center.() -> Unit) = doInit(Center(), init)
fun Html.table(init: Table.() -> Unit) = doInit(Table(), init)
fun Html.center(init: Center.() -> Unit) = doInit(Center(), init)

fun Table.tr(color: String? = null, init : TR.() -> Unit) = doInit(TR(), init).set("bgcolor", color)
fun Table.tr(color: String? = null, init: TR.() -> Unit) = doInit(TR(), init).set("bgcolor", color)

fun TR.td(color: String? = null, align : String = "left", init : TD.() -> Unit) = doInit(TD(), init).set("align", align).set("bgcolor", color)
fun TR.td(color: String? = null, align: String = "left", init: TD.() -> Unit) = doInit(TD(), init).set("align", align).set("bgcolor", color)

fun Tag.text(s : Any?) = doInit(Text(s.toString()), {})
fun Tag.text(s: Any?) = doInit(Text(s.toString()), {})

2 changes: 1 addition & 1 deletion src/v_builders/htmlDemo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import javax.swing.SwingConstants.CENTER


fun main(args: Array<String>) {
with (JFrame("Product popularity")) {
with(JFrame("Product popularity")) {
setSize(600, 600)
defaultCloseOperation = JFrame.EXIT_ON_CLOSE
add(JScrollPane(JLabel(renderProductTable(), CENTER)))
Expand Down
1 change: 0 additions & 1 deletion src/v_builders/n37StringAndMapBuilders.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package v_builders

import util.TODO
import java.util.*

fun buildStringExample(): String {
fun buildString(build: StringBuilder.() -> Unit): String {
Expand Down
2 changes: 1 addition & 1 deletion src/v_builders/n39HtmlBuilders.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import v_builders.data.getProducts
import v_builders.htmlLibrary.*

fun getTitleColor() = "#b9c9fe"
fun getCellColor(row: Int, column: Int) = if ((row + column) %2 == 0) "#dce4ff" else "#eff2ff"
fun getCellColor(row: Int, column: Int) = if ((row + column) % 2 == 0) "#dce4ff" else "#eff2ff"

fun todoTask39(): Nothing = TODO(
"""
Expand Down
8 changes: 4 additions & 4 deletions src/v_builders/n40BuildersHowItWorks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fun task40() = linkedMapOf<Int, Answer>(
b. function declaration
c. function invocation
*/
1 to insertAnswerHere(),
1 to insertAnswerHere(),

/*
2. In the Kotlin code
Expand All @@ -49,7 +49,7 @@ fun task40() = linkedMapOf<Int, Answer>(
b. argument name
c. argument value
*/
2 to insertAnswerHere(),
2 to insertAnswerHere(),

/*
3. The block
Expand All @@ -62,7 +62,7 @@ from the previous question is:
c. something mysterious
*/
3 to insertAnswerHere(),
3 to insertAnswerHere(),

/*
4. For the code
Expand All @@ -84,5 +84,5 @@ which of the following is true:
}
}
*/
4 to insertAnswerHere()
4 to insertAnswerHere()
)
8 changes: 4 additions & 4 deletions src/vi_generics/n41GenericFunctions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ fun task41(): Nothing = TODO(
You should write a function that splits the collection into two collections given as arguments.
The signature of the 'toCollection()' function from the standard library may help you.
""",
references = { l: List<Int> ->
l.partition { it > 0 }
l.toCollection(HashSet<Int>())
}
references = { l: List<Int> ->
l.partition { it > 0 }
l.toCollection(HashSet<Int>())
}
)

fun List<String>.partitionWordsAndLines(): Pair<List<String>, List<String>> {
Expand Down

0 comments on commit 36d3c33

Please sign in to comment.