Skip to content

Instantly share code, notes, and snippets.

View nfroidure's full-sized avatar
🌻
Getting greener

Nicolas Froidure nfroidure

🌻
Getting greener
View GitHub Profile
@nfroidure
nfroidure / SimpleSingleton.js
Last active December 31, 2018 17:30
Part of my blog post Revisiting Singleton Design Pattern : https:///insertafter.com
// Singleton pattern
var MySingleton=(function() {
// creating a variable to contain the instance
var instance=null;
// here goes private stuff
var _myPrivateVar=1;
var _myPrivateFunction=function(){
console.log('In private function.');
};
// creating singleton constructor
@nfroidure
nfroidure / Queue.js
Last active December 31, 2018 16:43
A simple Javascript Queue based on an Array wrap and using setters/getters. More explanations here : https://insertafter.com/blog-fifo_lifo_javascript.html
class Queue {
constructor(...elements) {
// Initializing the queue with given arguments
this.elements = [...elements];
}
// Proxying the push/shift methods
push(...args) {
return this.elements.push(...args);
}
shift(...args) {
@nfroidure
nfroidure / Stack.js
Last active December 31, 2018 16:39
A simple Javascript Stack based on an Array wrap. More explanations here : https://insertafter.com/blog-fifo_lifo_javascript.html
class Stack {
constructor(...elements) {
// Initializing the stack with given arguments
this.elements = [...elements];
}
// Proxying the push/shift methods
push(...args) {
return this.elements.push(...args);
}
pop(...args) {
@nfroidure
nfroidure / Queue.js
Last active December 31, 2018 16:36
A simple Javascript Queue based on an Array wrap. More explanations here : https://insertafter.com/blog-fifo_lifo_javascript.html
class Queue {
constructor(...elements) {
// Initializing the queue with given arguments
this.elements = [...elements];
}
// Proxying the push/shift methods
push(...args) {
return this.elements.push(...args);
}
shift(...args) {
const noop = arg => arg;
const combine = () => Array.from(arguments).reduce((a, b) => a.concat(b));
const compact = arr => arr.filter(el => el);
const difference = () => {
var others = Array.from(arguments).slice(1).reduce((a, b) => a.concat(b));
return arguments[0].filter(el => !others.some(exclude => el === exclude));
};
function buildMessage(values) {
const DIFF = Math.pow(2, 9); // 512
const MAX_PRECISION = 4; // 512 * 4 allows to encode 300 <= x <= 1100
const maxIndex = values.indexOf(Math.max(...values));
const minIndex = values.indexOf(Math.min(...values));
const maxDiff = values.reduce(
(diff, v) => Math.max(v - values[minIndex], diff),
0
);
const precision = Math.ceil(maxDiff / DIFF);
package main
import (
"bufio"
"fmt"
"log"
"math"
"os"
"path/filepath"
"strconv"
@nfroidure
nfroidure / manual.js
Created June 5, 2017 09:44
`swagger-http-router` with manual DI
import { getInstance, initializer } from 'knifecycle';
import uuid from 'uuid';
import { initDelayService } from 'common-services';
import {
initRouter,
initHTTP,
initHTTPTransaction
} from 'swagger-http-router';
@nfroidure
nfroidure / api.swagger.json
Created May 11, 2017 07:25
API Client generation experiment
{
"swagger": "2.0",
"info": {
"description": "WMG web services running altogether",
"version": "5.6.0",
"title": "infrastructure-wmg"
},
"host": "localhost:1664",
"schemes": [
"https"
@nfroidure
nfroidure / workflow-oriented-controller.js
Last active November 7, 2016 07:51
A worflow oriented controller
// No middleware, just pur functions
import getBodyFromReq from 'pureBodyParser';
import getQueryFromReq from 'pureQueryParser';
import sendToRes from 'pureResponseMaker';
// Use dependency injection for required services
// app/config/timer just come from this function caller
module.exports = ({ app, config, timer}) => {
app.post((req, res) => {