I really want to avoid calling `right.single { rec...
# general-advice
g
I really want to avoid calling
right.single { record.name == target }
, because if
target
comes from a loop thats a full cartesian product, and the data sets of left & right could be a thousand or so elements long but they are ordered, so
right[n]
must have a record in
left
that occurs before
right[n+1]
c
So, this can't happen?
Copy code
val left = listOf(
    CoolRecord("name1", emptyList()),
    CoolRecord("name2", emptyList()),
    CoolRecord("name3", emptyList()),
    CoolRecord("name4", emptyList())
)
val right = listOf(
    CoolRecord("name1", listOf(1.0, 2.0))
    CoolRecord("name4", listOf(5.0, 6.0))
)
Are elements sorted by name in
right
? e.g. if I see
name4
and I haven't yet seen
name3
, can I assume it doesn't exist, or do I have to continue searching?
g
what you posted can happen, a variant that cant happen is
Copy code
val left = listOf(
    CoolRecord("name1", emptyList()),
    CoolRecord("name2", emptyList()),
    CoolRecord("name3", emptyList()),
    CoolRecord("name4", emptyList())
)
val right = listOf(
    CoolRecord("name4" /*!!!*/, listOf(1.0, 2.0))
    CoolRecord("name1", listOf(5.0, 6.0))
)
the idea is that they are ordered the same, but the
right
is sparse
s
Copy code
fun reallySlickJoinFunction(
  left: List<CoolRecord>,
  right: List<CoolRecord>
): List<CoolRecord> {
  var r = 0
  return left.map {
    if (it.name == right.getOrNull(r)?.name) right[r++] else it
  }
}