How to mutualize many list extractions in a same foreach

Certified Senior Developer

Hi,

I have a very simply code and need to make 2 things:

- create a first list that feet a condition

- create another list (exclusion) using the same condition

I would like to see how would you do using a single foreach please ?

a!localVariables(
  local!lst,
  local!goodVehicles: a!forEach(
    items: ri!vehicles,
    expression: if(and(
        fv!item.typeid = 1,
        fv!item.isavailable,
        not(fv!item.deleted)
      ),
      fv!item,
      {},
    )
  ),
  
  local!badVehicles: a!forEach(
    items: ri!vehicles,
    expression: if(and(
        fv!item.typeid = 1,
        fv!item.isavailable,
        not(fv!item.deleted)
      ),
      {},
      append(local!lst, fv!item),
    )
  ),
  {
    good: local!goodVehicles,
    bad: local!badVehicles
  }
)

  Discussion posts and replies are publicly visible

Parents Reply
  • 0
    Certified Senior Developer
    in reply to Mike Schmitt

    Mike, my example was incorrect, and yes, the "local!lst" was an error.

    I should have wrote this.

      /* correction */
      local!goodVehicles: a!forEach(
        items: ri!vehicles,
        expression: if(and(
            fv!item.typeid = 1,
            fv!item.isavailable,
            not(fv!item.deleted)
          ),
          fv!item,
          {},
        )
      ),
      
      local!badVehicles: a!forEach(
        items: ri!vehicles,
        expression: if(and(
            fv!item.typeid = 1,
            fv!item.isavailable,
            not(fv!item.deleted)
          ),
          {},
          fv!item,
        )
      ),
      {
        good: local!goodVehicles,
        bad: local!badVehicles
      }

Children
  • 0
    Certified Lead Developer
    in reply to cedric01

    Gotcha; yes this is the approach i'd suggest.  Trying to condense this down to one a!forEach loop (and nothing else added) would require enormously more complexity (if even possible). 

    The one possible alternative I can think of, requiring only one a!forEach() loop, would be to have it create an array of "tags" (i.e. iterate through the vehicles and create a NEW array of strings, each being i.e. "Good", "Bad", "Neutral"), then afterwards, set the local!goodVehicles and local!badVehicles arrays based on indexing into the original array of vehicles based on the indices in the "tag" array i.e. "get every vehicle at a 'good' position", etc.  The fallback here being, I'm not sure this is any more efficient, and is certainly at least as complex, as just having your two a!forEach loops.

  • 0
    Certified Senior Developer
    in reply to Mike Schmitt

    yes, you're probably right Mike.

    Yes, I see what you mean, I tried these solution too, for another case.

    Thank you for your help.