Use more Sequences to reduce collection processing
https://kotlinlang.org/docs/sequences.html
This commit is contained in:
parent
b4d10c9613
commit
acf8071d9e
|
@ -31,8 +31,10 @@ class EmojiAdapter(
|
|||
private val animate: Boolean
|
||||
) : RecyclerView.Adapter<BindingHolder<ItemEmojiButtonBinding>>() {
|
||||
|
||||
private val emojiList: List<Emoji> = emojiList.filter { emoji -> emoji.visibleInPicker == null || emoji.visibleInPicker }
|
||||
private val emojiList: List<Emoji> = emojiList.asSequence()
|
||||
.filter { emoji -> emoji.visibleInPicker == null || emoji.visibleInPicker }
|
||||
.sortedBy { it.shortcode.lowercase(Locale.ROOT) }
|
||||
.toList()
|
||||
|
||||
override fun getItemCount() = emojiList.size
|
||||
|
||||
|
|
|
@ -63,8 +63,10 @@ class PollAdapter : RecyclerView.Adapter<BindingHolder<ItemPollBinding>>() {
|
|||
}
|
||||
|
||||
fun getSelected(): List<Int> {
|
||||
return pollOptions.filter { it.selected }
|
||||
return pollOptions.asSequence()
|
||||
.filter { it.selected }
|
||||
.map { pollOptions.indexOf(it) }
|
||||
.toList()
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingHolder<ItemPollBinding> {
|
||||
|
|
|
@ -59,7 +59,8 @@ class AnnouncementsViewModel @Inject constructor(
|
|||
.fold(
|
||||
{
|
||||
announcementsMutable.postValue(Success(it))
|
||||
it.filter { announcement -> !announcement.read }
|
||||
it.asSequence()
|
||||
.filter { announcement -> !announcement.read }
|
||||
.forEach { announcement ->
|
||||
mastodonApi.dismissAnnouncement(announcement.id)
|
||||
.fold(
|
||||
|
|
|
@ -389,13 +389,14 @@ class ComposeViewModel @Inject constructor(
|
|||
val emojiList = emoji.replayCache.firstOrNull() ?: return emptyList()
|
||||
val incomplete = token.substring(1)
|
||||
|
||||
return emojiList.filter { emoji ->
|
||||
emoji.shortcode.contains(incomplete, ignoreCase = true)
|
||||
}.sortedBy { emoji ->
|
||||
emoji.shortcode.indexOf(incomplete, ignoreCase = true)
|
||||
}.map { emoji ->
|
||||
AutocompleteResult.EmojiResult(emoji)
|
||||
}
|
||||
return emojiList.asSequence()
|
||||
.filter { emoji ->
|
||||
emoji.shortcode.contains(incomplete, ignoreCase = true)
|
||||
}.sortedBy { emoji ->
|
||||
emoji.shortcode.indexOf(incomplete, ignoreCase = true)
|
||||
}.map { emoji ->
|
||||
AutocompleteResult.EmojiResult(emoji)
|
||||
}.toList()
|
||||
}
|
||||
else -> {
|
||||
Log.w(TAG, "Unexpected autocompletion token: $token")
|
||||
|
|
|
@ -175,7 +175,8 @@ class NetworkTimelineViewModel @Inject constructor(
|
|||
val overlappedTo = statusData.indexOfFirst { it.asStatusOrNull()?.id?.isLessThan(lastId) ?: false }
|
||||
|
||||
if (overlappedFrom < overlappedTo) {
|
||||
data.mapIndexed { i, status -> i to statusData.firstOrNull { it.asStatusOrNull()?.id == status.id }?.asStatusOrNull() }
|
||||
data.asSequence()
|
||||
.mapIndexed { i, status -> i to statusData.firstOrNull { it.asStatusOrNull()?.id == status.id }?.asStatusOrNull() }
|
||||
.filter { (_, oldStatus) -> oldStatus != null }
|
||||
.forEach { (i, oldStatus) ->
|
||||
data[i] = data[i].asStatusOrNull()!!
|
||||
|
|
|
@ -84,9 +84,11 @@ class TrendingViewModel @Inject constructor(
|
|||
}
|
||||
|
||||
val tags = response.body()!!
|
||||
.asSequence()
|
||||
.filter { homeFilters?.none { filter -> filter.phrase.equals(it.name, ignoreCase = true) } ?: false }
|
||||
.sortedBy { tag -> tag.history.sumOf { it.uses.toLongOrNull() ?: 0 } }
|
||||
.map { it.toViewData() }
|
||||
.toList()
|
||||
.asReversed()
|
||||
|
||||
_uiState.value = TrendingUiState(tags, LoadingState.LOADED)
|
||||
|
|
|
@ -59,7 +59,7 @@ class FilterModel @Inject constructor() {
|
|||
val now = Date()
|
||||
val nonExpiredFilters = filters.filter { it.expiresAt?.before(now) != true }
|
||||
if (nonExpiredFilters.isEmpty()) return null
|
||||
val tokens = nonExpiredFilters
|
||||
val tokens = nonExpiredFilters.asSequence()
|
||||
.map { filterToRegexToken(it) }
|
||||
|
||||
return Pattern.compile(TextUtils.join("|", tokens), Pattern.CASE_INSENSITIVE)
|
||||
|
|
|
@ -64,7 +64,8 @@ fun getInitialLanguages(language: String? = null, activeAccount: AccountEntity?
|
|||
val system = AppCompatDelegate.getApplicationLocales().toList() +
|
||||
LocaleListCompat.getDefault().toList()
|
||||
|
||||
return (selected + system.map { it.language }).distinct().filter { it.isNotEmpty() }
|
||||
return (selected + system.map { it.language })
|
||||
.asSequence().distinct().filter { it.isNotEmpty() }.toList()
|
||||
}
|
||||
|
||||
fun getLocaleList(initialLanguages: List<String>): List<Locale> {
|
||||
|
|
|
@ -136,8 +136,11 @@ class SpanUtilsTest {
|
|||
}
|
||||
|
||||
override fun <T : Any> getSpans(start: Int, end: Int, type: Class<T>): Array<T> {
|
||||
return spans.filter { it.start >= start && it.end <= end && type.isInstance(it) }
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return spans.asSequence()
|
||||
.filter { it.start >= start && it.end <= end && type.isInstance(it) }
|
||||
.map { it.span }
|
||||
.toList()
|
||||
.toTypedArray() as Array<T>
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue