Scala updated equivalent

Hello everyone!

What do you all think about adding the equivalent of Scala’s update method to collections that have the insert() func? This would likely take the form of:

public func inserted(newElement: Self.Generator.Element, atIndex i: Self.Index) -> Self {
    var modified = self
    modified.insert(newElement, atIndex: i)
    return modified
}

I’ve had to do this manually quite a lot in my various programs, where I’ve had to return an array or dictionary with just one element modified, and I have a feeling that I am not alone. I think that this is quite a natural way to go about things, and the addition would cut down on boilerplate code.

— Sasha

P.S. Alternatives could be “.withInserted(el, atIndex: i)” or “.with(el, insertedAtIndex: i)"

Moving to swift-evolution.

···

On Tue, Dec 22, 2015 at 2:42 AM, Alexandre Lopoukhine via swift-dev <swift-dev@swift.org> wrote:

Hello everyone!

What do you all think about adding the equivalent of Scala’s update method to collections that have the insert() func? This would likely take the form of:

public func inserted(newElement: Self.Generator.Element, atIndex i: Self.Index) -> Self {
    var modified = self
    modified.insert(newElement, atIndex: i)
    return modified
}

I’ve had to do this manually quite a lot in my various programs, where I’ve had to return an array or dictionary with just one element modified, and I have a feeling that I am not alone. I think that this is quite a natural way to go about things, and the addition would cut down on boilerplate code.

— Sasha

P.S. Alternatives could be “.withInserted(el, atIndex: i)” or “.with(el, insertedAtIndex: i)"
_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/

I like this addition. Per the the API guidelines, the method signature should probably be

inserting(newElement: Element, atIndex i: Index)

Would/should this be an addition to protocol RangeReplaceableCollectionType (easy) or to CollectionType?

It would also be a useful addition to String.

Guillaume Lessard

+1. This is very functional. Need to drill home the point that arrays have
copy semantics in the intro swift docs though, if we start going in this
direction.

···

On Tue, Dec 22, 2015, 7:12 AM Dmitri Gribenko via swift-evolution < swift-evolution@swift.org> wrote:

Moving to swift-evolution.

On Tue, Dec 22, 2015 at 2:42 AM, Alexandre Lopoukhine via swift-dev > <swift-dev@swift.org> wrote:
> Hello everyone!
>
> What do you all think about adding the equivalent of Scala’s update
method to collections that have the insert() func? This would likely take
the form of:
>
> public func inserted(newElement: Self.Generator.Element, atIndex i:
Self.Index) -> Self {
> var modified = self
> modified.insert(newElement, atIndex: i)
> return modified
> }
>
> I’ve had to do this manually quite a lot in my various programs, where
I’ve had to return an array or dictionary with just one element modified,
and I have a feeling that I am not alone. I think that this is quite a
natural way to go about things, and the addition would cut down on
boilerplate code.
>
> — Sasha
>
> P.S. Alternatives could be “.withInserted(el, atIndex: i)” or “.with(el,
insertedAtIndex: i)"
> _______________________________________________
> swift-dev mailing list
> swift-dev@swift.org
> https://lists.swift.org/mailman/listinfo/swift-dev

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution