https://kotlinlang.org logo
#getting-started
Title
# getting-started
d

Daniele B

01/02/2021, 8:43 PM
I have a list of numbers:
val numbers = listOf(1,0,4,0,10,14,16,0,0,34,46)
how can I transform it in another list where each number is the maximum so far?
val numbers = listOf(1,1,4,4,10,14,16,16,16,34,46)
r

randomcat

01/02/2021, 8:49 PM
You can use `runningReduce`: https://pl.kotl.in/MfM-GN6U1
d

Daniele B

01/02/2021, 9:00 PM
@randomcat thanks! I think I simplified it too much 🙂 in reality, my list is structured like this: val numbers =
listOf(obj(1),obj(0),obj(4),obj(0),obj(10),obj(14),obj(16),obj(0),obj(0),obj(34),obj(46))
it’s a list of objects, where the number is one of the object properties how is it possible to apply the same pattern in case of the object?
r

randomcat

01/02/2021, 9:00 PM
add a
map { it.value /* or whatever */ }
to get the value out
d

Daniele B

01/02/2021, 9:16 PM
I got stuck. I am not sure how to use map in this context
class MyObj {
val number : Int
}
initial list:
val = listOf(MyObj(1), MyObj(0), MyObj(4), MyObj(0), MyObj(10), MyObj(14), MyObj(16), MyObj(0),obj(0), MyObj(34), MyObj(46))
desired list:
listOf(MyObj(1), MyObj(1), MyObj(4), MyObj(4), MyObj(10), MyObj(14), MyObj(16), MyObj(16), obj(16), MyObj(34), MyObj(46))
r

randomcat

01/02/2021, 9:17 PM
oh, you want to keep the objects, not just the values
one second
d

Daniele B

01/02/2021, 9:17 PM
yes, exactly
r

randomcat

01/02/2021, 9:19 PM
d

Daniele B

01/02/2021, 9:28 PM
@randomcat thanks again! however I can see that it replaces the whole object and not just the value of the property (as I would like). I have added an extra property and you can see it here: https://pl.kotl.in/tuvumPXk9
r

randomcat

01/02/2021, 9:30 PM
so which values of the other properties do you want?
d

Daniele B

01/02/2021, 9:30 PM
I would like to keep the same objects, and only update the single property in the objects
r

randomcat

01/02/2021, 9:31 PM
which object do you want the other properties to come from? the first object?
d

Daniele B

01/02/2021, 9:31 PM
so the “id” properties in the example, should stay the same
as you can see it’s a list of 5 objects, where each object has two properties: value and id I would like the “id” to stay the same, and only change the “value”
r

randomcat

01/02/2021, 9:32 PM
where do you want the id to come from?
d

Daniele B

01/02/2021, 9:32 PM
the “id” should stay the same
r

randomcat

01/02/2021, 9:33 PM
it should stay the same at what value?
d

Daniele B

01/02/2021, 9:33 PM
basically, I would like this:
[I(value=1, id=A), I(value=0, id=B), I(value=4, id=C), I(value=0, id=D), I(value=10, id=E)]
to become this:
[I(value=1, id=A), I(value=1, id=B), I(value=4, id=C), I(value=4, id=D), I(value=10, id=E)]
r

randomcat

01/02/2021, 9:35 PM
d

Daniele B

01/02/2021, 9:36 PM
by running the code I get this:
[I(value=1, id=A), I(value=1, id=A), I(value=4, id=C), I(value=4, id=C), I(value=10, id=E)]
so, the ID gets changed too
r

randomcat

01/02/2021, 9:36 PM
that's not what I get with the link above
d

Daniele B

01/02/2021, 9:36 PM
that’s weird
ok, I did refresh, and it works now, thanks!
Ok, I see, you are using “copy”, so you are actually generating a new object. For efficiency, it would be ideal to just update the value.
but I am not sure if in Kotlin it’s possible
r

randomcat

01/02/2021, 9:40 PM
runningReduce
doesn't play nice with mutation, and it seems like a bad idea to mutate here
d

Daniele B

01/02/2021, 9:40 PM
ok, I see
still, many thanks!