how to create an array by adding/removing from another array?

Hi,

Using Swift 3, I am in a situation where I need to construct an output array step by step by picking (and possibly modifying) some item from an input array (“consuming” that item, i.e. removing it from the input array).

That very easy to do by mutating “var” variables, using the append() and remove(at:) methods.

However I was wondering if it was possible (preferable?) to do it in a more “functional” style. I was not able to find non-mutating variations of append and remove, which might have looked like:

let myNewArray = existingArray.newArrayByAppending(element: someValue)
let myOtherNewArray = existingArray.newArrayByRemovingElement(At: someIndex)

(method names intentionally chosen to be exceedingly verbose)

Did I miss something?
What would have been the best way to find the answer to such question on my own?

I tried using Dash (with the Swift 3 docSet), and the Xcode 8 beta 6 documentation window (in addition to several attempts using auto-complete).

What is the most idiomatic way to accomplish this simple thing?

Thanks,

Jean-Denis

Apple’s Foundation framework has Array.arrayByAddingObject (but not ByRemoving).

However, this would not be preferable (at least in performance) due to all the copying. Performance of appending n items would be O(n^2), for instance, vs. O(n) for appending in-place.

—Jens

···

On Aug 24, 2016, at 7:07 AM, Jean-Denis Muys via swift-users <swift-users@swift.org> wrote:

However I was wondering if it was possible (preferable?) to do it in a more “functional” style.