[HN Gopher] Mnemonic for Shift() and Unshift()?
       ___________________________________________________________________
        
       Mnemonic for Shift() and Unshift()?
        
       JavaScript push() and pop() are intuitive method-names because they
       come from the context of using an array as a "stack". You can
       "push" things to a stack meaning a new item is added as the new
       last element of the array. push() pushes a new element into the
       array. Pop() pops one off it.  shift() and unshift() on the other
       hand I always had trouble with. One of them adds a new element to
       the front of the array and the other removes and returns the 1st
       element. But which is which? I can google the answer but would be
       nice if I didn't have to google. And worse, sometimes I got
       confused between the two and used shift() where unshift() should
       have been used => Error,, stack overflow!  So I googled for
       "Mnemonics for shift() and unshift()", and found an entry on Stack
       Overflow. But that question was "Closed because the answers are
       opinion-based". I looked at the answers available but none of them
       struck me as particularly memorable.  Now after years of struggling
       with this confusion I think I've come up with a mnemonic I can live
       with:  Word "shift" is shorter than the word "unshift". Therefore
       shift() makes the array shorter.  Word "unshift" is longer than the
       word "shift". Therefore unshift() makes the array longer.  I know
       it doesn't really explain it, but it is a rule I can remember. I
       would appreciate hearing how anybody else remembers when to use
       shift() and when to use unshift().
        
       Author : galaxyLogic
       Score  : 5 points
       Date   : 2024-03-28 05:40 UTC (17 hours ago)
        
       | gregjor wrote:
       | Certainly you will remember the difference now, after writing
       | this.
       | 
       | I remember the same way I remember anything else -- by
       | memorizing.
       | 
       | In British English "shift" means "move," so shifting something
       | off an array means moving it (into a variable). Un- as a prefix
       | indicates negation or reversing in English. So you just need to
       | remember what shift does, and unshift does the opposite, as
       | indicated by the word.
        
       | Someone wrote:
       | Learn shell programming (https://tldp.org/LDP/Bash-Beginners-
       | Guide/html/sect_09_07.ht...) or MS-DOS
       | (https://learn.microsoft.com/en-us/windows-
       | server/administrat...), and it won't be a problem remembering
       | what _shift_ does.
       | 
       | Neither of those has _unshift_ , but you can infer its meaning
       | from knowing about _shift_.
       | 
       | I also think _push_ and _pop_ aren't good names. _append_ ,
       | respectively _removelast_ are way better.
        
         | sargstuff wrote:
         | lifo: last in, first out (cafeteria food line) -- taking from
         | front/top, adding to back/bottom -- enought shifts will push
         | element out of array queue without need to 'unshift'
         | 
         | fifo : first in, first out (stack of plates) -- taking from
         | front/top, adding to front/top. -- shift on "1"/push; unshift
         | off "0"/pop. start with at least one shift/push. ideally equal
         | number of unshifts/pops by end of task to clear the stack
         | array.
        
         | brudgers wrote:
         | "append" means adding something to the back end, e.g.
         | appendices in a book are encountered later when sequentially
         | processing the book's pages.
         | 
         | "prepend" is used to describe adding something at the beginning
         | of a sequence.
         | 
         | Good old "cons" has the specific technical meaning of placing
         | data at the lowest address of a sequential memory
         | structure...and it is shorter than "append" and "prepend."
        
           | sargstuff wrote:
           | 'idice' is the specific location context to put at end.
        
       | sargstuff wrote:
       | Mix html with bit twiddle logic!
       | 
       | html < is open block, html > is close block.
       | 
       | < and << are logical shift left for array of bits.
       | 
       | > and >> are logical shift right for array of bits.
       | 
       | Looking at only 'logical value of 1'. Have to open/shift left
       | before unopen/shift right to make thing correct. aka init
       | variable to 0; shift left/push gives 1; shift right/removes aka
       | pops the 1.
       | 
       | html < pushes/shifts all the commands onto stack.
       | 
       | html > pops all the commands todo off of the stack until reach <
       | aka end of stack marker.
        
       ___________________________________________________________________
       (page generated 2024-03-28 23:02 UTC)