Skip to content

Instantly share code, notes, and snippets.

@metamatt
metamatt / risk_dice.py
Last active August 7, 2022 23:23
Calculate odds of single Risk battle
#! /usr/bin/env python3
#
# Purpose: calculate the odds
# usage: risk_dice.py [ number of attacking armies ] [ number of defending armies ]
# eg: risk_dice.py 3 2
#
# I first tried looking this up to see what others had found: https://www.google.com/search?q=risk+dice+odds
# The first 3 Google results have answers which disagree substantially:
# 1) https://www.reddit.com/r/theydidthemath/comments/2h224y/comment/ckottiv/ has an analytic approach
# which doesn't seem to apply the actual game rules
@metamatt
metamatt / QBird.js
Created June 11, 2015 23:00
Replace Angular $q with Bluebird
app.config(['$qProvider', function($qProvider) {
// Tell Angular to create Bluebird promises instead of $q promises.
$qProvider.$get = function() {
function QBird(resolver) {
return new Promise(resolver);
}
QBird.defer = function() {
var deferred = {};
deferred.promise = new Promise(function(resolve, reject) {
@metamatt
metamatt / gist:c53e6b80bb3d0c89f9a2
Created March 3, 2015 01:53
In NodeJS 0.10 (V8 3.14), V8 Error.stack getter replaces itself on the first call. In NodeJS 0.10 (V8 3.28), it does not. How to tell if it's already been called?
matt@matt-dev ~> ~/node-binaries/nodejs-0.10.33
> e = new Error()
[Error]
> Object.getOwnPropertyDescriptor(e, 'stack')
{ get: [Function],
set: [Function],
enumerable: false,
configurable: true }
> e.stack
'Error\n at repl:1:6\n at REPLServer.self.eval (repl.js:110:21)\n at Interface.<anonymous> (repl.js:239:12)\n at Interface.emit (events.js:95:17)\n at Interface._onLine (readline.js:202:10)\n at Interface._line (readline.js:531:8)\n at Interface._ttyWrite (readline.js:760:14)\n at ReadStream.onkeypress (readline.js:99:10)\n at ReadStream.emit (events.js:98:17)\n at emitKey (readline.js:1095:12)'
@metamatt
metamatt / node-gyp-output
Created February 27, 2015 00:33
node-webkit-agent compile errors in node 0.12.0 after nan patch
matt@matt-dev ~/s/n/node-webkit-agent> node-gyp rebuild
gyp info it worked if it ends with ok
gyp info using node-gyp@1.0.2
gyp info using node@0.12.0 | linux | x64
child_process: customFds option is deprecated, use stdio instead.
gyp info spawn python
gyp info spawn args [ '/usr/lib/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args 'binding.gyp',
gyp info spawn args '-f',
gyp info spawn args 'make',
@metamatt
metamatt / README.md
Created February 18, 2015 17:08
Demo for NodeJS preemption bugs via node::MakeCallback.

Demo for NodeJS preemption bugs via node::MakeCallback.

Usage:

  • node 0.12.0 should be on your PATH
  • grab all the files from this gist into the same directory
  • node-gyp rebuild to compile preemptor.cc into build/Release/preemptor.node
  • node demo to run the demo

For me, the demo output is

@metamatt
metamatt / keybase.md
Created February 17, 2015 07:14
Keybase proof of identity

Keybase proof

I hereby claim:

  • I am metamatt on github.
  • I am metamatt (https://keybase.io/metamatt) on keybase.
  • I have a public key whose fingerprint is 6EBF C4F0 19C0 122A 168F 600E 0452 043E 5FC8 7E11

To claim this, I am signing this object:

@metamatt
metamatt / promise-chain.js
Created February 3, 2015 19:07
promise chaining in Q and angular $q
// find Q/$q implementation
var Q;
try {
$injector = angular.injector([ 'ng' ]);
Q = $injector.get('$q');
} catch (err) {
Q = require('q');
}
// helper function to attach handlers that show how promise is resolved
@metamatt
metamatt / debug node fails.md
Last active August 29, 2015 14:11
reproducible v8 fatal error in debug version of nodejs 0.10.33
  1. Check out node 0.10.33 source
  2. ./configure --without-snapshot --debug
  3. make
  4. copy/symlink out/Debug/node so it's the node binary on your path
  5. create a package.json with the following contents
{
  "dependencies": {
    "archiver": "0.13",
@metamatt
metamatt / Debug vs Release.md
Last active August 29, 2015 14:11
Be careful when building Node.js from source and asking for both debug and release versions.

If you build Node.js from source and want to try both the Debug and Relase configurations, and you notice that if you do

% ./configure --without-snapshot --debug
% make

you get both Debug and Release versions, and you think you can use them normally, think again:

@metamatt
metamatt / bigStrings.js
Created November 13, 2014 19:54
v8 does not intern strings that are identical, at least not if they're big. You can see distinct string objects in the heap dump.
require('webkit-devtools-agent')
b1 = fs.readFileSync('/usr/bin/nodejs')
b2 = fs.readFileSync('/usr/bin/nodejs')
c1 = b1.toString(); 0
c2 = b2.toString(); 0
c1 === c2 // true!