Use more Sequences to reduce collection processing

https://kotlinlang.org/docs/sequences.html
This commit is contained in:
Goooler 2023-02-28 21:35:38 +08:00
parent b4d10c9613
commit acf8071d9e
9 changed files with 27 additions and 14 deletions

View File

@ -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

View File

@ -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> {

View File

@ -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(

View File

@ -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")

View File

@ -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()!!

View File

@ -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)

View File

@ -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)

View File

@ -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> {

View File

@ -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>
}