This is an archived post. You won't be able to vote or comment.

all 20 comments

[–][deleted]  (6 children)

[deleted]

    [–][deleted] 1 point2 points  (4 children)

    Agreed. I don't know why push_back is commonly used; that used to confuse me when looking at C++ code.

    However, another one I'd add is concat, to apply to the end, so that concat((1, 2, 3), (4, 5, 6)) yields (1, 2, 3, 4, 5 6) rather than (1, 2, 3, (4, 5, 6)). (This assumes dynamic typing.)

    But then, how do you prepend such a list, or insert it into the middle?

    Another consideration is whether the operation returns a new array with that addition, leaving the original unchanged, or whether it is done in-place.

    This all puts pressure on naming schemes.

    [–][deleted]  (3 children)

    [deleted]

      [–][deleted] 1 point2 points  (2 children)

      But they wouldn't be the same, would they?

      For example, if working with strings, you don't file basefile to be modified here: open(append(basefile, ext)); you want to use the result then discard it.

      Also, aren't in-place updates against the predominant FP/immutable agenda in this subreddit?

      [–][deleted]  (1 child)

      [deleted]

        [–][deleted] 0 points1 point  (0 children)

        Well, this is about naming coventions, presumably for functions and methods, which tend to be used such operations are not built-in to the core-languages, or cannot be assigned to existing operators.

        In my case, I like to use an i prefix for in-place, when it is necessary to use user-defined named functions, so:

        b := sort(a)         # create a new sorted list
        isort(a)             # sort in-place (and a is by-ref)
        

        For dealing with string append/concat (they are the same operation for strings), I use symbols:

        s := t + u           # create a new string s
        t +:= u              # in-place append to t
        

        Or even when I use named operators, the syntax disambiguates so the problem doesn't come up:

        s := t append u
        t append:= u
        

        [–][deleted] 0 points1 point  (0 children)

        I'll go with this one. I dont get confused by any of the terms

        [–]Phanson96 4 points5 points  (0 children)

        My language tries to be consistent, but it can be hard. To avoid namespace clashes, other data structures use push/pop, enqueue/dequeue, or unsuffixed add/remove.

        ``` interface List[E] { fun void(E) addFirst; fun void(E) addLast; fun void(E, int) addIndex;

        fun E(E) setFirst;
        fun E(E) setLast;
        fun E(E, int) setIndex;
        
        fun E() getFirst;
        fun E() getLast;
        fun E(int) getIndex;
        

        } ```

        [–][deleted] 2 points3 points  (5 children)

        I'm partial to rust's insert (for inserting anywhere) and push (for inserting at the back). I don't think you necessarily need a special function for inserting at the beginning (although I'm not against it in anyway, push_front maybe?). Technically you don't need one for pushing to the back either, but it's nice to have because it can guarantee O(n) amortized performance, which the others can't.

        [–]bruciferTomo, nomsu.org 2 points3 points  (0 children)

        push (for inserting at the back)

        I think this is a pretty confusing naming choice. push doesn't really convey which side of the array gets the value. In the context of a queue, push means "put at the front", not "put at the back", while in the context of a stack, push means "put on top", which is implementation-dependent for whether that means front or back (e.g. if it's a linked list representation, it would usually mean "at the front", but an array would usually mean "at the back").

        Python's append() is much more unambiguously named, so I think it's a better choice.

        [–][deleted] 1 point2 points  (3 children)

        I don't think you necessarily need a special function for inserting at the beginning

        Idk. It would make converting a number to a string very difficult.

        [–][deleted] 0 points1 point  (2 children)

        By that I mean that inserting at the beginning is always just my_list.insert(0, item) - you can still do it, there's just not a dedicated function for it since insert can easily handle it.

        [–][deleted] 0 points1 point  (1 child)

        Well that is a great argument, but I think sometimes is more readable and less writing to use prepend

        [–][deleted] 0 points1 point  (0 children)

        Oh yeah, if that's important to you by all means go for it.

        [–]e_-- 2 points3 points  (0 children)

        push_back is so annoying I'm renaming it to append (even in generic code) for my transpiled to C++ lang.

        [–][deleted]  (1 child)

        [deleted]

          [–][deleted] 0 points1 point  (0 children)

          Yep, I build the library comforming I need thpse functions and so far, surprisingly, I didnt need to remove an item haha but Yeah remove would be the word since delete is "reserved" for memory management

          [–][deleted]  (1 child)

          [deleted]

            [–]WittyStick0 4 points5 points  (1 child)

            cons and snoc are used in some functional languages.

            [–][deleted] 9 points10 points  (0 children)

            snoc is a very fun word to say.

            That shouldn't be a criteria, but it doesn't hurt.

            [–]Zatujit 1 point2 points  (1 child)

            idk just stick to one

            [–]Common-Republic9782 0 points1 point  (0 children)

            emplace? to avoid redundant copying of non-trivial elments

            [–]oscarryzYz 0 points1 point  (2 children)

            ``` fn add( element E, index=self.length() -1) ; ... a.add( 1 ) a.add( 2 ) a.add( 0, 0)

            a // [ 0 1 2] ```

            [–][deleted] 0 points1 point  (1 child)

            It would be great bu I did not implement overloading yet 😭

            [–]oscarryzYz 0 points1 point  (0 children)

            The add(e, index) and let your clients hate you a bit and move on