Skip to content

Instantly share code, notes, and snippets.

View mikesamuel's full-sized avatar

Mike Samuel mikesamuel

View GitHub Profile
@mikesamuel
mikesamuel / histogram.py
Last active July 11, 2024 21:46
Python histogramming
def histogram(nums, n_buckets, title):
min_val = min(*nums)
max_val = max(*nums)
buckets = []
bucket_width = (max_val - min_val) / float(n_buckets)
while (len(buckets) < n_buckets):
buckets.append(0)
sum_of_values = 0
for num in nums:
sum_of_values += num
@mikesamuel
mikesamuel / output.txt
Last active February 16, 2024 20:52
Exploring how regexes work differently on engines with different code-unit sizes / assumed encodings
Matching code-units that have all the same size OK.
applying /a/ to '"aό𝄞"'
. py UTF-8
. . 00000000 61 |a|
. . 00000001
. js UTF-16
. . 00000000 61 |a|
. . 00000001
. py UTF-32
. . 00000000 61 |a|
@mikesamuel
mikesamuel / subjectivity.html
Created November 16, 2023 21:59
Subjective method dispatch explanation
<!doctype html><meta charset=utf-8>
<!-- this file is subjectivity.html -->
<script>
// Just calls a method on the string it's given.
// Below each iframe locally overrides that method.
window.makeItBold = (str) => str.bold();
// We're loading the same HTML in three frames so fork.
switch (window.location.hash) {
@mikesamuel
mikesamuel / cmp.js
Created November 14, 2023 20:56
JS cmp function sorting NaN high and distinguishing zeroes
function cmpNums(a, b) {
if (Object.is(a, b)) { return 0; }
let delta = a - b;
if (delta) {
return Math.sign(delta);
} else if (delta === 0) {
// The only values that do not match Object.is above
// and which do not lead to a NaN delta are -0, +0
// and vice versa.
return Object.is(a, -0) ? -1 : 1;
@mikesamuel
mikesamuel / examples.md
Last active July 11, 2024 06:08
Lexically nested types

In JavaScript a nested type can read and assign variables from outer scopes.

function counter(start = 0) {
  let n = start;
  class Counter {
    peek() { return n; }
    increment() { ++n; }
  }
 return new Counter();
@mikesamuel
mikesamuel / Foo.java
Created June 3, 2021 13:43
Java boxing is violable
import java.lang.reflect.*;
import java.util.*;
public class Foo {
public static void main(String[] argv) throws Exception {
Class<?> integerCacheClass = Arrays.stream(Integer.class.getDeclaredClasses())
.filter(c -> c.getSimpleName().equals("IntegerCache"))
.findFirst()
.get();
Field cacheField = Arrays.stream(integerCacheClass.getDeclaredFields())
@mikesamuel
mikesamuel / test.js
Last active April 3, 2019 19:20
Informal tests for augmented HostEnsureCanCompileStrings
// Run in the context of a HostEnsureCanCompileStrings callout that
// throws an EvalError (from the callee realm) for string values, and
// arrays.
const PASS = {}, FAIL = {};
function tryCompileAsFunctionBody(description, source, expectedStatus) {
let status = FAIL;
try {
new Function(source);
@mikesamuel
mikesamuel / unundoable.js
Created March 14, 2019 04:53
Things you can't undo
// On the server
response.write(x);
// On the client
document.body.innerHTML = x;
@mikesamuel
mikesamuel / weird0.html
Created February 24, 2019 15:51
Yet another example of why HTML is weird.
<script>/*&#42;/alert('svg');//*/alert('html');
//<![CDATA[</script><!--]]>--></script>
<svg>
<script>/*&#42;/alert('svg');//*/alert('html');
//<![CDATA[</script><!--]]>--></script>
</svg>
@mikesamuel
mikesamuel / type-checking-code-loaders.js
Last active January 20, 2019 15:16
Checking arguments to Function/eval
class TrustedScript { // Just a stub of github.com/wicg/trusted-types
constructor(x) {
this.content = String(x);
}
toString() {
return this.content;
}
static createUnsafely(x) {