title: Simple way to replace an item in an array if it exists, append it if it doesn't
body: <p>Swift 4.2</p>
  
  <p>I have multiple functions that replace an object or struct in an array if it exists, and if it does not exist, it adds it.</p>
  
  <pre><code>func updateFruit(_ fruit: Fruit)
  {
      if let idx = fruitArray.firstIndex(where: { $0.id == fruit.id })
      {
          fruitArray[idx] = fruit
      }
      else
      {
          fruitArray.append(fruit)
      }
  }
  </code></pre>
  
  <p>Obviously I could make this into extension on Array:</p>
  
  <pre><code>extension Array
  {
      mutating func replaceOrAppend(_ item: Element, whereFirstIndex predicate: (Element) -&gt; Bool)
      {
          if let idx = self.firstIndex(where: predicate)
          {
              self[idx] = item
          }
          else
          {
              append(item)
          }
      }
  }
  </code></pre>
  
  <p>However, is there a simpler, easier way of expressing this? Preferably using a closure or build-in function.</p>
  
  <p>NOTE: current implementation does not allow using a set.</p>