Skip to content

Instantly share code, notes, and snippets.

View gist:9542216
class Foo.<T> {
static function callIt(f : () -> T) : void {
f();
}
}
class _Main {
static function main(args : string[]) : void {
Foo.<void>.callIt(function () {});
}
@kazuho
kazuho / gist:9800525
Created Mar 27, 2014
switch from master to wip, then undo the switch
View gist:9800525
$ git status
# On branch master
nothing to commit, working directory clean
$ unco record --log=/tmp/unco.log git checkout wip
Switched to branch 'wip'
$ git status
# On branch wip
nothing to commit, working directory clean
View gist:9803136
#include <stdio.h>
static int N;
static int findN(void)
{
int min, max, mid;
// index starts from 1; search using Elias encoding (i.e. search at 1,2,4,8,16,... and then perform binary search)
for (max = 1; ; max *= 2) {
View gist:9798307
$ git status
# On branch master
nothing to commit, working directory clean
$ DYLD_INSERT_LIBRARIES=../../unco.dylib DYLD_FORCE_FLAT_NAMESPACE=YES git checkout HEAD^
Note: checking out 'HEAD^'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
View gist:9802167
static int _log_exists(const char *dir, long long log_index, int *exists)
{
struct stat st;
char fnbuf[PATH_MAX];
snprintf(fnbuf, sizeof(fnbuf), "%s/%lld", dir, log_index);
if (fstat(path, &st) == 0) {
*exists = 1;
} else if (errno == ENOENT) {
View gist:9883861
$ alias git='unco record -- git' <-- set alias
$ git status <-- now on 'master'
# On branch master
nothing to commit, working directory clean
$ git checkout wip <-- switch to 'wip'
Switched to branch 'wip'
$ git status <-- now on 'wip'
# On branch wip
nothing to commit, working directory clean
$ unco history <-- print history
@kazuho
kazuho / gist:11168660
Created Apr 22, 2014
tempfile, UNLINK, and flock
View gist:11168660
my $fn = do {
# some BSD variants (notably OS X) flocks the file handle when calling tempfile()
my ($fh, $fn) = tempfile(UNLINK => 1);
$fn
# setting UNLINK => 1 keeps a hidden reference to the file handle, and thus the file
# is not automatically closed when exitting this block
};
open my $fh, "<", $fn
or die $!;
View gist:472485cd46468bc14b3b
$ git clone git@github.com:miyagawa/cpanminus.git
Cloning into 'cpanminus'...
remote: Reusing existing pack: 8570, done.
remote: Total 8570 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (8570/8570), 3.70 MiB | 96.00 KiB/s, done.
Resolving deltas: 100% (3504/3504), done.
Checking connectivity... done.
$ cd cpanminus/
$ git branch
* devel
View nestedparams.pl
use strict;
use warnings;
use Data::Dump qw(dump);
sub parse {
my $k = shift;
my @keys;
while (1) {
@kazuho
kazuho / gist:be297f2735d836b3cd4f
Created Jun 30, 2014
#libuv - only one of the callbacks is called when a socket which is at the same time reading / writing gets closed by peer
View gist:be297f2735d836b3cd4f
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <uv.h>
static uv_tcp_t sconn, cconn;
static uv_connect_t cconn_ctx;
static uv_write_t wreq;
static uv_buf_t on_alloc(uv_handle_t *handle, size_t suggested_size)