Skip to content

Instantly share code, notes, and snippets.

View ap's full-sized avatar

Aristotle Pagaltzis ap

View GitHub Profile
@ap
ap / gcd.sh
Created May 28, 2009 14:26
shell func for cd'ing to top of git working copy
# these implementations both take special care to keep `cd -` working correctly
# good way
gcd() {
local TOP="`git rev-parse --show-cdup 2> /dev/null`" || return 1
[ "$TOP" ] && cd "$TOP"
}
# old, dumb, bad way
gcd() {
" DELIMITERS MADE LESS ANNOYING
"
" Main novelty here (if it is one): this does NOT try to be helpful by
" inserting the closing delimiter for you when you type an opening one.
" Instead it only tries to be smart about what to do when you type a closing
" delimiter yourself.
"
" If you just typed an empty delimiter pair, it'll move the cursor back
" inside. If you type a closing delimiter the cursor is already on (or to
" the left of, if the cursor is on a space), it'll skip the cursor past that
@ap
ap / gist:152462
Created July 23, 2009 04:27
My first Haskell code
-- http://thedailywtf.com/Articles/Programming-Praxis-Russian-Peasant-Multiplication.aspx
foldl1 (+) . map (snd) . filter (odd . fst) . takeWhile ((0 <) . fst) $ iterate (\(x,y) -> (x`div`2, y*2)) (18,23)

Both of these sed programs transform lines containing binary addition expressions of the form

1+1111111111111

to the respective value of the expression, again in binary.

The easiest way to invoke them in shell is using here strings, like so:

./bin_add.sed <<< 1+1111111111111

./bin_add_fast.sed <<< 1+1111111111111

@ap
ap / Apple_KB_and_Initrd_Notes.mkd
Created November 6, 2009 01:25
Apple mini aluminium keyboard in Linux and Initrd Notes

Apple mini aluminium keyboard in Linux and Initrd Notes

Problem: The Fn key on my Apple mini aluminium keyboard (wired) doesn’t work in Linux.

Solution, according to long Google research: the hid-apple and usbhid modules in recent kernel versions list the combination of properties of this keyboard so the kernel can support it properly.

Problem: I don’t want to recompile my entire kernel.

Solution: In the case of usbhid, Ryan Finnie points out that the driver accepts keyboard properties configuration with an option, so I don’t need to recompile anything in the first place:

Storing/Retrieving from the File System

Some projects (the Git project itself for example) store additional files directly in the Git file system without them necessarily being a checked in file.

Let’s start off by storing a random file in Git:

$ echo "Foo" | git hash-object -w --stdin
51fc03a9bb365fae74fd2bf66517b30bf48020cb

At this point the object is in the database, but if you don’t set something up to point to that object it will be garbage collected. The easiest way is to tag it:

#!/usr/bin/perl
# original by towo <towo@ydal.de> under CC-BY
use strict;
use File::Basename qw( basename );
use File::Spec::Functions qw( catfile );
use Scalar::Utils qw( openhandle );
my %monthnum = qw(
Jan 01 Feb 02 Mar 03
use 5.010;
use strict;
use SQL::Beautify;
my $sql = do { local $/; <> };
$_ = do {
my $p = SQL::Beautify->new;
$p->query( $sql );
$p->beautify;
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
no warnings qw( once qw );
use Parse::Perl qw( current_environment parse_perl );
use PadWalker qw( peek_my peek_sub );
use Devel::LexAlias qw( lexalias );
my $empty_env = current_environment;
@ap
ap / license
Created January 27, 2010 11:22 — forked from defunkt/license
#!/bin/bash
# Usage: license [fullname]
# Prints an MIT license appropriate for totin' around.
#
# $ license > COPYING
# $ license 'Joe Random' > COPYING
#!/bin/sh
FULLNAME=${1:-$(getent passwd $(id -u) | cut -d: -f5 | cut -d, -f1)}
cat << "."
Copyright (c) `date +%Y` $FULLNAME