mirror of
https://github.com/twitter/the-algorithm.git
synced 2024-06-02 01:08:47 +02:00
ef4c5eb65e
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.
40 lines
1.4 KiB
Scala
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)
|
|
}
|
|
}
|