<commit_before>/*
 * Copyright (c) 2018. <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package splitties.arch.lifecycle

import android.arch.lifecycle.LifecycleOwner
import android.arch.lifecycle.LiveData
import android.arch.lifecycle.Observer
import android.arch.lifecycle.Transformations

inline fun <T> LifecycleOwner.observe(
        liveData: LiveData<T>,
        crossinline observer: (t: T?) -> Unit
): Observer<T> = Observer<T> {
    observer(it)
}.also { liveData.observe(this, it) }

inline fun <T : Any> LifecycleOwner.observeNotNull(
        liveData: LiveData<T>,
        crossinline observer: (t: T) -> Unit
): Observer<T> = Observer<T> {
    if (it != null) observer(it)
}.also { liveData.observe(this, it) }

@JvmName("observeWithLiveDataOfNullable")
inline fun <T : Any> LifecycleOwner.observeNotNull(
        liveData: LiveData<T?>,
        crossinline observer: (t: T) -> Unit
): Observer<T?> = Observer<T?> {
    if (it != null) observer(it)
}.also { liveData.observe(this, it) }

/**
 * Applies the given function on the main thread to each value emitted by source
 * LiveData and returns LiveData, which emits resulting values.
 *
 * The given function [transform] will be **executed on the main thread**.
 *
 * @param transform   a function to apply
 * @param X           a type of source LiveData
 * @param Y           a type of resulting LiveData.
 * @return            a LiveData which emits resulting values
 */
inline fun <X, Y> LiveData<X>.map(
        crossinline transform: (X?) -> Y
): LiveData<Y> = Transformations.map(this, { input -> transform(input) })
<commit_msg>Add switchMap + notNull variant, and mapNotNull<commit_after>/*
 * Copyright (c) 2018. <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package splitties.arch.lifecycle

import android.arch.lifecycle.LifecycleOwner
import android.arch.lifecycle.LiveData
import android.arch.lifecycle.Observer
import android.arch.lifecycle.Transformations

inline fun <T> LifecycleOwner.observe(
        liveData: LiveData<T>,
        crossinline observer: (t: T?) -> Unit
): Observer<T> = Observer<T> {
    observer(it)
}.also { liveData.observe(this, it) }

inline fun <T : Any> LifecycleOwner.observeNotNull(
        liveData: LiveData<T>,
        crossinline observer: (t: T) -> Unit
): Observer<T> = Observer<T> {
    if (it != null) observer(it)
}.also { liveData.observe(this, it) }

@JvmName("observeWithLiveDataOfNullable")
inline fun <T : Any> LifecycleOwner.observeNotNull(
        liveData: LiveData<T?>,
        crossinline observer: (t: T) -> Unit
): Observer<T?> = Observer<T?> {
    if (it != null) observer(it)
}.also { liveData.observe(this, it) }

/**
 * Applies the given function on the main thread to each value emitted by source
 * LiveData and returns LiveData, which emits resulting values.
 *
 * The given function [transform] will be **executed on the main thread**.
 *
 * @param transform   a function to apply
 * @param X           a type of source LiveData
 * @param Y           a type of resulting LiveData.
 * @return            a LiveData which emits resulting values
 */
inline fun <X, Y> LiveData<X>.map(
        crossinline transform: (X?) -> Y
): LiveData<Y> = Transformations.map(this, { input -> transform(input) })

inline fun <X, Y> LiveData<X>.mapNotNull(
        crossinline transform: (X) -> Y
): LiveData<Y> = Transformations.map(this, { input: X? ->
    input?.let { transform(it) }
})

inline fun <X, Y> LiveData<X>.switchMap(
        crossinline transform: (X?) -> LiveData<Y>?
): LiveData<Y> = Transformations.switchMap(this, { input -> transform(input) })

inline fun <X, Y> LiveData<X>.switchMapNotNull(
        crossinline transform: (X) -> LiveData<Y>?
): LiveData<Y> = Transformations.switchMap(this, { input: X? ->
    input?.let { transform(it) }
})

{'fraction_non_alphanumeric': 0.11808576755748912, 'fraction_numerical': 0.0033146882121400456, 'mean_word_length': 3.637848222862632, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5711603', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1414, 'n_words': 500}