https://kotlinlang.org logo
#android
Title
# android
i

ivano

02/28/2023, 12:37 PM
disclaimer: I do not know what is the channel for this problem, so I post here because I am doing in Android I have
Copy code
data class SomeIntegers(val a: Int, val b: Int, val c: Int, val NoCalculateMe: Int)

val list = listOf<SomeIntegers>( //A lot of SomeIntegers )
I want to find the one max value present in the a, b, c into the list I thought to 2 approaches, do
a

Alexander Pozdniakov

02/28/2023, 12:52 PM
So basically what you need is to find max value among
a
,
b
,
c
for every
SomeIntegers
object in a list and then find max value among those max values?
i

ivano

02/28/2023, 12:53 PM
this is correct Alexander, and it looks overwhelming my second solution
is that bad?
a

Alexander Pozdniakov

02/28/2023, 1:05 PM
I would do that with something like the following
Copy code
fun SomeIntegers.getMax(): Int = maxOf(a, maxOf(b, c))

val maxValue = list.fold(Int.MIN_VALUE) { currMax, integers ->
    maxOf(currMax, integers.getMax())
}
i

ivano

02/28/2023, 1:10 PM
wow thank you very much this looks much more functional than my solution, I am going to read the documentation for what you suggest, Alex do you have some tips on what I can do to improve on this filtering game, I am quite weak
and in the future would love to come up with a solution like yours
s

sciack

02/28/2023, 1:11 PM
if you have a list of Int (or an int array I don't remember) you can use the max function (iirc)
i

ivano

02/28/2023, 1:13 PM
OK thanks
r

Robert Williams

02/28/2023, 1:13 PM
There's also overloads of maxOf which take more than 2 values so you don't need multiple maxOfs
i

ivano

02/28/2023, 1:14 PM
folks do you have a resource I can improve in solving these kind of problems please? Will do leetcode, advent code, codewars, help me or I need to do other to become proficient?
the steps i pseudocoded also are working great:
Copy code
val max  = integerList.map { listOf(it.a,it.b, it.c,it.d) }
                        .flatten()
                        .max()
it looks pretty functional
and readable
what do you think? (I am not senior)
5 Views