warriorprincess
06/01/2018, 11:00 AMminBy
actually give a reference to original object? I do lowest = lowest?.next
in my code above, does it actually move the head
of the list stored in lists
?diesieben07
06/01/2018, 11:02 AMwarriorprincess
06/01/2018, 11:08 AMlowest
here it will change in the original array too?diesieben07
06/01/2018, 11:11 AMval x = MyObject() // x holds reference to that object
val y = x // y is set to the same value as x, i.e. the reference to the object
warriorprincess
06/01/2018, 11:14 AMval x = 9; var y = x; y = 8
diesieben07
06/01/2018, 11:15 AMEverything is pass-by-value
warriorprincess
06/01/2018, 11:15 AMclass ListNode(var `val`: Int = 0) {
var next: ListNode? = null
}
diesieben07
06/01/2018, 11:16 AMwarriorprincess
06/01/2018, 11:17 AMvar head: ListNode?
head = head?.next
head
is simply being overwritten?diesieben07
06/01/2018, 11:18 AMhead
, yes. I am not sure why you think this would not work?warriorprincess
06/01/2018, 12:20 PMval
?: Int.MAX_VALUE }
`var lowest = lists.minBy { it?.`val` ?: Int.MAX_VALUE }
lowest = lowest?.next
lists
isn't affected by lowest = lowest?.next
diesieben07
06/01/2018, 12:22 PMwarriorprincess
06/01/2018, 12:22 PMdiesieben07
06/01/2018, 12:28 PMwarriorprincess
06/01/2018, 12:35 PMdiesieben07
06/01/2018, 12:44 PMval (lowest, lowestIndex) = lists.withIndex().minBy { (value, index) -> value.whatever }
lists[lowestIndex] = lists[lowestIndex].next
class LinkedList(var head: Node?)
val listWithLowest = lists.minBy { it.head?.val ?: Int.MAX_VALUE }
if (listWithLowest != null) listWithLowest.head = listWithLowest.head?.next
warriorprincess
06/01/2018, 12:54 PMval (lowest, lowestIndex) = lists.withIndex().minBy { (value, index) -> value.whatever }
withIndex
say 'Returns a lazy Iterable of IndexedValue for each element of the original array.'diesieben07
06/01/2018, 12:56 PMlist.withIndex()
turns a List<T>
into a Iterable<IndexedValue<T>>
. The (value, index)
is a destructuring operation, grabbing the actual value and it's index in the list out of the IndexedValue
.warriorprincess
06/01/2018, 12:57 PMdiesieben07
06/01/2018, 12:57 PMwarriorprincess
06/01/2018, 12:57 PMdiesieben07
06/01/2018, 12:58 PMminBy
is defined for Iterable
.warriorprincess
06/01/2018, 12:58 PMval (lowest, lowestIndex) = lists.withIndex().minBy { (value, index) -> value.`val` ?: Int.MAX_VALUE }
diesieben07
06/01/2018, 12:59 PMminBy
is nullable. Yeswarriorprincess
06/01/2018, 1:00 PMval
is not defined for value
diesieben07
06/01/2018, 1:02 PM(index, value)
, sorry.minBy { it.value.val ?: Int.MAX_VALUE }
, if you don't like the destructuring.warriorprincess
06/01/2018, 1:44 PM