## 1
```kotlin=
fun getItems(): Observable<List<Item>> =
Observable.create { emitter ->
emitter.onNext(getItemsBlocking())
emitter.onComplete()
}
@Throws(IOException::class)
fun getItemsBlocking(): List<Item> = ...
```
## 2
Допустим у нас есть следующий код:
```kotlin=
class Course(val id: Long)
class CourseContent(val sections: List<String>)
interface CourseRepository {
fun getCourse(courseId: Long): Maybe<Course>
}
interface CourseContentRepository {
fun getCourseContent(courseId: Long): Observable<CourseContent>
}
private val coursePublishSubject: BehaviorSubject<Course> =
BehaviorSubject.create()
fun getCourse(courseId: Long): Maybe<Course> =
courseRepository
.getCourse(courseId)
.doOnSuccess(coursePublishSubject::onNext)
fun getCourseContent(): Observable<CourseContent> =
coursePublishSubject
.flatMap { courseContentRepository.getCourseContent(it.id) }
// somewhere
val compositeDisposable = CompositeDisposable()
compositeDisposable += getCourseContent()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribeBy(
onNext = { Log.d("TAG", it.toString()) }
)
compositeDisposable += getCourse(111)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribeBy(
onNext = { Log.d("TAG", it.toString()) }
)
```
Что, в каком порядке и на каком потоке будет выполнено?
## 3
Допустим у нас есть следующий код:
```kotlin=
class Item
class ItemResponse(
val items: List<Item>
)
/**
* Получение ItemResonse из сети
*/
fun getItemResponse(): Single<ItemResponse> = ...
fun getItems(): Single<List<Item>> =
getItemResponse()
.map(itemResponseMapper)
```
и мы бы хотели, чтобы при вызове метода `map` каждый раз не создавался анонимный класс. Какая реализация это обеспечивает?
###### A
```kotlin=
private val itemResponseMapper =
io.reactivex.functions.Function<ItemResponse, List<Item>>(ItemResponse::items)
```
###### B
```kotlin=
private val itemResponseMapper =
ItemResponse::items
```
###### C
```kotlin=
private val itemResponseMapper = { response: ItemResponse -> response.items }
```
## 4
Есть метод преобразующий `Bundle` в `Map<String, String>`
```kotlin=
fun bundleToMap(bundle: Bundle): Map<String, String> =
HashMap<String, String>()
.also { map ->
bundle.keySet().forEach { key ->
map[key] = bundle[key].toString()
}
}
```
Метод `get` объявлен в `Bundle` как
```java=
class Bundle {
/**
* Returns the entry with the given key as an object.
*
* @param key a String key
* @return an Object, or null
*/
@ android.annotation.Nullable
public Object get(String key);
}
```
Какой из двух методов будет вызван в строке 5: `bundle[key].toString()`
Extension для `Any?`
```kotlin=
/**
* Returns a string representation of the object. Can be called with a null receiver, in which case
* it returns the string "null".
*/
public fun Any?.toString(): String
```
Или метод `toString()` у `Any`
```kotlin=
public open class Any {
/**
* Returns a string representation of the object.
*/
public open fun toString(): String
}
```
## 5
Допустим мы хотим написать расширение `totalSize` для `LongArray`, которое считает сумму длин подмассивов. Предложены две реализации:
```kotlin=
fun Iterable<LongArray>.totalSize(): Int =
sumBy { array: LongArray -> array.size }
```
```kotlin=
fun Iterable<LongArray>.totalSize(): Int =
sumBy(LongArray::size)
```
Есть ли различия между реализациями?