Last active
July 26, 2020 10:41
-
-
Save DenisBronx/534582fe4d9c5b87eef3652faa745314 to your computer and use it in GitHub Desktop.
ListMappers in FP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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