hjertnes.blog

#

30.11.2017 01:00

I get why some people want an iOS laptop or iPad laptop. While at the same time realizing it would never be for me. I would be perfectly happy if Apple would make their keyboards reliable again. And maybe a SD card slot?

Immutability

30.11.2017 01:00

You basically have two different ways which you can change the contents of a variable. You can change or mutate it, or you can repalce it. There are good and bad sides to both. The biggest advantage of mutating data is that it is more efficient. While the biggest advantages of Immutability is that you often avoid a certain type of very difficult to avoid bug and that they are easier to understand.

Let’s take the following example. You have a toggle button, that changes the status of a list or selection of items. And it crashes 20% of the time when our CEO uses it. If this was a mutable method you would end up in a interesting place, because parts of the data set would be in one state, while the rest would be in another. While it wouldn’t be a huge problem if we used a immutable method like .map, because nothing would happen until we complated the map and replaced the exisitng state.

When you deal with immutability, you can write very simple methods that take any number of arguments, makes a copy of the data and changes it, returns a new copy. This means that it only needs to know what it is supposed to do and it will always do the same thing with the same input. WHile a immutable method might live in a class, and attributes defined in the class instead of arguments.

What happens if some other methods change one of them in the middle?

I personally think that using pure functions, that returns a new copy of the data and gets all the “state” as parameters is a much clearer and less buggy way to solve problems. More reliable code is always better than the fastest possible; right?

Moving code to gists

30.11.2017 01:00

For various reasons I have moved this site over to WordPress; none of them are very important in the grand scheme of things. Anyways. I have been spending quite a lot of time trying to figure out how to best deal with code in these blog posts. And I have come to the conclusion that the best compromise between work I have to do, and the best possible expereince for you as a reader of this site is to embed GitHub Gists.

#

29.11.2017 01:00

When I wonder: why haven’t I spent much time on micro.blog this week, the answer is almost always in wakatime.

#

29.11.2017 01:00

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

Adding elements to an array

29.11.2017 01:00

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.

Splice

28.11.2017 01:00

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.

#

27.11.2017 01:00

The emoji for American power outlets: 😮

Ink Review

27.11.2017 01:00

I received this 30ml bottle of ink free of charge from Tudos, for the purpose of reviewing it. Tudos have been very supportive of The Ink Smudge since our paths crossed, and I would recommend checking them out if you live in Norway. I buy all of my notebooks from there

Funny thing, I can never for the live of me remember what this ink is called. I always have to reference my “Transparency” page.

This is my first bottle of ink, that isn’t from Iroshizuku in a few years. I got to say it, the experience is very different.

First of all, I love the ink, the colour is awesome and I enjoyed writing with it. Are there inks I didn’t enjoy using? Yes, I have used inks that feels “scratchy”, and I really hatet that. I assume it was because the ink wasn’t lubricated enough.

But, the bottle feels really cheap. It is plastic. This isn’t all bad. But it is another world than the awesome glass bottles Iroshizuku comes in. I see two problems with it though, the hole can be a little bit narrow for some pens, and there is no design accommodations to make sure you can use as much ink as possible.

Keep I’m mind though, this ink is priced at $7.50 while a Iroshizuku is priced at $30: the latter comes in a larger bottle, but still at least twice the price. ($0.25 per ml vs $1,7 pr ml)

This ink looks awesome. It is kind f weird that I have gotten so into blue inks lately. I used to hate them.

This ink reminds me of those light blue standard inks everyone sends you with their pens. I call it “BIC blue”.

I hate those inks, but I love this one. It is a darker, and looks “just right”. I have tested it with all of my pens and it looks just as good with finer and broader nibs.

The only bad thing about this ink is that the dry time is significantly longer than with Iroshizuku. It isn’t so long that it is a problem. But I notice that it is something I need to think a little bit about.

Keep it in mind if it is the kind of thing that is a deal breaker for you.

Slice

27.11.2017 01:00

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.