µ-posts

2020-06-14 16:17:46 +0200 +0200

Let’s assume we have an array of data which we want to process into an array of objects (in Javascript). For instance:

const data = [1, 2, 3, 4, 5, 6];

const squares = data.map((x) => {
  return { number: x, square: x * x };
});

// [{ number: 1, square: 1 },
// { number: 2, square: 4 }, ...]

How do we force the parser to treat the object literal as an Object (without return)? This won’t work, clearly:

data.map((x) => {
  number: x, square: x * x
});
// Syntax error

In order to do this, we simply need to wrap the literal in parenthesis. This forces the parser to treat it as a literal and it works as expected:

data.map((x) => ({ number: x, square: x * x }));
// [{ number: 1, square: 1 },
// { number: 2, square: 4 }, ...]
2020-06-13 21:59:46 +0200 +0200

In a recent Go project where I’ve used an object-relational mapper (GORM), I needed to whether a time.Time (mapped to a potential NULL database field) had been initialized of not.

Go’s initialisation value for time.Time is 0001-01-01 00:00:00 +0000 UTC (1st January 2001). It turns out time.Time provides a convenient method (IsZero()) to do just this.

import "time"

 var myDate time.Time

if myDate.IsZero() {
    // not initialised
} else {
    // initialised
}
2020-06-11 00:53:46 +0200 +0200

CVSS (Common Vulnerability Scoring System) is a standard measure of a vulnerability’s severity. It takes several factors into account, such as impact, temporal and environmental metrics. For a dataset that I’m working on, this is a comparison of the CVSS3 score against the more coarse grained “severity” score. We normalise the impact data (originally from $[0, 3]$) as well as the cvss3_score and produce a regression plot.

from sklearn import preprocessing
import numpy as np

scaler = preprocessing.MinMaxScaler()
scaled_data = scaler.fit_transform(data)
sns.regplot(x=scaled_data[:,0],
            y=scaled_data[:,1],
            x_estimator=np.mean)
ax.set(xlabel='CVSS3', ylabel='Impact')

2020-06-10 19:13:46 +0200 +0200

Trying out a section of the site for “micro blogging”. This is will include random, assorted thoughts. “Macro” blogging (also known as “Posts") will be available in the usual section.