Skip to content

Instantly share code, notes, and snippets.

View tail-call's full-sized avatar
🐚
Conch

Maria tail-call

🐚
Conch
View GitHub Profile
@tail-call
tail-call / gist:4e711120f94a22d89a892d249ca79cb9
Created November 1, 2017 08:53
Pick random weighted element from dictionary
def get_random_element(dictionary):
S = 0
r = random.random()
for (key, val) in dictionary.items():
S += val
if S > r:
return key
@tail-call
tail-call / getNormal.js
Created November 1, 2017 09:20
Get normally distributed random value
// From <https://github.com/ondras/rot.js/blob/master/src/rng.js>
// I have no idea how this works
function getNormal (mean, stddev) {
do {
var u = 2*this.getUniform()-1;
var v = 2*this.getUniform()-1;
var r = u*u + v*v;
} while (r > 1 || r == 0);
#lang racket
(define (last items)
"Get the last element of ITEMS"
(foldl (lambda (x acc) x) (void) items))
@tail-call
tail-call / lwjgl-empty-window.scala
Created April 2, 2018 04:23
Scala LWJGL window template
import org.lwjgl.Version
import org.lwjgl.glfw._
import org.lwjgl.opengl.GL
println("LWJGL Version "+ Version.getVersion() +" is working.")
// Print to console on error
GLFW.glfwSetErrorCallback(
GLFWErrorCallback.createPrint(System.err)
)
function iota (n, zero = 0, delta = 1) {
return (new Array(n)).fill(null).map((_, i) => zero + delta * i);
}
@tail-call
tail-call / package.json
Created August 7, 2018 00:10
React/babel/browserify minimal project
{
"name": "tail-call-ru",
"version": "0.0.1",
"description": "My personal site",
"main": " ",
"dependencies": {},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
// Coll zero padding hack
// See <https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date#comment52315011_30272803>
(new Array(100)).fill(null).map((_, i) => ("0" + i).slice(-2))
@tail-call
tail-call / submit.js
Created March 24, 2019 03:49
Submit virtual form from JS
function makeElement(tagName, attributes, ...children) {
let element = document.createElement(tagName);
Object.assign(element, attributes);
for (let child of children) {
element.appendChild(child);
}
return element;
}
@tail-call
tail-call / Enum.js
Created April 17, 2019 05:03
Enum idea (JS)
class Enum {
static create(...members) {
class DerivedEnum extends Enum {
static contains(member) {
return member instanceof DerivedEnum;
}
}
for (let member of members) {
DerivedEnum[member] = new DerivedEnum();
@tail-call
tail-call / example.js
Created June 25, 2019 08:06
Functions with verbose call syntax
import verboseFunction from './verbose.js';
const execute = verboseFunction(["after", "function"], (ms, cb) => setTimeout(cb, ms));
execute.after(1000).function(() => console.log("so verbose!"));
// You also get currying for free
const waitOneSecondAndExecute = execute.after(1000);