edit array

Simple question: why can’t I edit a variable array with a functional method?

For example:

struct TestStruct
  {
  var number = 0
  }

var array = [TestStruct](repeatElement(TestStruct(), count: 2))
array.forEach { $0.number += 1 }

Jan E.

Because you are (trying) to edit a copy.

To edit the value in the array itself use:

array[index].number += 1

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl - A server for websites build in Swift

···

On 26 Apr 2017, at 16:15, J.E. Schotsman via swift-users <swift-users@swift.org> wrote:

Simple question: why can’t I edit a variable array with a functional method?

For example:

struct TestStruct
  {
  var number = 0
  }

var array = [TestStruct](repeatElement(TestStruct(), count: 2))
array.forEach { $0.number += 1 }

Jan E.
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

That requires a for loop.
Functional programming lets you write for loops more succinctly.
Why can’t $0 not be used as a reference, like array[index] ?
I've checked, it’s not among the commonly rejected proposals.

Jan E.

···

On 26 Apr 2017, at 16:27, Rien <Rien@Balancingrock.nl> wrote:

To edit the value in the array itself use:

array[index].number += 1

Agree, though the function should probably be named something like: withEach instead of forEach.
Maybe worth a proposal on evolution?

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Balancingrock (Rien) · GitHub
Project: http://swiftfire.nl - A server for websites build in Swift

···

On 26 Apr 2017, at 16:47, J.E. Schotsman via swift-users <swift-users@swift.org> wrote:

On 26 Apr 2017, at 16:27, Rien <Rien@Balancingrock.nl> wrote:

To edit the value in the array itself use:

array[index].number += 1

That requires a for loop.
Functional programming lets you write for loops more succinctly.
Why can’t $0 not be used as a reference, like array[index] ?
I've checked, it’s not among the commonly rejected proposals.

Jan E.

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Let’s wait until the people at the other side of the big lake have had time to react.

Jan E.

···

On 26 Apr 2017, at 16:54, Rien <Rien@Balancingrock.nl> wrote:

Agree, though the function should probably be named something like: withEach instead of forEach.
Maybe worth a proposal on evolution?

There have been requests for something like this on swift-evolution, e.g. here in the context of the discussion about a `reduce` variant that takes `inout` arguments: [swift-evolution] Reduce with inout

An alternative might be to add a variant of `map` to `MutableCollection` that mutates the collection directly, see: https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170123/030643.html\.

But as far as I know there hasn't been a proposal for adding this. (Is it important enough to have in the standard library? I don't know.)

···

On 26.04.2017 17:01, J.E. Schotsman via swift-users wrote:

On 26 Apr 2017, at 16:54, Rien <Rien@Balancingrock.nl> wrote:

Agree, though the function should probably be named something like: withEach instead of forEach.
Maybe worth a proposal on evolution?

Let’s wait until the people at the other side of the big lake have had time to react.

Relatively straightforward to write this, which usually has meant "no" to adding something to the standard library. (Not that I agree with that measure.)

···

Sent from my iPhone, please excuse brevity and errors

On Apr 26, 2017, at 11:13 AM, Ole Begemann via swift-users <swift-users@swift.org> wrote:

On 26.04.2017 17:01, J.E. Schotsman via swift-users wrote:

On 26 Apr 2017, at 16:54, Rien <Rien@Balancingrock.nl> wrote:

Agree, though the function should probably be named something like: withEach instead of forEach.
Maybe worth a proposal on evolution?

Let’s wait until the people at the other side of the big lake have had time to react.

There have been requests for something like this on swift-evolution, e.g. here in the context of the discussion about a `reduce` variant that takes `inout` arguments: [swift-evolution] Reduce with inout

An alternative might be to add a variant of `map` to `MutableCollection` that mutates the collection directly, see: https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170123/030643.html\.

But as far as I know there hasn't been a proposal for adding this. (Is it important enough to have in the standard library? I don't know.)

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

There have been requests for something like this on swift-evolution, e.g. here in the context of the discussion about a `reduce` variant that takes `inout` arguments: [swift-evolution] Reduce with inout

Jonathan Hull is indeed asking for the same thing.

But as far as I know there hasn't been a proposal for adding this. (Is it important enough to have in the standard library? I don't know.)

I do it all the time!

BTW, it seems that the possibility to edit an array in this way isn’t mentioned explicitly anywhere in The Swift Programming Language.

Formally it requires two copies, like

myInstanceCopy = array[index]
myInstanceCopy.field = newValue
array[index] = myInstanceCopy

Jan E.

···

On 26 Apr 2017, at 18:13, Ole Begemann <ole@oleb.net> wrote: