LXSoft
LXSoft

Reputation: 596

Select object from a list o objects by smallest attribute, only if object have neighbours

As I written in my title, how to get object from a list of objects by smallest attribute only if that object have neighbours on the left and right side. If object does not have neighbours select another minimum object. My list contains minimum 3 elements.

My input list is:

val objects = List(Car("BMW", "Serie 3", 1800), Car("Mercedes", "Benz A", 1400), Car("Audi", "A3", 1200), ...)

Car with minimum engine of my list is index 2 Car("Audi", "A3", 1200), which does not have left and right neighbours. I need choose somehow minimum again to get index 1, which will give me: Car("Mercedes", "Benz A", 1400) which has left and right neighbours (index 0 and index2.

I tried this without success, because method can give me any index including first and last object:

val cars = List(Car("BMW", "Serie 3", 1800), Car("Mercedes", "Benz A", 1400), Car("Audi", "A3", 1200), ...)
val engines = cars.map(_.engine)
val minEngine = engines.min
// (???) TODO: condition against first and last index to repick min
// which has left and right neighbours
val index = cars.indexWhere(_.engine == minEngine)
println("Previous engine: " cars(index-1) + "Min engine: " + cars(index) + "Next engine: " + cars(index+1))

Upvotes: 1

Views: 66

Answers (2)

Peter Neyens
Peter Neyens

Reputation: 9820

You could get the minimum of the list without the first and last element, the remaining cars will have a neighbour.

cars.drop(1).dropRight(1).minBy(_.engine)
// or
cars.tail.init.minBy(_.engine)

Upvotes: 1

Noah
Noah

Reputation: 13959

You can use sliding(3) with reduce to get what you want:

val minCar = cars.sliding(3).reduce((l, r) => if (r(1).engine > l(1).engine) l else r)(1)

This only works for lists of length 3 or more.

Upvotes: 1

Related Questions