Skip to content

Instantly share code, notes, and snippets.

View munificent's full-sized avatar

Bob Nystrom munificent

View GitHub Profile
// Function parameter types are in a "contravariant position", so allowing this
// override would break soundness. Consider:
class Base {
final String Function(dynamic val) validator;
}
class Derived extends Base {
String Function(String val) validator;
}
@munificent
munificent / dump_map.c
Created July 27, 2019 20:04
Dump a PPM image of a Wren map's hash buckets
// Generates a PPM image showing the entries in a Wren Map.
//
// Each pixel represents an entry. It will be black if the entry is empty is
// empty. Otherwise, it will be some other color. The red and green channels
// represents how far the key in that entry is from where it would ideally be.
// In other words, how many collisions occurred before it could be inserted.
// Brighter colors are worse. In particular, brighter green colors are much
// worse.
//
// A good hashing function will lead to an image containing a random-looking
@munificent
munificent / hidden_forward.md
Last active February 21, 2023 11:16
The "hidden forwarders" pattern in Dart

Say you have a package my_stuff. It contains two classes, A and B. You want to store them in separate files, but A needs access to some private functionality of B (in this case, the method _secret() and the constructor B._magic()). You don't want to expose that functionality outside of the package. You can express that like this:

The app using your package does:

some_app.dart:

Here is the relevant part of the log, the error seems to occur when trying to format the directory 'dart_style':
[1528/1570] ACTION //utils/dartfmt:dartfmt(//build/toolchain/linux:clang_x64)
FAILED: gen/dartfmt.dart.snapshot
python ../../build/gn_run_binary.py compiled_action dart --deterministic --packages=/dart-sdk/sdk/.packages --snapshot=gen/dartfmt.dart.snapshot --snapshot-depfile=/dart-sdk/sdk/out/ReleaseX64/gen/dartfmt.dart.snapshot.d --snapshot-kind=app-jit /dart-sdk/sdk/third_party/pkg_tested/dart_style/bin/format.dart ../../third_party/pkg_tested/dart_style
Command failed: ./dart --deterministic --packages=/dart-sdk/sdk/.packages --snapshot=gen/dartfmt.dart.snapshot --snapshot-depfile=/dart-sdk/sdk/out/ReleaseX64/gen/dartfmt.dart.snapshot.d --snapshot-kind=app-jit /dart-sdk/sdk/third_party/pkg_tested/dart_style/bin/format.dart ../../third_party/pkg_tested/dart_style
output: Formatting directory ../../third_party/pkg_tested/dart_style:
After which it prints the file it could not format/ Full list
@munificent
munificent / required-signatures.txt
Created March 30, 2019 00:11
Histogram of the various parameter signatures and how they mix required and optional named parameters
--- Signatures (869 total) ---
88 ( 10.127%): -@@@@@@
81 ( 9.321%): @
73 ( 8.400%): @@
41 ( 4.718%): @-
37 ( 4.258%): -@
34 ( 3.913%): -@@
34 ( 3.913%): @@@
26 ( 2.992%): -@-
20 ( 2.301%): @@-
#include <stdio.h>
#include <stdlib.h>
const int H = 40;
const int W = 80;
char map[H][W];
int rnd(int max) {
return rand() % max;
@munificent
munificent / generate.c
Last active March 18, 2024 08:31
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
class Foo extends A {
Foo({
Bar bar,
Baz baz,
}) : assert(bar != null),
super(baz);
}
@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;
}
}
@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"