the-algorithm/product-mixer/component-library/src/main/scala/com/twitter/product_mixer/component_library/selector/InsertSelector.scala
twitter-team ef4c5eb65e Twitter Recommendation Algorithm
Please note we have force-pushed a new initial commit in order to remove some publicly-available Twitter user information. Note that this process may be required in the future.
2023-03-31 17:36:31 -05:00

40 lines
1.4 KiB
Scala

package com.twitter.product_mixer.component_library.selector
import com.twitter.product_mixer.core.functional_component.common.CandidateScope
import CandidateScope.PartitionedCandidates
import com.twitter.product_mixer.core.functional_component.selector.SelectorResult
import com.twitter.product_mixer.core.model.common.presentation.CandidateWithDetails
private[selector] object InsertSelector {
/**
* Insert all candidates from a candidate pipeline at a 0-indexed fixed position. If the current
* results are a shorter length than the requested position, then the candidates will be appended
* to the results.
*/
def insertIntoResultsAtPosition(
position: Int,
pipelineScope: CandidateScope,
remainingCandidates: Seq[CandidateWithDetails],
result: Seq[CandidateWithDetails]
): SelectorResult = {
assert(position >= 0, "Position must be equal to or greater than zero")
val PartitionedCandidates(selectedCandidates, otherCandidates) =
pipelineScope.partition(remainingCandidates)
val resultUpdated = if (selectedCandidates.nonEmpty) {
if (position < result.length) {
val (left, right) = result.splitAt(position)
left ++ selectedCandidates ++ right
} else {
result ++ selectedCandidates
}
} else {
result
}
SelectorResult(remainingCandidates = otherCandidates, result = resultUpdated)
}
}