Skip to content

Instantly share code, notes, and snippets.

@DenisBronx
Last active July 26, 2020 10:41
Show Gist options
  • Save DenisBronx/534582fe4d9c5b87eef3652faa745314 to your computer and use it in GitHub Desktop.
Save DenisBronx/534582fe4d9c5b87eef3652faa745314 to your computer and use it in GitHub Desktop.
ListMappers in FP
// Non-nullable to Non-nullable
inline fun <I, O> mapList(input: List<I>, mapSingle: (I) -> O): List<O> {
return input.map { mapSingle(it) }
}
// Nullable to Non-nullable
inline fun <I, O> mapNullInputList(input: List<I>?, mapSingle: (I) -> O): List<O> {
return input?.map { mapSingle(it) } ?: emptyList()
}
// Non-nullable to Nullable
inline fun <I, O> mapNullOutputList(input: List<I>, mapSingle: (I) -> O): List<O>? {
return if (input.isEmpty()) null else input.map { mapSingle(it) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment