Skip to content

Instantly share code, notes, and snippets.

View mjdominus's full-sized avatar

Mark Jason Dominus (陶敏修) mjdominus

View GitHub Profile
@mjdominus
mjdominus / gist:1486532
Created December 16, 2011 15:44
is the working tree dirty?
# This is the function that git-rebase--interactive uses to determine if the
# working tree is clean, and to bail out if not
require_clean_work_tree () {
# test if working tree is dirty
git rev-parse --verify HEAD > /dev/null &&
git update-index --ignore-submodules --refresh &&
git diff-files --quiet --ignore-submodules &&
git diff-index --cached --quiet HEAD --ignore-submodules -- ||
die "Working tree is dirty"
}
@mjdominus
mjdominus / gist:1560515
Created January 4, 2012 15:22
git name-rev --name-only doesn't get the name of the current branch
$ git branch
master
* snonk
$ git rev-parse master snonk
1333053e19b35008e74476b1549ecfed709c3d44
1333053e19b35008e74476b1549ecfed709c3d44
$ git name-rev --name-only HEAD
master
$ git rev-parse --symbolic-full-name --abbrev-ref HEAD
snonk
@mjdominus
mjdominus / gist:1761992
Created February 7, 2012 21:14
Separating two sets of changes into two branches

Say you want to separate commits to two sets of files, and you want to do this on the branch that starts at commit start and ends at commit end, so that if there are n of these commits, you'll end up with two new branches, each with n commits, one branch with just to the changes in the files in group X and the other with just the changes to the files in group Y.

First, we will create branch branch-X, for just the changes to files in group X:

git checkout -b branch-X start^
for commit in $(git rev-list --reverse start..endpoint); do
  git checkout $commit -- ((list of files and directories in X))
  git commit -C $commit
done
@mjdominus
mjdominus / content.md
Created October 5, 2012 18:12 — forked from ayosec/content.md
Why Lisp macros are cool, a Perl perspective
@mjdominus
mjdominus / gist:4166459
Created November 29, 2012 02:49 — forked from arvearve/gist:4158578
Mathematics: What do grad students in math do all day?

Mathematics: What do grad students in math do all day?

by Yasha Berchenko-Kogan

A lot of math grad school is reading books and papers and trying to understand what's going on. The difficulty is that reading math is not like reading a mystery thriller, and it's not even like reading a history book or a New York Times article.

The main issue is that, by the time you get to the frontiers of math, the words to describe the concepts don't really exist yet. Communicating these ideas is a bit like trying to explain a vacuum cleaner to someone who has never seen one, except you're only allowed to use words that are four letters long or shorter.

What can you say?

@mjdominus
mjdominus / circle.pl
Created January 18, 2013 01:40
Search for permutations of objects in a circle for which any pair of objects is at a different minimal distance along the circle than originally; see http://math.stackexchange.com/questions/281094.
#!/usr/bin/perl
my $M = shift() || 15;
# A node has [ N, [1, ... ] ]
# where N is the max of the [ 1, ... ]
sub children {
my ($node) = @_;
my ($N, $circ) = @$node;
my @children;
my %kv = map {$_ => [ $args{$_}, undef ]} @fields;
my $nullcount = join " + ", map "(me.$_ IS NULL)", @fields;
my $min_nulls = $self->base_rs->search(
\%kv,
{ 'select' => [ { "min" => \$nullcount } ],
});
my $rs = $self->base_rs->search(\%kv);
@mjdominus
mjdominus / solve.pl
Created February 18, 2013 14:10
How many ways are there to add up 10 numbers, each between 1 and 12, to make a total of 70?
my @f;
sub solve {
my ($n, $x) = @_;
return 1 if $x == 0 and $n == 0;
return 0 if $x <= 0 || $n == 0;
return $f[$n][$x] if defined $f[$n][$x];
my $total = 0;
for my $i (1 .. 12) {
$total += solve($n-1, $x-$i);
@mjdominus
mjdominus / gist:5323656
Last active December 15, 2015 21:09
cron abominations
04 00 * * * /usr/local/bin/cronjob --rcpt [email protected] -c "yday=$(perl -le '@d = localtime(time - 7200); printf(q{\%04d-\%02d-\%02d}, $d[5]+1900, $d[4]+1, $d[3])'); $STARTERVIEW/bin/populate-email-open-tracking -v /vol2/log/ziprecruiter-cron/track-logs/track_log.\$yday-?? && $STARTERVIEW/bin/job_search_email_opens_by_domain"
# Riddles:
# 1. How many different quoting contexts are in this line?
# 2. How many different quote characters are there?
# 3. How deep is the deepest level of quote nesting?
# 4. Explain the difference between the first \ and the last \.
# 5. Which of the following changes would be innocuous:
# a. "..." to '...'
@mjdominus
mjdominus / with-memory-limit
Created April 11, 2013 02:22
with-memory-limit utility program
!/usr/bin/perl
use BSD::Resource qw(setrlimit RLIMIT_VMEM RLIM_INFINITY);
my ($mem) = shift // usage();
@ARGV or usage();
my ($n, $s) = $mem =~ /\A (\d+) ([kmgKMG]?) \z/x or usage();
my $suf = { k => 1024,
m => 1024 * 1024,
g => 1024 * 1024 * 1024,
};