FAQ
How to delete multiple elements in slice .

i want to delete elements 1,4,5, 6 of my slice. As of now i am
deleting by using append() function in loop , How to delete all
elements in single shot ?.

Regards
Anand.N

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Djadala at Jun 28, 2015 at 8:08 am
    https://github.com/golang/go/wiki/SliceTricks

    Djadala
    On Sunday, June 28, 2015 at 10:47:15 AM UTC+3, Anand Nekkunti wrote:

    How to delete multiple elements in slice .

    i want to delete elements 1,4,5, 6 of my slice. As of now i am
    deleting by using append() function in loop , How to delete all
    elements in single shot ?.

    Regards
    Anand.N
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Egon at Jun 28, 2015 at 10:54 am

    On Sunday, 28 June 2015 10:47:15 UTC+3, Anand Nekkunti wrote:
    How to delete multiple elements in slice .

    i want to delete elements 1,4,5, 6 of my slice. As of now i am
    deleting by using append() function in loop , How to delete all
    elements in single shot ?.
    Assuming your indexes are sorted:
    https://play.golang.org/p/Sekz2Mtzkt

    Of course, it can be optimized further.

    Also if you are more likely to delete index ranges then, you might use some
    of the slice tricks.

    + Egon

    Regards
    Anand.N
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Adam willis at Jun 28, 2015 at 6:23 pm
    golang is imperative programming language, you have to build up the
    functionality needed. The slicetricks link provided by djadala is a nice
    guide to different approaches. Also http://ocw.mit.edu/index.htm has great
    material on data structures.
    On Sunday, June 28, 2015 at 3:47:15 AM UTC-4, Anand Nekkunti wrote:

    How to delete multiple elements in slice .

    i want to delete elements 1,4,5, 6 of my slice. As of now i am
    deleting by using append() function in loop , How to delete all
    elements in single shot ?.

    Regards
    Anand.N
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Daniël de Kok at Jun 28, 2015 at 6:38 pm

    On Sun, Jun 28, 2015, at 20:23, adam willis wrote:
    golang is imperative programming language, you have to build up the
    functionality needed.
    This has little to do with imperative vs. non-imperative programming.
    It's just that you can't implement it in a type-generic manner, so it'd
    have to be a built-in.

    Take care,
    Daniël

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Adam willis at Jun 28, 2015 at 8:22 pm
    it has everything to do with the language being imperative, he is asking
    for declarative functionality from an imperative language. He has to build
    up the functionality himself. this has nothing to do with generics.
    On Sunday, June 28, 2015 at 2:38:32 PM UTC-4, Daniël de Kok wrote:
    On Sun, Jun 28, 2015, at 20:23, adam willis wrote:
    golang is imperative programming language, you have to build up the
    functionality needed.
    This has little to do with imperative vs. non-imperative programming.
    It's just that you can't implement it in a type-generic manner, so it'd
    have to be a built-in.

    Take care,
    Daniël
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Daniël de Kok at Jun 29, 2015 at 7:09 am

    On Sun, Jun 28, 2015 at 01:22:18PM -0700, adam willis wrote:
    it has everything to do with the language being imperative, he is asking
    for declarative functionality from an imperative language. He has to build
    up the functionality himself. this has nothing to do with generics.
    I don't think 'declarative' means what you think it means. First of all, it
    has everything to do with generics, since this is trivial to do in
    a language with generics. E.g. C++ (didn't compile, could contain errors):

    ---
    template <typename T>
    std::vector<T> removeIndices(std::set<int> const &indices,
       std::vector<T> v)
    {
       for (auto iter = indices.rbegin(); iter != indices.rend(); ++iter)
         vec.erase(vec.begin() + *iter);
       return v;
    }
    ---

    Generics make this simple, because:

    - You can easily define generic data types that have the necessary
       constraints (e.g. ordered sets).
    - You define the function once, and it works for vectors of every type.

    As for declarative programming - declarative programming simply means that
    decribes that logic, and not the control flow. E.g. I just wrote up the
    Prolog (the defacto declarative language) solution (which is actually much
    more work):

    ---
    remove(Indices,List,RList) :-
       remove(Indices,List,RList,0).

    remove([IndexHead|IndexTail],[_|Tail],RTail,IndexHead) :-
       Counter is IndexHead + 1,
       remove(IndexTail,Tail,RTail,Counter).
    remove(Indices,[Head|Tail],[Head|RTail],Counter) :-
       Counter1 is Counter + 1,
       remove(Indices,Tail,RTail,Counter1).
    remove([],[],[],_).
    ---

    Rather than having an explicit control structure, it defines the cases where
    the list head is retained/removed. As a result, you can use the same
    predicate in a couple of ways:

    ---
    ?- remove([1,3],[a,b,c,d,e,f],R).
    R = [a, c, e, f] .
    ?- remove(Idx,[a,b,c,d,e,f],[a,c,e,f]).
    Idx = [1, 3] .
    ?- remove([1,3],L,[a,c,e,f]).
    L = [a, _G1177, c, _G1183, e, f] .
    ---

    tl;dr: the imperative type-generic solution is actually much shorter than
    the declarative approach (though, you can probably come up with something
    shorter using nth0).

    Take care,
    Daniël de Kok

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 28, '15 at 7:47a
activeJun 29, '15 at 7:09a
posts7
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase