Skip to content

Instantly share code, notes, and snippets.

@nelhage
nelhage / README.md
Created April 26, 2018 03:14
Experience report: Trying to map an `FnMut` over a binary tree in Rust

This story is simplified from an attempt to write an AST walker for a toy compiler, but the essential facts are unchanged.

I am fairly new to Rust (but an experienced C++ programmer) and am trying to write a program to map an FnMut over a binary tree. I start with a simple tree definition, and try to write what seems to me to be the straightforward code:

use std::rc::Rc;
@nelhage
nelhage / cxx-move-semantics.cc
Created November 27, 2017 18:22
Which of these compile?
#include <memory>
using namespace std;
void accept_val(unique_ptr<int> i);
void accept_ref(unique_ptr<int> &i);
void accept_cref(const unique_ptr<int> &i);
void accept_rref(unique_ptr<int> &&i);
int main(int argc, char **argv) {
@nelhage
nelhage / acha_senate.txt
Created June 12, 2017 20:45
AHCA Senate Call Script
My name is Nelson Elhage. I'm a constituent of the Senator's in zip
code 94102.
I’m calling to express deep concern that Senate Republicans are
threatening to call for a vote on the ACA repeal without any public
hearings, and are keeping the text of the bill hidden from the public.
I urge the Senator to use every procedural tool at her disposal to
stop business as usual in the Senate and prevent this bill from
passing. I urge her to withhold consent when the bill comes to the
$ ruby -e 'puts ("".split(" ")).inspect'
[]
$ perl -MData::Dumper -E 'say Dumper([split(" ", "")])'
$VAR1 = [];
$ python -c 'print "".split(" ")'
['']
# https://play.golang.org/p/qErK71KCsF
BootCurrent: 0001
Timeout: 2 seconds
BootOrder: 0001,0017,0000,0018,0019,001A,001B,001C,0021
Boot0000* Windows Boot Manager HD(1,GPT,d72d73a1-3247-41ca-af0b-cd5af1642048,0x800,0x100000)/File(\EFI\Microsoft\Boot\bootmgfw.efi)WINDOWS.........x...B.C.D.O.B.J.E.C.T.=.{.9.d.e.a.8.6.2.c.-.5.c.d.d.-.4.e.7.0.-.a.c.c.1.-.f.3.2.b.3.4.4.d.4.7.9.5.}....................
Boot0001* ubuntu HD(1,GPT,d72d73a1-3247-41ca-af0b-cd5af1642048,0x800,0x100000)/File(\EFI\ubuntu\grubx64.efi)
Boot0010 Setup FvFile(721c8b66-426c-4e86-8e99-3457c46ab0b9)
Boot0011 Boot Menu FvFile(126a762d-5758-4fca-8531-201a7f57f850)
Boot0012 Diagnostic Splash Screen FvFile(a7d8d9a6-6ab0-4aeb-ad9d-163e59a7a380)
Boot0013 Lenovo Diagnostics FvFile(3f7e615b-0d45-4f80-88dc-26b234958560)
Boot0014 Startup Interrupt Menu FvFile(f46ee6f4-4785-43a3-923d-7f786c3c8479)
use std::collections::hash_set::HashSet;
use std::vec::Vec;
use std::time::{Instant,Duration};
use std::env;
const SAMPLE_INTERVAL : usize = 1000;
fn duration_ns(d : Duration) -> i64 {
return (d.as_secs() as i64) * 1000000000 + (d.subsec_nanos() as i64);
}

honey-go feedback

  • Having to initialize it globally is weird. Having an implicit global client would be fine, but it's much more idiomatic for Go libraries to be able to launch your own client that maintains state completely independently for a while. That tripped me up at first – I tried to instantiate a Builder without init'ing globally, and it didn't work.
  • relatedly, I expected NewBuilder should take a Config so I could configure it without having to mutate separately. It's unclear from the docs whether I have to call NewBuilder or whether &Builder{} is a valid Builder
  • The implicit global keys and func()s is convenient but also scares me a little because ew global state. An interesting compromise might be to have
 func AddContextField(key string, value func(context.Context)interface{})
 func (b *Builder) NewEventContext(context.Context) *Event
[white to play]
5. [B] [B] [B] [W] [W]
4. [] [] [B] [W] [B]
3. [] [] [] [W B] []
2. [] [] [] [W B W] []
1. [W] [] [] [BC] []
a. b. c. d. e.
stones: W:14 B:14
2016/06/02 07:26:43 their-move game-id=44170 ply=21 ptn=11.B move="Sa1"
2016/06/02 07:26:43 seed=1464852403
2016/06/02 07:26:43 [minimax] deepen: depth=1 val=1495 pv=[5b1>14] time=116.247µs total=116.736µs evaluated=71 tt=0 branch=71
2016/06/02 07:26:43 [minimax] stats: visited=1 scout=0 evaluated=71 null=0/0 cut=0 cut0=0(0.00) cut1=0(0.00) m/cut=0.00 m/ms=619.370822 all=0 research=2
2016/06/02 07:26:43 [minimax] deepen: depth=2 val=1475 pv=[5b1>14 c2] time=214.212µs total=381.572µs evaluated=109 tt=1 branch=1
2016/06/02 07:26:43 [minimax] stats: visited=70 scout=68 evaluated=109 null=0/0 cut=67 cut0=67(0.99) cut1=0(0.99) m/cut=0.00 m/ms=835.620787 all=0 research=2
2016/06/02 07:26:43 [minimax] deepen: depth=3 val=1575 pv=[5b1>14 c2 e1] time=4.828232ms total=5.239369ms evaluated=4445 tt=9 branch=40
2016/06/02 07:26:43 [minimax] stats: visited=184 scout=181 evaluated=4445 null=0/0 cut=105 cut0=83(0.78) cut1=14(0.92) m/cut=9.67 m/ms=958.736034 all=67 research=2
2016/06/02 07:26:43 [minimax] deepen: depth=4 v
2016/05/04 10:46:26 < GameList Add Game#309 TakticianBot vs onewhohides, 5x5, 1200, 0 half-moves played, TakticianBot to move
2016/05/04 10:46:26 > Observe 309
2016/05/04 10:46:26 < Observe Game#309 TakticianBot vs onewhohides, 5x5, 1200, 1 half-moves played, onewhohides to move
2016/05/04 10:46:26 < Game#309 P A1
2016/05/04 10:46:26 < Game#309 Time 1200 -2216321
2016/05/04 10:46:26 < Game#309 P A1
2016/05/04 10:46:26 < Game#309 Time 1200 -2216321
2016/05/04 10:46:26 < GameList Remove Game#309 TakticianBot vs onewhohides, 5x5, 1200, 1 half-moves played, onewhohides to move
2016/05/04 10:46:26 < Game#309 Over 1-0
2016/05/04 10:46:26 < GameList Remove Game#309 TakticianBot vs onewhohides, 5x5, 1200, 1 half-moves played, onewhohides to move