Skip to content

Instantly share code, notes, and snippets.

View jimblandy's full-sized avatar

Jim Blandy jimblandy

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / with-this.js
Created September 6, 2017 22:01
Demonstration of `this` value for functions found on `with` operands
let obj = { f: function() { console.log(this === obj); } };
with (obj) f();
// output: true
@jimblandy
jimblandy / SICM
Created September 22, 2017 03:39
SICM 2nd ed. 1.2
/// https://en.wikipedia.org/wiki/Euler_angles
struct EulerAngle3<R> { α: R, β: R, γ: R }
struct Point<R> { x: R, y: R, z: R }
struct Pin<R> {
/// The location of the pin's center of gravity.
center: Point<R>,
/// The angle with the positive Z axis of some distinguished point
/// on the pin that is off the pin's axis of symmetry.
orientation: EulerAngle3<R>
@jimblandy
jimblandy / cast.patch
Last active October 30, 2017 16:59
Bug 1277377: Don't permit `.append((float*) ptr)` to a `Vector<int*>` 😟 (patch from Luke)
diff --git a/mfbt/Vector.h b/mfbt/Vector.h
--- a/mfbt/Vector.h
+++ b/mfbt/Vector.h
@@ -158,17 +158,22 @@ struct VectorImpl
*/
template<typename T, size_t N, class AP>
struct VectorImpl<T, N, AP, true>
{
template<typename... Args>
MOZ_NONNULL(1)