The original code is already quite concise and idiomatic Kotlin, using mapIndexedNotNull to achieve the desired result in a single pipeline step. However, if the goal is to explicitly separate the "transformation to relevant data" and "filtering" steps for potentially better readability, especially for more complex conditions, using filter after an initial mapIndexed can be a good alternative.
Here's the refactored version, breaking it down into distinct steps:
kotlin
// Assuming 'reorderedQuestions' is List<Question> and 'initialPositions' is Map<QuestionId, Int>
// Let's define a simple Question and QuestionId for context
data class QuestionId(val value: String)
data class Question(val id: QuestionId, val text: String)
// Example data (replace with your actual data)
val reorderedQuestions = listOf(
Question(QuestionId("q1"), "What is Kotlin?"),
Question(QuestionId("q2"), "What is Java?"),
Question(QuestionId("q3"), "What is Python?"),
Question(QuestionId("q4"), "What is C++?")
)
val initialPositions = mapOf(
QuestionId("q1") to 1, // Stays at 1
QuestionId("q2") to 3, // Moved from 3 to 2
QuestionId("q3") to 2, // Moved from 2 to 3
QuestionId("q4") to 4 // Stays at 4
)
val movedQuestionsMap = reorderedQuestions
.mapIndexed { index, q ->
// Step 1: Transform each question into an intermediate object
// containing its ID, its calculated new position, and its initial position.
val newPosition = index + 1
Triple(q.id, newPosition, initialPositions[q.id])
}
.filter { (questionId, newPosition, initialPosition) ->
// Step 2: Filter out only the questions that have moved.
// The condition is explicit and easy to read.
newPosition != initialPosition
}
.associate { (questionId, newPosition, _) ->
// Step 3: Map the filtered results to the desired (id to newPosition) pairs
// and collect them into a Map. `associate` is often preferred over `toMap`
// when creating a map directly from a collection by providing key-value pairs.
questionId to newPosition
}
// You can test it:
// println(movedQuestionsMap) // Expected output: {QuestionId(value=q2)=2, QuestionId(value=q3)=3}
Instead of immediately deciding to null out elements, this step creates an intermediate List of Triples. Each Triple contains all the necessary information (questionId, newPosition, initialPosition) to make the filtering decision in the next step.
This separates the calculation/transformation of data from the filtering logic.
This is now a dedicated filtering step. It clearly states the condition (newPosition != initialPosition) for an item to be included in the subsequent pipeline steps.
Using destructuring declarations (questionId, newPosition, initialPosition) makes the filter predicate very readable, as you directly refer to the components of the Triple.
associate { (questionId, newPosition, _) -> questionId to newPosition }:
This terminal operation is used to directly create a Map from the filtered Triples.
associate is often more expressive than toMap() when you're explicitly creating key-value pairs from the collection elements.
We use _ to discard the initialPosition as it's no longer needed for the final map.
Why this might be considered more readable:
Separation of Concerns: The code now clearly delineates three distinct phases:
Preparation/Transformation:mapIndexed to gather all relevant data for each item.
Filtering:filter to select only the items that meet a specific condition.
Collection:associate to build the final map from the selected items.
Explicit Filtering Condition: The filter lambda's condition newPosition != initialPosition is directly visible and doesn't rely on the if/else null pattern, which some developers find less direct for filtering purposes.
Intermediate Data Clarity: Although it creates an intermediate List<Triple>, the Triple (or a small data class if more complex) explicitly carries the values needed for both filtering and final mapping, which can be helpful for understanding the data flow.