November 29, 2017

I remember when I was very happy with like 10 good pictures out of a roll of film

Adding elements to an array

November 29, 2017

I spend a lot of time on this site trying to convince people that immutable data structures are the way to go. They might be a little bit more difficult from time to time, and a little bit slower; but I believe it is worth it because when you use immutable techniques instead of mutable you know that you need to actually tell your code “replace this with this” for something to change. And I believe it is a good way to avoid a lot of annoying bugs.

The standard way to add a element to a array in Javascript is using the push method like below:

Or you could do it with concat, which is the way I prefer to do it:

The result is the same, but with the latter you can do what ever you want with the list, without worrying about fucking up the original, before you explicitly tell it to be replaced.


November 28, 2017

Splice is another mehtod on the Array Prototype; it is in many ways Slice’s weird brother in law. The main difference is that splice mutates the array you run it on, while slice does not. I personally never use splice because mutation often lead to unintended circomstances.

The other thing about splice is that you can’t do all of what you can do with slice; for example the cool -1 tricks for removing stuff at the end. This is where it get’s a little bit “weird”. You can use it to remove stuff from an array. It takes a start argument and a number argument, for example like below (removing the two first elements)

But you can also feed it any number of elements after the number argument which will be inserted where you removed the elements. Splice can be very useful, but be careful, because everything is just weird enough that it is very easy to do something you didn’t want to and cause some difficult to spot bugs.

Let me end with the key differences between slice and splice:

  • Splice mutates

  • Slice doesn’t mutate

  • Slice works with start and end position

  • Splice works with a start position and a “deleteCount”.

  • Splice can also append elements.


November 27, 2017

The emoji for American power outlets: 😮


November 27, 2017

Slice is a method on the Array Prototype that you can use to extract a section of a array. Let’s say you want to remove the first two elements from a array. Then you could do something like this:

You could of course do the same with a combination of filters and map without using slice:

There aren’t much to the slice method, but the key thing is that it doesn’t mutate the array you run it on, but rather it returns a new copy. It takes two arguments: begin and end. If you don’t give it a end it treats the end as to the end of the array. And you can give it a negative number of remove things at the end. For example, like below to remove the last three elements at the end; without doing some weird .length hacks or double reversing.

Slice is a very versatile and useful method, when you need to modify a list based on position instead of content. And it much more elegant than some of the other solutions I have seen (and written) to get the same thing. Everything from something like my map + filter hack above to some more advanced uses for the old school for loop.


November 26, 2017

My favourite thing about emacs is how easy it is to make a simple function that solves something that don’t work like you want it to, or something that takes too many steps


November 26, 2017

Sanity is highly overrated.


November 24, 2017

Template literals

November 24, 2017

There exists like a billion templating libraries for Javascript, and I guess many of them exist mostly to get around the problem of: How to generate html without doing that weird "<h1>"+title+"</h1>" problem. It works fine for short stuff, but it doesn’t take a lot before it becomes too cumbersome for anyone to enjoy.

Template Literals to the rescue.

All good programming languages have a way to do something like for example this in C#

This is where one of my favourite features in ES6 comes in, that I always forget to use, because the plus hack is to ingrained in my brain that I always forget to use it:

They are great, and could be used as a mini templating languages, if you needs are too simple to include something like Pug or Handlebars; they do exactly what they say they are, and work great. But I’m not the worlds biggest fan of the use of ticks: `. Mainly because it is kind of weird how they are inserted into text files. Other than that, great.

I get why they didn’t, but I kind of just wish they had broken the String type and let us use ${} inside regular strings.


November 23, 2017

Every time I introduce someone to pingdom they “complain” about it being expensive. I think it is cheap, if you take into consideration that it discovers issues in 9/10 times before our customers does.