Skip to content

Instantly share code, notes, and snippets.

View hugmanrique's full-sized avatar

Hugo Manrique hugmanrique

View GitHub Profile
@hugmanrique
hugmanrique / optimizeFont.sh
Last active April 4, 2020 14:06
Font optimization
#!/bin/bash
# Required:
# fonttools, zopfli, installable via pip install.
set -e
ORIGINAL=original.otf
UNICODES_FILE=unicodes.txt
FORMATS=(woff woff2)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@hugmanrique
hugmanrique / PackageInjector.java
Created July 29, 2019 17:06
Byte Buddy PackageInjector
import com.google.common.reflect.ClassPath;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.loading.ClassInjector;
import java.io.File;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.nio.file.Files;
public enum BooleanFieldEnum {
// 2000 enum constants
private final boolean set;
BooleanFieldEnum() {
// Compute value on class loading, doesn't affect benchmark results
this.set = ThreadLocalRandom.current().nextBoolean();
}
@hugmanrique
hugmanrique / birthday.r
Created July 13, 2019 21:04
Birthday Paradox in R
d = 365 # days
n = 100 # people
prob = function(i) { 1 - ((d - 1) / d)^(i * (i - 1) / 2)}
X <- seq(1, n, 1)
plot(prob(X), type="l")
prob(72)
# = 0.9990993
@hugmanrique
hugmanrique / index.html
Last active June 12, 2019 19:51
Plot of a BivariateBeta(alpha_X, beta_X, alpha_Y, beta_Y) distribution (assumes X and Y are independent events)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Plot</title>
</head>
<body>
<div id="plot"></div>
<script src="https://unpkg.com/@stdlib/stdlib@0.0.32/dist/stdlib-flat.min.js"></script>
@hugmanrique
hugmanrique / findMinimum.js
Last active July 24, 2021 22:05
Multivariate (ℝ^2) relative minimum brute-force finder
// The function we want to find the minimums of
// https://i.imgur.com/GG6uXvD.png
const getValue = (m, n) => m * m + n * n;
// The sampling interval (in ℝ^2)
let mStart = 0;
let nStart = 0;
let mEnd = 1;
let nEnd = 1;
@hugmanrique
hugmanrique / detectMath.js
Created December 30, 2018 21:59
Next.js MathML feature detection + polyfill
import Router from 'next/router';
const namespaceURI = 'http://www.w3.org/1998/Math/MathML';
let supports = false;
let inserted = false;
function supportsMathML() {
const div = document.createElement('div');
div.innerHTML = '<math><mspace height="23px" width="77px" /></math>';
@hugmanrique
hugmanrique / Navbar.js
Last active November 18, 2022 14:40
Next.js Navbar component using the new React Hooks API
import React, { useState, useEffect } from 'react';
import Router from 'next/router';
import Link from 'next/link';
export default function Navbar({ brand, items }) {
const [activeIndex, setActiveIndex] = useState(-1);
useEffect(
() => {
const handleRouteChange = url => {
@hugmanrique
hugmanrique / regionfile-pr.md
Last active October 26, 2020 05:34
Paper PR about RegionFiles

Currently, the RegionFile (line 52) class extends the file if it's size is not a multiple of 4KB, but does it in a inefficient way:

for(int i = 0; i < (this.file.length() & 0xfff); i++) {
        this.file.write(0);
}

That code could potentially cause up to 4095 file extensions, when we could just do them at once with the following code: