Skip to content

Instantly share code, notes, and snippets.

View jimblandy's full-sized avatar

Jim Blandy jimblandy

View GitHub Profile
@jimblandy
jimblandy / foo.html
Created September 6, 2017 21:58
Example page illustrating element object properties visible as variables
<body>
<form id='myform' x="form's attr x">
<input id='mybutton' type='button' y="button's attr y"
onclick='console.log("hi, " + x + ", " + y); log_this(); log_that();'>
</input>
</form>
<script>
var form = window.document.getElementById('myform');
form.x = "form's property x";
@jimblandy
jimblandy / heads.py
Created August 4, 2017 03:51
There must be a better way to compute this...
import random
random.seed()
def flip():
return random.choice([True, False])
def run():
b = flip()
n = 1
@jimblandy
jimblandy / js-fn-edit.el
Last active July 18, 2017 00:24
Emacs major mode for editing SpiderMonkey JS_FN_HELP entries.
;;; edit-js-fn.el --- Edit JS_FN_HELP sections in SpiderMonkey shell files
;; See js-fn-edit-documentation.
(defvar js-fn-edit-original-buffer nil
"The buffer to which a js-fn-edit-mode buffer will store the edited text.")
(defvar js-fn-edit-original-region nil
"The region of js-fn-edit-original-buffer the original definition occupies.")
@jimblandy
jimblandy / co-contra-variance.md
Last active July 26, 2020 14:24
A simple explanation of covariance and contravariance

If you say "T is a subtype of U", that means that whenever someone wants a U, a T will do: every T works as a U. It follows from this phrasing that U is a subtype of U: certainly if someone wants a U, a U will do.

So if you imagine a type as denoting a set of values, you can write: T ⊆ U.

If every T works as a U, then a function that accepts any U is certainly also a function that accepts any T: fn(U) works as a fn(T). So fn(U) is a subtype of fn(T): fn(U)fn(T).

This is interesting, because the subtypedness gets reversed: T ⊆ U implies fn(U)fn(T).

@jimblandy
jimblandy / reborrows.md
Created January 4, 2017 23:00
Generic functions and implicit reborrows

Consider the following code:

#![allow(dead_code)]

fn f    (x: &mut i32) -> &mut i32 { x }
fn id<T>(x: T       ) -> T        { x }

fn main() {
    let mut x = 10;

let m = &mut x;

@jimblandy
jimblandy / gen.rs
Created December 17, 2016 17:57
Simulating generators in Rust
//! Generators in Rust, simulated at great expense using threads
//!
//! Here's a generator which yields the numbers from one to three:
//!
//! let gen = generate(|(), out| {
//! for i in 1..4 {
//! out.yield_(i);
//! }
//! });
//!
@jimblandy
jimblandy / barriers.h
Created September 21, 2016 01:23
Header file from Minor Scheme for memory barriers
/* barriers.h --- interface to memory and code translation barriers */
#ifndef MN__GC_BARRIERS_H
#define MN__GC_BARRIERS_H
/* The following two functions provide the memory-coherence behavior
of mutexes, but without any blocking or communication.
On many modern multi-threaded systems, one thread may see writes to
@jimblandy
jimblandy / gist:7028a3acce8453944ec285f667293b4f
Created July 27, 2016 05:29
fn t(n: usize) -> usize { (0..n+1).fold(0, |s, i| s+i*i*i) }
5828: 49 83 fa ff cmp $0xffffffffffffffff,%r10
582c: 74 5d je 588b <_ZN8triangle4main17h1e5682daf508becbE+0x11b>
582e: 49 8d 42 ff lea -0x1(%r10),%rax
5832: 49 f7 e2 mul %r10
5835: 49 89 c0 mov %rax,%r8
5838: 49 89 d1 mov %rdx,%r9
583b: 49 8d 72 fe lea -0x2(%r10),%rsi
583f: 48 f7 e6 mul %rsi
5842: 48 89 c7 mov %rax,%rdi
5845: 48 89 d3 mov %rdx,%rbx
@jimblandy
jimblandy / gist:f3a60f2ea1e5cc20ca4fa8965e50d186
Created July 27, 2016 05:20
fn t(n: usize) -> usize { (0..n+1).fold(0, |s, i| s+i*i) }
5830: 48 8d 47 ff lea -0x1(%rdi),%rax
5834: 48 f7 e7 mul %rdi
5837: 48 89 c1 mov %rax,%rcx
583a: 48 89 d6 mov %rdx,%rsi
583d: 48 8d 5f fe lea -0x2(%rdi),%rbx
5841: 48 f7 e3 mul %rbx
5844: 0f af de imul %esi,%ebx
5847: 48 0f a4 ce 3f shld $0x3f,%rcx,%rsi
584c: 48 8d 0c 76 lea (%rsi,%rsi,2),%rcx
5850: 01 da add %ebx,%edx
@jimblandy
jimblandy / gist:a94d117a29d4e5031c5ae7084758f932
Created July 27, 2016 05:18
Machine code generated for triangle function
5826: xor %ecx,%ecx
5828: cmp $0xffffffffffffffff,%rsi
582c: je 5840
582e: lea -0x1(%rsi),%rax
5832: mul %rsi
5835: mov %rdx,%rcx
5838: shld $0x3f,%rax,%rcx
583d: add %rsi,%rcx
5840: ... use %rcx