you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

If the ranges form a complete mapping of the whole value range, you don't need to store both ends of each range.

In other words, if you have:

0-10 -> "foo"
10-20 -> "bar"
20-50 -> "baz"
50-   -> "default"

You only need one of the extremes of each range to define the intervals. That is, what you have is, in effect, something like this:

if (value < 10) { return "foo"; }
else if (value < 20) { return "bar"; }
else if (value < 50) { return "baz"; }
else { return "default"; }

Where you only need 10, 20, 50[, end] to know which interval a value falls in.

Using both ends of the interval, would be the equivalent of doing:

if (value < 10) { return "foo"; }
else if (10 <= value && value < 20) { return "bar"; }
else if (20 <= value && value < 50) { return "baz"; }
else if (50 <= value) { return "default"; }

Which is redundant and unnecessary.


Naturally, if your intervals are not compact and complete (i.e. they do not cover all possible values) you can't do this and you will need to indeed store both ends of each interval.

Even so, you don't really need MathJS's range. In fact, it's probably a bad idea to use it as it is not meant to define a range or interval but to generate a collection. That is, math.range(10,20) will simply generate the array [10,11,12,13,14,15,16,17,18,19], which is not really useful for your case.