Skip to content

Instantly share code, notes, and snippets.

Avatar

Alexandre Moreno moleike

View GitHub Profile
@moleike
moleike / restful.js
Created Mar 29, 2016 — forked from BinaryMuse/restful.js
Express API with Async/Await
View restful.js
import express from "express";
/**
* Takes a route handling function and returns a function
* that wraps it after first checking that the strings in
* `reserved` are not part of `req.body`. Used for ensuring
* create and update requests do not overwrite server-generated
* values.
*/
function checkReservedParams(routeHandler, ...reserved) {
@moleike
moleike / http-errors.js
Last active Dec 22, 2019 — forked from justmoon/custom-error.js
HTTP Error classes in Node.js
View http-errors.js
'use strict';
const statusCodes = require('http').STATUS_CODES;
function createError(code, name) {
return function(message) {
Error.captureStackTrace(this, this.constructor);
this.name = name;
this.message = message;
this.statusCode = code;
}
@moleike
moleike / lazy.js
Created Apr 8, 2016 — forked from kana/lazy.js
Lazy evaluation in JavaScript
View lazy.js
function delay(expressionAsFunction) {
var result;
var isEvaluated = false;
return function () {
if (!isEvaluated)
result = expressionAsFunction();
return result;
};
}
@moleike
moleike / defaults-overrides.md
Created Apr 21, 2016 — forked from ericelliott/defaults-overrides.md
ES6 defaults / overrides pattern
View defaults-overrides.md

ES6 Defaults / Overrides Pattern

Combine default parameters and destructuring for a compact version of the defaults / overrides pattern.

function foo ({
    bar = 'no',
    baz = 'works!'
  } = {}) {
View gist:5bc65c431d65e1cd06954f62677bf8ce
var util = require('util')
function hook_stdout(callback) {
var old_write = process.stdout.write
process.stdout.write = (function(write) {
return function(string, encoding, fd) {
write.apply(process.stdout, arguments)
callback(string, encoding, fd)
}
@moleike
moleike / reverse-proxy.js
Last active Aug 26, 2016
an http api for mqtt-shepherd
View reverse-proxy.js
// reverse proxy (HTTP(s) to LWM2M over MQTT)
// this should be mounted on an Express app
const router = require('express').Router()
const shepherd = require('lib/shepherd');
const ch = require('lib/status'); // coap to http status
const herrero = require('herrero');
const bodyParser = require('body-parser')
const { NotFoundError } = herrero;
View parseAttributes.js
const parseAttributes = payload => payload
.split(',')
.map(pair => pair.split('='))
.filter(pair => pair.length === 2)
.map(([key,val]) => ({ [key]: val }))
// > parseAttributes('pmin=5,pmax=40')
// [ { pmin: '5' }, { pmax: '40' } ]
@moleike
moleike / pearcy.py
Created Sep 24, 2016 — forked from anonymous/pearcy.py
Compute and plot Pearcey integral
View pearcy.py
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as integrate
# Dimension of image in pixels
N = 256
# Number of samples to use for integration
M = 32
@moleike
moleike / latency.markdown
Created Oct 9, 2016 — forked from hellerbarde/latency.markdown
Latency numbers every programmer should know
View latency.markdown

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs
Read 1 MB sequentially from memory ..... 250,000 ns  = 250 µs
View quicksort.hs
-- randomized quick sort with 3-way partition
module QuickSort (quicksort) where
import Data.Array.ST
import Control.Monad
import Control.Monad.ST
import System.Random
type QA s = STUArray s Int Int
swap :: Int -> Int -> QA s -> ST s ()
You can’t perform that action at this time.