gmoraleda
gmoraleda

Reputation: 1953

Swift filter with condition

I'm creating a filter to filter an Array of vehicles. I'm filtering according to price, performance and, optionally, to gearing type.

func filterCarSpecifications(among carSpecifications: [CarSpecification]) -> [CarSpecification] {

    let filteredCars = carSpecifications.filter {

        let performance = $0.car.ps ?? 0.0

        return $0.pricing.price >= IoC.filterDataService.selectedMinPrice.rounded(.down)
            && $0.pricing.price <= IoC.filterDataService.selectedMaxPrice.rounded(.up)
            && performance >= IoC.filterDataService.selectedMinPerformance
            && performance <= IoC.filterDataService.selectedMaxPerformance
    }

    if IoC.filterDataService.autoTransmision {
        return filteredCars.filter {
            let transmission: GearingType = .automatic
            return $0.car.gearingType == transmission
        }
    }

    return filteredCars
}

IoC.filterDataService.autoTransmision is a Bool to check if the user wants to see only automatic vehicles. Is there a way to include this extra value into my original filter?

Upvotes: 2

Views: 2228

Answers (3)

OOPer
OOPer

Reputation: 47896

You can write something like this:

func filterCarSpecifications(among carSpecifications: [CarSpecification]) -> [CarSpecification] {

    let filteredCars = carSpecifications.filter {

        let performance = $0.car.ps ?? 0.0

        return $0.pricing.price >= IoC.filterDataService.selectedMinPrice.rounded(.down)
            && $0.pricing.price <= IoC.filterDataService.selectedMaxPrice.rounded(.up)
            && performance >= IoC.filterDataService.selectedMinPerformance
            && performance <= IoC.filterDataService.selectedMaxPerformance
            && (!IoC.filterDataService.autoTransmision || $0.car.gearingType == .automatic)
    }

    return filteredCars
}

When IoC.filterDataService.autoTransmision == false, the left hand side of || operator becomes true, so the right hand side is ignored.

When it is true, !IoC.filterDataService.autoTransmision becomes false. You know false || someBoolValue is equivalent to someBoolValue.

So, $0.car.gearingType == .automatic is evaluated only if IoC.filterDataService.autoTransmision == true.

Upvotes: 2

Shehata Gamal
Shehata Gamal

Reputation: 100533

Append it like this

func filterCarSpecifications(among carSpecifications: [CarSpecification]) -> [CarSpecification] {

let filteredCars = carSpecifications.filter {

    let performance = $0.car.ps ?? 0.0

    return $0.pricing.price >= IoC.filterDataService.selectedMinPrice.rounded(.down)
        && $0.pricing.price <= IoC.filterDataService.selectedMaxPrice.rounded(.up)
        && performance >= IoC.filterDataService.selectedMinPerformance
        && performance <= IoC.filterDataService.selectedMaxPerformance 
        && (IoC.filterDataService.autoTransmision) ? $0.car.gearingType == transmission : true
  }
}

Upvotes: 1

Lal Krishna
Lal Krishna

Reputation: 16180

Append

&& IoC.filterDataService.autoTransmision ? $0.car.gearingType == .automatic : true

to your initial conditions

Upvotes: 3

Related Questions