Skip to content

Instantly share code, notes, and snippets.

View lucasjellema's full-sized avatar

Lucas Jellema lucasjellema

View GitHub Profile
@lucasjellema
lucasjellema / debug-upon-error-2.js
Created July 7, 2020 14:41
Collect debug messages to only print to output when an exception occurs
const debugBuffer = []
const savepoints = {}
const debug = function (msg) {
// using the full UTC string is a bit heavy handed perhaps; only minutes, seconds and ms would be quite enough, or just the timestamp in ms
debugBuffer.push(`${new Date().toUTCString()} ${msg}`)
}
const setSavepoint = function (savepoint) {
savepoints[savepoint] = debugBuffer.length
const debug = function (debugBuffer, msg) {
// using the full UTC string is a bit heavy handed perhaps; only minutes, seconds and ms would be quite enough, or just the timestamp in ms
debugBuffer.push(`${new Date().toUTCString()} ${msg}`)
}
const spillDebugBeans = function (debugBuffer) {
debugBuffer.forEach(msg => { console.log(`DEBUG: ${msg}`) });
}// spillDebugBeans
const calculateSums = async function (sums) {
@lucasjellema
lucasjellema / welcome.sh
Last active December 18, 2019 14:50
Example of how to call a function in a Linux Shell Script - passing in input parameters and receiving the result from the function (in a way that resembles modern programming languages)
# a global variable
WELCOME_ROOT=Hello
# invoke this function with two arguments:
# 1. (FROM_VAR) the name of the greeter
# 2. (TO_VAR) the name of the greetee
# this function returns a welcoming message
get_welcoming_message()
{
FROM_VAR=$1
@lucasjellema
lucasjellema / pipelined-sensor-analysis-1.js
Created May 1, 2019 05:19
ES 2018 Demonstration of Promises, Asynchronous Generators and Pipelining (requires Node 10 or higher) - Time Windowed Aggregates
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, Math.floor(milliseconds)))
}
const lg = (msg) => {
const d = new Date()
console.log(`${d.getSeconds()}.${Math.round(d.getMilliseconds() / 100)} - ${msg}`)
}
// each sensor has a slightly randomized timeput period, suggesting a different and somewhat varying production rate of measurements
@lucasjellema
lucasjellema / pipelined-sensor-analysis-1.js
Created May 1, 2019 05:19
ES 2018 Demonstration of Promises, Asynchronous Generators and Pipelining (requires Node 10 or higher) - Time Windowed Aggregates
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, Math.floor(milliseconds)))
}
const lg = (msg) => {
const d = new Date()
console.log(`${d.getSeconds()}.${Math.round(d.getMilliseconds() / 100)} - ${msg}`)
}
// each sensor has a slightly randomized timeput period, suggesting a different and somewhat varying production rate of measurements
@lucasjellema
lucasjellema / pipelined-sensor-analysis-0
Created May 1, 2019 05:17
ES 2018 Demonstration of Promises, Asynchronous Generators and Pipelining (requires Node 10 or higher) for running agregates
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, Math.floor(milliseconds)))
}
const lg = (msg) => {
const d = new Date()
console.log(`${d.getSeconds()}.${Math.round(d.getMilliseconds() / 100)} - ${msg}`)
}
// each sensor has a slightly randomized timeput period, suggesting a different and somewhat varying production rate of measurements
@lucasjellema
lucasjellema / pancake-party-0.js
Last active April 29, 2019 09:36
Simulating a birthday party where pancakes are served - this gist describes a naïve approach (no parallel or aysnchronous processing, no use of pipelining) and a smart approach (where those mechanisms are employed). The code demonstrates the use of asynchronous generators in combination with promises.
// pancake party for my son's birthday
// I have promised him pancakes. Each pancake has to be baked, decorated (syrup, sugar, jam, ..) and sliced (in bite size sections)
const numberOfGuests = 8
// assuming each guest eats exactly three pancakes
const totalNumberOfPancakes = numberOfGuests * 3
const numberOfPans = 1
// times in milliseconds
const timeToBakeOnePancake = 3000;
@lucasjellema
lucasjellema / sensor-analysis-0.js
Last active April 29, 2019 04:47
ES 2018 Demonstration of Promises, Asynchronous Generators and Pipelining (requires Node 10 or higher)
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, Math.floor(milliseconds)))
}
const lg = (msg) => {
const d = new Date()
console.log(`${d.getSeconds()}.${Math.round(d.getMilliseconds() / 100)} - ${msg}`)
}
// each sensor has a slightly randomized timeput period, suggesting a different and somewhat varying production rate of measurements
@lucasjellema
lucasjellema / node-generator.js
Created April 24, 2019 19:23
ES 2018 Asynchronous Iterator (async generator function and await for..of loop) - foundation for pipelined and nevery ending generator functions
// asynchronous generator - read in await for..of loop
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
const lg = (msg) => {
const d = new Date()
console.log(`${d.getSeconds()}:${d.getMilliseconds()} - ${msg}`)
}
const alphabet = async function* () {
@lucasjellema
lucasjellema / neo4j-node.js
Created December 31, 2018 17:10
Node JS code to load JSON document with countries and prepare some useful Sets with unique collections of data from the JSON document
var countriesDocumentURL = "https://raw.githubusercontent.com/mledoze/countries/master/countries.json"
request(countriesDocumentURL, async function (error, response, body) {
var countries = JSON.parse(body)
// get unique region values (see: https://codeburst.io/javascript-array-distinct-5edc93501dc4)
// take all elements in the countries array, for each of them: take the region element; create a Set of all the resulting region values (a Set contains unique elements)
var regions = [...new Set(countries.map(country => country.region))]
var subregions = [...new Set(countries.map(country => country.subregion))]
// see https://stackoverflow.com/questions/39837678/why-no-array-prototype-flatmap-in-javascript for this flatMap function
const flatMap = (f, xs) =>