[swift-users] how to create an array by adding/removing from another array?

Jean-Denis Muys jdmuys at gmail.com
Wed Aug 24 09:07:04 CDT 2016


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



More information about the swift-users mailing list