Hjertnes.blog

Looping without loops.

November 20, 2017

As a coder or a apiring one you have probably experienced many moments where all the smokes blows away and you understand something much clearer. One of these moments for me was when I was introduced to recoursion. Probably, while learning Scheme or Haskell. The basic idea is that a function calls itself. Don’t use recoursion when you write definitions, because circularity is BAD philosophy.

Here is a very simple example that loops over an array, and prints the result, without using a regular loop; like while, for, do, map, forEach etc.

You could of course do the same with a for of for example:

To figure out how to do something, without using the “default” is always a very good exercise. The basic idea above is to print the first element, and re-run the function if, the input is larger than one with the array minus the first element as input.

#

November 18, 2017

#

November 17, 2017

Higher order Functions

November 17, 2017

Higher order functions (or components, if you’re using React) is a functions that does at least one out of two thigns. It takes a function as a argument; not its value, but the function itself. Or it returns a function itself; again, not the value from it but the function itself. This might be considered a very exotic topic in many languages, but should be common practice for every JavaScript developer. You are doing this every time you use a callback, a map/filter/reduce or a promise.

The short example above are using a higher order function to double each element in the list. And the example below uses a higher order function that returns a function.

Higher Order Functions, in both forms is a very useful teqnique to avoid a lot of almost identical code. And even more useful when you start to code in React by doing the same thing just by taking components as arguments instead of functions or returning components instead of functions.

#

November 16, 2017

Join

November 16, 2017

Join is a method living on the String prototype. All it does is to take a list of strings and combines them into a string, either with or without a dividing character. Very useful if you have a array of something that you want to list out seperated by a comma or something like that. Below is an example, both with Join and I also re-implemented join as a reducer, just because they are so similar; but reduce is far more powerful.

#

November 15, 2017

Seeing people saying they are switching to Firefox makes me wonder: is this 2004?

#

November 15, 2017

Sorting stuff with Javascript

November 15, 2017

To sort stuff with Javascript is very easy, especially if it is a array of strings or numbers; then you can just do something like this:

But if you are sorting something like for example a array of objects or array of arrays, then you need to pass a function to it telling sort how to order the data. It is very easy to do, when you understand how it works. The way it works is that you define a function that recieves two elements, and your function have figure out if they are equal, if the first is larger or if the second is larger.<br /> The short version is:

<li>

  return 0 if they are the same

</li>

<li>

  return 1 if the first is larger than the last

</li>

<li>

  return -1 if the last is larger than the first

</li>

Example:

Simple and powerful. You can sort more or less anything with it. But it isn&#8217;t always the best idea. Remember, the sort method <em>HAS</em> side affects, it <em>MUTATES</em> the array you are running it on. </div>