Random style question: ```line.mapIndexedNotNull{ ...
# advent-of-code
n
Random style question:
Copy code
line.mapIndexedNotNull{ y, c -> if(c == '#') (x to y).toPoint(d) else null }
Or:
Copy code
line.withIndex().filter { it.value == '#' }.map { (y, _) -> (x to y).toPoint(d) }
e
personally: the first one, unless it's really awkward
Kotlin doesn't (and can't, in general) have Haskell's list fusion optimization, so it does make a difference
k
The second one has wrong y coordinates I think
n
why do you think the second has a wrong y coordinate?
@ephemient yeah, that's true, though if
line
were a sequence instead of an iterable the difference should be pretty minimal no?
if line is an iterable than filter produces a whole new list which isn't great
e
well it says
list
in front so I was not expecting it to be a sequence ;) for sequence, if your type is a primitive, going through the extra stage forces an extra box/unbox, so sometimes the cost is measureable
n
it says "line" I think, but point taken 🙂 . but yeah good point about unboxing.
e
oh I can't read, oops
k
Sorry, missed the initial withIndex instead of mapWithIndex
I have a extension function for this anyway
b
why don't you filter and then mapIndex ?
oh I see you want to keep the index
nevermind