Skip to content

Instantly share code, notes, and snippets.

@afollestad
Created December 14, 2018 18:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save afollestad/4303908a32ac2884e6efc6ddd87b83ce to your computer and use it in GitHub Desktop.
Save afollestad/4303908a32ac2884e6efc6ddd87b83ce to your computer and use it in GitHub Desktop.
Map and switchMap live data with extension functions instead of method calls.
import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
fun <X, Y> LiveData<X>.map(mapper: (X) -> Y) =
Transformations.map(this, mapper)!!
fun <X, Y> LiveData<X>.switchMap(mapper: (X) -> LiveData<Y>) =
Transformations.switchMap(this, mapper)!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment