Skip to content

Instantly share code, notes, and snippets.

Avatar

Lucas Jellema lucasjellema

View GitHub Profile
@lucasjellema
lucasjellema / debug-upon-error-2.js
Created Jul 7, 2020
Collect debug messages to only print to output when an exception occurs
View debug-upon-error-2.js
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
View debug-upon-error.js
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 Dec 18, 2019
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)
View welcome.sh
# 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
ES 2018 Demonstration of Promises, Asynchronous Generators and Pipelining (requires Node 10 or higher) - Time Windowed Aggregates
View pipelined-sensor-analysis-1.js
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
ES 2018 Demonstration of Promises, Asynchronous Generators and Pipelining (requires Node 10 or higher) - Time Windowed Aggregates
View pipelined-sensor-analysis-1.js
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
ES 2018 Demonstration of Promises, Asynchronous Generators and Pipelining (requires Node 10 or higher) for running agregates
View pipelined-sensor-analysis-0
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 Apr 29, 2019
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.
View pancake-party-0.js
// 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 Apr 29, 2019
ES 2018 Demonstration of Promises, Asynchronous Generators and Pipelining (requires Node 10 or higher)
View sensor-analysis-0.js
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 Apr 24, 2019
ES 2018 Asynchronous Iterator (async generator function and await for..of loop) - foundation for pipelined and nevery ending generator functions
View node-generator.js
// 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 Dec 31, 2018
Node JS code to load JSON document with countries and prepare some useful Sets with unique collections of data from the JSON document
View neo4j-node.js
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) =>
You can’t perform that action at this time.