Skip to content

Instantly share code, notes, and snippets.

View munificent's full-sized avatar

Bob Nystrom munificent

View GitHub Profile
@munificent
munificent / randoms.wren
Created February 9, 2016 15:21
Given samplePick and sampleRes, which are the two random sampling algorithms, determines which is fastest for a given number of samples and collections
import "random" for Random
var TRIALS = 10
var RANDOM = Random.new()
class FindCutoffs {
// Determine the time ratio between picking and reservoir sampling [samples]
// from [list].
static calculateRatio(list, samples) {
System.gc()
@munificent
munificent / function_types.dart
Last active October 20, 2016 23:20
Using `(params) -> return` syntax.
// Uses in typedefs.
typedef CompilerInputProvider = Uri->Future/*<String | List<int>>*/;
typedef ReadStringFromUri = Uri->Future<String>;
typedef CompilerOutputProvider = (String name, String extension) -> EventSink<String>;
typedef DiagnosticHandler =
(Uri, int begin, int end, String message, Diagnostic kind) -> void;
@munificent
munificent / function_types2.dart
Last active October 20, 2016 23:20
Using `(params -> return)` syntax.
// Uses in typedefs.
typedef CompilerInputProvider = (Uri -> Future/*<String | List<int>>*/);
typedef ReadStringFromUri = (Uri -> Future<String>);
typedef CompilerOutputProvider = (String name, String extension -> EventSink<String>);
typedef DiagnosticHandler =
(Uri, int begin, int end, String message, Diagnostic kind -> void);
@munificent
munificent / function_types3.dart
Created October 20, 2016 23:20
Using `return (params)` syntax
// Uses in typedefs.
typedef CompilerInputProvider = Future/*<String | List<int>>*/ (Uri);
typedef ReadStringFromUri = Future<String> (Uri);
typedef CompilerOutputProvider = EventSink<String> (String name, String extension);
typedef DiagnosticHandler =
void (Uri, int begin, int end, String message, Diagnostic kind);
@munificent
munificent / function_types4.dart
Created October 20, 2016 23:20
Using `return func(params)` syntax.
// Uses in typedefs.
typedef CompilerInputProvider = Future/*<String | List<int>>*/ func(Uri);
typedef ReadStringFromUri = Future<String> func(Uri);
typedef CompilerOutputProvider = EventSink<String> func(String name, String extension);
typedef DiagnosticHandler =
void func(Uri, int begin, int end, String message, Diagnostic kind);
@munificent
munificent / boxes.md
Created February 19, 2018 19:22
Bin-packing-esque

This is in response to: https://gist.github.com/derek-knox/2aee55a146276b0514581f36b36b3f54

Here's some thoughts:

1.

As long as even distribution is a soft goal and not a hard constraint, and you've got that specific set of small box sizes, it seems pretty tractable. It would be harder if your box sizes were weird and large like 4x19, etc. But with 1x1 in there, you can always find some solution that fills the region even if it ends up using more 1x1 boxes than you want.

There are probably a number of algorithms that would work. With those specific box sizes, I might try:

@munificent
munificent / import_syntax.md
Last active October 24, 2023 18:46
Thoughts in a relative and logical import syntax for Wren

So we need some syntax to distinguish between a relative import and a logical import. I'm not sure which way to go, and I'd like some feedback (or possibly other alternate ideas I haven't considered).

My two favorites are:

// Use
use "relative/path"
import "logical/path"
@munificent
munificent / covariance_wat.dart
Created July 12, 2018 17:29
Function-typed fields and covariant generics
class Foo<T> {
Function(T) callback;
T field;
add(T thing) {
T local = thing;
}
}
class Foo extends A {
Foo({
Bar bar,
Baz baz,
}) : assert(bar != null),
super(baz);
}
@munificent
munificent / generate.c
Last active August 3, 2026 10:19
A random dungeon generator that fits on a business card
#include <time.h> // Robert Nystrom
#include <stdio.h> // @munificentbob
#include <stdlib.h> // for Ginny
#define r return // 2008-2019
#define l(a, b, c, d) for (i y=a;y\
<b; y++) for (int x = c; x < d; x++)
typedef int i;const i H=40;const i W
=80;i m[40][80];i g(i x){r rand()%x;
}void cave(i s){i w=g(10)+5;i h=g(6)
+3;i t=g(W-w-2)+1;i u=g(H-h-2)+1;l(u