Skip to content

Instantly share code, notes, and snippets.

View fasiha's full-sized avatar
💭
🧘‍♂️🐻

Ahmed Fasih fasiha

💭
🧘‍♂️🐻
View GitHub Profile
@fasiha
fasiha / demo.c
Created April 6, 2023 14:13
For a specific float32 `a`, check all 2-billion-odd float32s `b` if `a > b / 1e6 == a * 1e6 > b`. Answer: yes, that equality holds for this `a` and all float32s `b`.
// Compile with:
// clang -O2 -Wall f32.c; time ./a.out > a.txt
#include <float.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#define STARTO (0)
#define ENDO (UINT32_MAX)
@fasiha
fasiha / objectValueFilterArgMin.ts
Created March 31, 2023 21:34
A fast and optimized way to do `Object.values(obj).filter(filter).sort((a, b) => f(a) - f(b))[0]`
/**
* This is a fast and optimized way to do
* ```ts
* Object.values(obj).filter(filter).sort((a, b) => f(a) - f(b))[0]
* ```
* without an expensive sort and without intermediate array allocations.
*/
export function objectValueFilterArgMin<T>(
obj: Record<string, T>,
filter: (x: T) => boolean,
@fasiha
fasiha / reselect-demo.js
Last active April 6, 2023 13:36
Basic Reselect/Redux mental model, see https://github.com/reduxjs/reselect
var { createSelector } = require('reselect');
var state = { symbols: [] };
var symbolsSet = createSelector([(s) => s.symbols], (symbols) => {
console.log('RERUNNING SET CREATION');
return new Set(symbols);
});
console.log(symbolsSet(state));
@fasiha
fasiha / demo.py
Created February 5, 2023 17:28
Density of the max of independent Beta random variables implementing https://math.stackexchange.com/a/2965587
import pylab as plt
from math import prod
import numpy as np
from scipy.stats import beta as betarv
from scipy.special import betainc, beta as betafn
plt.ion()
plt.style.use('ggplot')
# %matplotlib qt
@fasiha
fasiha / App.tsx
Last active December 30, 2022 07:15
D3, React, and TypeScript. TypeScript generalization of https://stackoverflow.com/a/69044058
/*
This assumes you have a React app. Then,
```bash
npm i d3
npm i -D @types/d3
```
if necessary. Then use this component.
*/
import * as d3 from 'd3';

Oral history of Freenet from Chris Wellons https://nullprogram.com via email on May 6, 2017, with permission granted on Sep 3, 2018 to publish online. Lightly edited.


Have you ever heard of Freenet? I haven't paid any attention to it in almost a decade, so I have no idea what state it's in these days. It's a lot more advanced than Dat, but less accessible -- you have to install a large, ill-behaved Java application. Like a Dat URL, Freenet URLs embed a key that encrypt and authenticate the data, so only people who know the URL can decrypt it. It has both static URLs with fixed data (like

@fasiha
fasiha / tld-price.md
Last active October 29, 2022 23:48
Cloudflare domain registration costs per TLD, as if October 4, 2022, sorted by increasing price
  • $8.18 .business
  • $8.18 .company
  • $9.15 .com
  • $9.18 .contact
  • $9.33 .xyz
  • $12.18 .group
  • $12.18 .place
  • $12.18 .rocks
  • $13.68 .biz
  • $14.18 .blue
@fasiha
fasiha / safeArithmetic.ts
Created October 13, 2022 16:34
Given an object with nullable numbers, and an arithmetic function of a similar object with non-nullable values, run the function if all values are non-null/undefined numbers.
/**
* Given an object with nullable numbers, and an arithmetic function of a
* similar object with non-nullable values, run the function if all values are
* non-null/undefined numbers.
*/
function safeArithmetic<T extends string>(
args: Record<T, number | null | undefined>,
fn: (ts: Record<T, number>) => number | undefined | null,
): number | undefined | null {
// TypeScript pacification: unclear why this isn't inferred, instead it's "unknown[]"?
@fasiha
fasiha / demo.ts
Last active November 14, 2023 15:54
How to efficiently and compactly limit concurrency in JavaScript's Promise.all. A simplified and convincing demo for https://stackoverflow.com/a/51020535/ that you can run on TypeScript Playground
const sleep = (t: number) => new Promise((rs) => setTimeout(rs, t));
/**
* This function will return an array of the same length as the input,
* but not in the same order. It's straightforward to extend it to
* preserve order, but I left it like this because this way the timing
* is clearly visible. This helped me confirm that it works.
*/
async function processArray(input: number[], numWorkers: number) {
const ret: { date: number; description: string }[] = [];
@fasiha
fasiha / hi.txt
Created July 7, 2022 02:16
gist for storing attachments
This is just a gist to let me attach images in comments for a URL.