Published
Edited
Sep 27, 2019
Insert cell
Insert cell
Insert cell
function deconstruct(number){
let sign = 1;
let coefficient = number;
let exponent = 0;
// Remove sign from the coefficient
if(coefficient < 0){
coefficient = -coefficient;
sign = -1;
}
if(Number.isFinite(number) && number !== 0){
//=================================================
// Reduce coefficient
exponent = -1128; // Exponent of Number.MIN_VALUE
let reduction = coefficient;
// Divide by 2 until we have shifted all
// bits away to zero.
while(reduction !== 0){
exponent += 1;
reduction /= 2;
}
//=================================================
// Reduce exponent
// Bring exponent to zero so number can be viewed as integer
reduction = exponent;
while(reduction > 0){
coefficient /= 2;
reduction -= 1;
}
while(reduction < 0){
coefficient *= 2;
reduction += 1;
}
}
return {
equation: `${sign} * ${coefficient} * (2 ^ ${exponent})`,
sign,
coefficient,
exponent,
number,
};
}
Insert cell
deconstruct(Number.MAX_SAFE_INTEGER)
Insert cell
deconstruct(1)
Insert cell
deconstruct(-1)
Insert cell
deconstruct(0.1)
Insert cell
Insert cell
Insert cell
way_1 = deconstruct( (0.1 + 0.2) + 0.3 )
Insert cell
way_2 = deconstruct( 0.1 + (0.2 + 0.3) )
Insert cell
way_1.equation === way_2.equation
Insert cell
Insert cell
((0.1 + 0.2) + 0.3) === (0.1 + (0.2 + 0.3))
Insert cell
Insert cell
function refine(collection, path){
// Walk down path into object to find value desired
return path.reduce(
function(refinement, element){
try {
return refinement[element];
} catch (ignore) {}
},
collection
);
}
Insert cell
refine({first: "Nick", last: "Strayer", job: "Statistician"}, ['last'])
Insert cell
refine({name: {first:"Nick", last: "Strayer"}, job: "Statistician"}, ['name','first'])
Insert cell
function by(...keys){
const paths = keys.map(
function(element){
return element.toString().split(".");
}
);
// Compare each pair of values until finding a mismatch
// if no mismatch, then items are equal.
return function compare(first, second){
// These hold the first value that differs between
// the first and second object
let first_value;
let second_value;
// Walks down path until we find where the two
// objects differ from eachother. If none do, then
// we just return as objects are essentially same.
const all_values_equal = paths.every(function(path){
first_value = refine(first, path);
second_value = refine(second, path);
return first_value === second_value;
});

if (all_values_equal){
return 0;
}
return(
(
typeof first_value === typeof second_value
? first_value < second_value
: typeof first_value < typeof second_value
)
? -1
: 1
);
}
}
Insert cell
[
{first: "Frank", last: "Farkel"},
{first: "Fanny", last: "Farkel"},
{first: "Douglas", last: "Crockford"},
{first: "Aardvark",last: "Zamboni"},
{first: "Nick", last: "Strayer"}
].sort(by('first','last'))
Insert cell
[
{first: "Frank", last: "Farkel"},
{first: "Fanny", last: "Farkel"},
{first: "Douglas", last: "Crockford"},
{first: "Aardvark",last: "Zamboni"},
{first: "Nick", last: "Strayer"}
].sort(by('last','first'))
Insert cell
Insert cell
function sealer_factory(){
// Initialize a map that will store objects
const weakmap = new WeakMap();
return {
sealer(object){
// Build a box out of a fully empty and immutable object
const box = Object.freeze(Object.create(null));
// Set that box as the key to access the object passed in map
weakmap.set(box, object);
// Return the box to user.
return box;
},
unsealer(box){
// Access whatever object was put in map by sealer
return weakmap.get(box);
}
}
}
Insert cell
{
const my_secret_container = sealer_factory();
const important_object = {type: 'cat', name: 'flumpert'};
// Seal object in box
const key_for_important_object = my_secret_container.sealer(important_object);
// Some code happens...
// I want to get my important object now...
return my_secret_container.unsealer(key_for_important_object);
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more