Skip to content
This repository has been archived by the owner on Sep 17, 2019. It is now read-only.

Commit

Permalink
Reordering: made 'collections' the second part
Browse files Browse the repository at this point in the history
  • Loading branch information
svtk committed Oct 23, 2015
1 parent 283d1e4 commit 371c46a
Show file tree
Hide file tree
Showing 98 changed files with 429 additions and 434 deletions.
2 changes: 1 addition & 1 deletion .idea/runConfigurations/Html_Demo.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Test v_builders/_40_Builders_How_It_Works.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package v_builders

import org.junit.Test
import v_builders.builders.todoTask40
import util.questions.Answer.*
import junit.framework.Assert
import v_builders.builders.task40

class _40_Builders_How_It_Works {
@Test fun testBuildersQuiz() {
val correctAnswers = mapOf(22 - 20 to b, 1 + 3 to c, 11 - 8 to b, 79 - 78 to c)
Assert.assertEquals("Your answers are incorrect: ", correctAnswers, task40())
}
}
4 changes: 2 additions & 2 deletions src/i_introduction/_11_SAM_Conversions/SAMConversions.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package i_introduction._11_SAM_Conversions

import util.TODO
import util.doc11_1
import util.doc11
import java.util.*

fun todoTask11() = TODO(
Expand All @@ -12,7 +12,7 @@ fun todoTask11() = TODO(
Rewrite the previous example changing an object expression to a lambda.
""",
documentation = doc11_1()
documentation = doc11()
)

fun task11(): List<Int> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fun todoTask12() = TODO(
Thus in Kotlin we don't introduce our own collections, but use standard Java ones (slightly improved).
Read about read-only and mutable views on Java collections.
""",
documentation = doc12_1()
documentation = doc12()
)

fun task12(): List<Int> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package i_introduction._3_Default_Arguments

import util.*

fun todoTask3_() = TODO(
fun todoTask3() = TODO(
"""
Task 3.
Several overloads of 'JavaCode2.foo()' can be replaced with one function in Kotlin.
Expand All @@ -13,10 +13,10 @@ fun todoTask3_() = TODO(
documentation = doc2(),
references = { name: String -> JavaCode2().foo(name); foo(name) })

fun foo(name: String): String = todoTask3_()
fun foo(name: String): String = todoTask3()

fun task3_(): String {
todoTask3_()
fun task3(): String {
todoTask3()
// return (foo("a") +
// foo("b", number = 1) +
// foo("c", toUpperCase = true) +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v_collections
package ii_collections

import java.util.HashSet
import util.TODO
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v_collections
package ii_collections

fun example1(list: List<Int>) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v_collections
package ii_collections

fun example2(list: List<Int>) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v_collections
package ii_collections

fun example() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v_collections
package ii_collections

fun example4() {
val max = listOf(1, 42, 4).max()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v_collections
package ii_collections

fun example5() {
val result = listOf("a", "bbb", "cc").sortedBy { it.length }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v_collections
package ii_collections

fun example6() {
val sum = listOf(1, 5, 3).sum()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v_collections
package ii_collections

fun example7() {
val result = listOf("a", "b", "ba", "ccc", "ad").groupBy { it.length }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v_collections
package ii_collections

fun example8() {
val numbers = listOf(1, 3, -4, 2, -11)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v_collections
package ii_collections

import java.util.HashSet

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v_collections
package ii_collections

fun Shop.getCustomersWhoOrderedProduct(product: Product): Set<Customer> {
// Return the set of customers who ordered the specified product
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package v_collections
package ii_collections

import util.TODO

fun todoTask9() = TODO(
"""
Task 9.
The function should behave the same as 'LJavaCode9.doSomethingStrangeWithCollection'
The function should behave the same as '_24_JavaCode.doSomethingStrangeWithCollection'
Replace all invocations of 'todoTask9()' with the appropriate code.
""",
references = { c: Collection<String> -> LJavaCode9().doSomethingStrangeWithCollection(c) }
references = { c: Collection<String> -> _24_JavaCode().doSomethingStrangeWithCollection(c) }
)

fun doSomethingStrangeWithCollection(collection: Collection<String>): Collection<String>? {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v_collections;
package ii_collections;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
Expand All @@ -8,7 +8,7 @@
import java.util.List;
import java.util.Map;

public class LJavaCode9 extends JavaCode {
public class _24_JavaCode extends JavaCode {
public Collection<String> doSomethingStrangeWithCollection(Collection<String> collection) {
Map<Integer, List<String>> groupsByLength = Maps.newHashMap();
for (String s : collection) {
Expand Down
2 changes: 1 addition & 1 deletion src/v_collections/shop.kt → src/ii_collections/shop.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v_collections
package ii_collections

data class Shop(val name: String, val customers: List<Customer>)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v_collections
package ii_collections

import util.TODO

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ii_conventions
package iii_conventions

data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package ii_conventions
package iii_conventions

import java.util.Calendar
import ii_conventions.TimeInterval.YEAR
import ii_conventions.TimeInterval.DAY
import ii_conventions.TimeInterval.WEEK
import iii_conventions.TimeInterval.YEAR
import iii_conventions.TimeInterval.DAY
import iii_conventions.TimeInterval.WEEK

fun MyDate.nextDay() = addTimeIntervals(DAY, 1)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package ii_conventions
package iii_conventions

import util.*

fun todoTask11(): Nothing = TODO(
fun todoTask25() = TODO(
"""
Task 11.
Task 25.
Uncomment the commented line and make it compile.
Add all changes to the file MyDate.kt.
Make class MyDate implement Comparable.
""",
documentation = doc11(),
documentation = doc25(),
references = { date: MyDate, comparable: Comparable<MyDate> -> }
)

fun task11(date1: MyDate, date2: MyDate): Boolean {
todoTask11()
fun task25(date1: MyDate, date2: MyDate): Boolean {
todoTask25()
// return date1 < date2
}

Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package ii_conventions
package iii_conventions

import util.*

fun todoTask12(): Nothing = TODO(
fun todoTask26() = TODO(
"""
Task 12.
Task 26.
Uncomment the commented code and make it compile.
Add all changes to the file MyDate.kt.
Make class DateRange implement Iterable<MyDate>.
Use object expression to implement Iterator<MyDate>.
Use the utility function 'MyDate.nextDay()'.
""",
documentation = doc12(),
documentation = doc26(),
references = { date: MyDate -> DateRange(date, date.nextDay()) })


fun iterateOverDateRange(firstDate: MyDate, secondDate: MyDate, handler: (MyDate) -> Unit) {
todoTask12()
todoTask26()
// for (date in DateRange(firstDate, secondDate)) {
// handler(date)
// }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package ii_conventions
package iii_conventions

import util.*

fun todoTask13(): Nothing = TODO(
fun todoTask27() = TODO(
"""
Task 13.
Task 27.
Uncomment the commented code and make it compile.
To make '..' work add a 'MyDate.rangeTo()' extension function returning DateRange.
Add all changes to the file MyDate.kt.
Expand All @@ -13,7 +13,7 @@ fun todoTask13(): Nothing = TODO(


fun iterateOverDateRange2(firstDate: MyDate, secondDate: MyDate, handler: (MyDate) -> Unit) {
todoTask13()
todoTask27()
// for (date in firstDate..secondDate) {
// handler(date)
// }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,21 @@
package ii_conventions
package iii_conventions

import util.*

interface Container<E> {
operator fun contains(element: E): Boolean
}

fun inConvention(container: Container<String>) {
"a" in container
// compiles to
container.contains("a")
}

fun numericalRange(i: Int, c: Char) {
//'..' resolves to 'rangeTo' function
if (i in 1..10) {}

if (c in 'a'..'z') {}
}

fun stringRange(s: String) {
if (s in "island".."isle") {}
}

fun todoTask14(): Nothing = TODO(
fun todoTask28() = TODO(
"""
Task 14.
Task 28.
Uncomment the commented line.
Right now interval membership is checked with the function 'fun <T> Iterable<T>.contains(T): Boolean'
which is not optimal (you can check the boundaries instead of iterating over the whole range).
Make class DateRange implement kotlin.Range<MyDate>.
Add all changes to the file MyDate.kt.
""",
documentation = doc14(),
documentation = doc28(),
references = { range: Range<Int> -> }
)

fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean {
todoTask14()
todoTask28()
// return date in first..last
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package ii_conventions
package iii_conventions

import util.*
import ii_conventions.TimeInterval.*
import iii_conventions.TimeInterval.*

fun todoTask15() = TODO(
fun todoTask29() = TODO(
"""
Task 15.
Task 29.
Implement a kind of date arithmetic. Support adding years, weeks and days to a date.
Use classes MyDate and TimeInterval.
Use a utility function MyDate.addTimeIntervals.
Uncomment the commented line and make it compile.
(1). Add an extension function 'plus()' to MyDate, taking a TimeInterval as an argument.
(2). Support adding several time intervals to a date. Add an extra class.
If you have any problems, see the ii_conventions/_15_Tips.kt file.
If you have any problems, see the iii_conventions/_29_Tips.kt file.
""",
references = { date: MyDate, timeInterval: TimeInterval ->
date.addTimeIntervals(timeInterval, 1)
})

fun task15_1(today: MyDate): MyDate {
todoTask15()
fun task29_1(today: MyDate): MyDate {
todoTask29()
// return today + YEAR + WEEK
}

fun task15_2(today: MyDate): MyDate {
todoTask15()
fun task29_2(today: MyDate): MyDate {
todoTask29()
// return today + YEAR * 2 + WEEK * 3 + DAY * 5
}

Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package ii_conventions
package iii_conventions

import util.TODO

fun todoTask15_2(): Nothing = TODO(
fun todoTask29_2() = TODO(
"""
Task15.2.
Task29.2.
Support adding several time intervals to a date.
Add an extra class for storing the time interval and the number of intervals,
e.g. 'class RepeatedTimeInterval(val ti: TimeInterval, val n: Int)'.
Expand Down
Loading

0 comments on commit 371c46a

Please sign in to comment.