r/javascript • u/freebit • Mar 24 '18
Can't keep Array.shift vs Array.unshift straight? (NSFW) NSFW
Here is a helpful, although gross mnemonic to help keep the two straight in your head. Drop the "f".
So, Array.shit() drops a load (element) from the beginning of an array and Array.unshit() will do the opposite.
38
u/senocular Mar 24 '18
I used the length to help remember.
push is longer than pop because it addspop is shorter than push because it removesunshift is longer than shift because it addsshift is shorter than unshift because it removes
Not as fun as dropping the "f" tho :(
3
2
12
u/mauvm Mar 24 '18
Why is this NSFW?
11
0
Mar 24 '18
[deleted]
26
u/beb1312 Mar 24 '18
In my workplace its not uncommon to hear "fuck your mother" being directed toward a computer.
4
u/NotSelfAware Mar 25 '18
I once punched my monitor at work out of frustration, and it broke. It was old so they didn't care much. Pretty soon after we all went out for lunch.
12
Mar 24 '18 edited Mar 24 '18
the word "shit" is generally not acceptable in the workplace here in the United States.
If you think that's really the case, then maybe you work with a bunch of puritans.
This post may be 'nsfw' entirely because there are minors interested in javascript.
No adult I know in any place I've worked would be offended by the word "shit".
2
u/mauvm Mar 24 '18
Any idea why?
3
u/oneeyedziggy Mar 25 '18
depending on the workplace a lot of us yanks are prudes, fancy ourselves "good Christians" and otherwise delight in upholding a bunch of made-up bs rules... makes the weak feel more powerful
2
u/oneeyedziggy Mar 25 '18
your workplace sucks then... you do what you gotta do to feed your family, but think of looking for somewhere less stuckup... as long as you're not swearing AT people, rules like that just show your workplace has its priorities all out of whack... what other usless or even obstructionist bs rules are they holding over you... you wear a uniform to your non-customer-interaction deskjob too? I just wish more people demanded better of their employers
3
u/prof3ssorSt3v3 Web|Mobile Design|Dev Mar 24 '18
My students will appreciate this. :)
But it is drop the "f".
3
8
u/sh0rtwave Mar 24 '18 edited Mar 25 '18
Some day, we'll get it right and realize we just need clear semantics for what these damned things actually do.
Teaching array.pop/push/slice/shift/unshift is one of the toughest things to do, IMHO.
Everbody gets pop and push, instantly. They make sense. We all know about popping something off the top of a stack of dishes and pushing something else back on top of it.
So does "slice". (Who doesn't understand slicing a pie?). We're just taking SOME of the dishes out of the stack, and maybe sticking new ones in. Makes sense. Lift the top part of the stack you're leaving, stick something new in, take something out, set them back down. Easy-peasy.
In fact, almost all the other functions are simple to understand, because they relate to physical things we can understand.
So that stack of plates...how does unshift work? So really, what you're doing is lifting the entire stack, and then...un-shifting(carefully scooting) the bottom plate with your foot into place so you can put that big stack back down on top of it.
Shift? Lift that whole stack of plates with both hands, and kick that bottom plate out with vigor.
I find, when explaining things to people about computing, it's best if you can grab an idea of the physical act you're doing, it aids with understanding in a whole other way than just being a memory tool.
Edit: I should mention...it took me a year or so of teaching(adults) to come up with that. Some of my students (mostly adult) had serious problems grasping even things like variables. Seriously, I had to use cups + water + food coloring to simulate the idea that a variable can NOT hold two values at a time. (That came in useful in many situations, e.g. AoCP Q#1.)
Edit again: Don't even get me started about trying to explain bitmasks.
Edit again again: Explained it backwards like an idiot.
3
Mar 24 '18
Unshift? Lift that whole stack of plates with both hands, and kick that bottom plate out with vigor.
But unshift adds, so you’d be carefully sliding a plate under.
3
1
u/Gemmellness Mar 25 '18
Shift moves the front pointer along one. Unshift moves it back one.
1
u/sh0rtwave Mar 25 '18
The technical explanation aside... This is how I explained it to absolute neophytes.
1
u/filleduchaos Mar 25 '18
...or maybe don't try to explain an Array(List) as though it's a Stack?
The methods are named just fine.
1
u/sh0rtwave Mar 25 '18
I never said they were named badly. Just that the explanation was easier to understand in physical terms.
3
2
73
u/Skhmt Mar 24 '18
This will confuse me, since you don't shit from the head, you shit from the back. But Array.shit() comes from the head.