Skip to content

Instantly share code, notes, and snippets.

View pcdavid's full-sized avatar

Pierre-Charles David pcdavid

View GitHub Profile
@pcdavid
pcdavid / Optional.java
Created June 3, 2009 07:14
Optional.java
public class Optional<T> {
private final boolean isSet;
private final T value;
public static <T> Optional<T> nothing() {
return new Optional<T>(false, null);
}
public static <T> Optional<T> value(T value) {
return new Optional<T>(true, value);
#include <string.h>
int main() { char *self = "\"#include <string.h>\nint main() { char *self=%s; char *format = strdup(self+1); format[strlen(format)-2] = 0; print(format, self); return 0;}\""; char *format = strdup(self+1); format[strlen(format)-2] = 0; printf(format, self); return 0;}
#!/usr/bin/env ruby
# From "On the Criteria To Be Used in Decomposing Systems into
# Modules", David L. Parnas, in CACM 15.12, p. 1053-1058, 1972:
#
# "The KWIC index system accepts an ordered set of lines, each line is
# an ordered set of words, and each word is an ordered set of
# characters. Any line may be "circularly shifted" by repeatedly
# removing the first word and appending it at the end of the line. The
# KWIC index system outputs a listing of all circular shifts of all
@pcdavid
pcdavid / gist:149546
Created July 18, 2009 12:57
numerote.rb
#!/usr/bin/env ruby
########################################
# numerote.rb
# Author: Pierre-Charles David ([email protected])
# Version: 0.1
# Depends on: nothing (except Ruby)
# License: General Public License (GPL, see http://www.gnu.org/gpl)
# A very simple Ruby script useful to rename a set of files
@pcdavid
pcdavid / gist:149547
Created July 18, 2009 12:57
rand-lines.rb
#!/usr/bin/env ruby
print ARGF.readlines.sort_by { rand }
@pcdavid
pcdavid / gist:149548
Created July 18, 2009 12:57
scrmable.rb
#!/usr/bin/env ruby
class String
def scrmable
return self if not self =~ /\w/m
first, *letters = self.split(//)
last = letters.pop
if last
order = Hash.new
letters.each do |l|
@pcdavid
pcdavid / gist:149549
Created July 18, 2009 12:57
texml.rb
#!/usr/bin/env ruby
########################################
# texml.rb
# Author: Pierre-Charles David ([email protected])
# Version: 0.4
# Depends on: xmlparser (available on RAA)
# License: General Public License (GPL, see http://www.gnu.org/gpl)
# Based of Douglas Lovell's paper:
@pcdavid
pcdavid / gist:149550
Created July 18, 2009 12:58
JavaArraysBreakTypeSafety.java
/**
* Illustrates a limitation of the Java type system when using arrays.
* The code below compiles without warnings but fails at runtime.
*/
public class JavaArraysBreakTypeSafety {
public static void main(String[] args) {
Object[] t = new String[1];
t[0] = new Integer(1);
}
}
@pcdavid
pcdavid / gist:149551
Created July 18, 2009 12:58
GetEnv.java
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Simple example of how to access environment variables in pure Java on Linux in pre-1.5 Java.
* Since Java 1.5, System.getenv() can do this in a more portable way.
class Module
def deprecate(*methodNames)
methodNames.each do |methodName|
module_eval <<END
alias_method :deprecated_#{methodName}, :#{methodName}
def #{methodName}(*args, &block)
$stderr.puts "Warning: calling deprecated method #{self}.#{methodName}"
return deprecated_#{methodName}(*args, &block)
end
END