Skip to content

Instantly share code, notes, and snippets.

@maxdeliso
maxdeliso / dispatcher.rb
Last active December 19, 2015 10:09
asynchronous dispatcher in ruby
# http://stackoverflow.com/questions/17499021/io-bound-threads-in-ruby
require 'thread'
class PoisonPill; end
def asyncDispatcher(numWorkers, stateArray, &processor)
q = Queue.new
threads = []
@maxdeliso
maxdeliso / option.scala
Last active December 17, 2015 11:50
Better, cleaner, briefer, more idiomatic use of Scala's Console
import scala.annotation.tailrec
import java.io.EOFException
import java.lang.NumberFormatException
object Sandbox extends App {
override def main(args: Array[String]) = {
print("Enter an int, and I'll compute the factorial: ")
val done = for {
@maxdeliso
maxdeliso / simpleFuture.scala
Created May 17, 2013 22:52
simple blocking future with scala
import scala.annotation.tailrec
import scala.concurrent._
import scala.concurrent.duration._
import ExecutionContext.Implicits.global
import java.io.EOFException
import java.lang.NumberFormatException
object Sandbox extends App {
@maxdeliso
maxdeliso / GCTest.java
Last active December 15, 2015 14:09
simple java garbage collector torture test
/*
* in this directory:
* to build:
* javac -Xlint:all GCTest.java
*
* to run:
* java -verbose:gc -server -Xincgc -Xmx1024m -classpath . GCTest
*
*/
@maxdeliso
maxdeliso / fptrs.c
Last active December 15, 2015 11:09
example of function pointers and safe terminal mode input in C
/*
* example of function pointers and safe terminal mode input in C
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef void (*printFunc_t) (void);
@maxdeliso
maxdeliso / nnIntRecognizer.cpp
Last active December 15, 2015 03:59
non-negative integer recognizer in C++
/*
* A practical implementation of a 'bulletproof' non-negative integer
* recognizer with overflow detection. Results are written to stdout,
* and diagnostics are written to stderr. Using redirection, you can
* easily retrieve just the recognized values separated by newlines
* like this: nnIntRecognizer <testInput >testOutput
*
* some edge cases ferreted in cooperating with Jared
*/
@maxdeliso
maxdeliso / late-binding.cpp
Last active December 14, 2015 00:39
example of late binding with c++
#include <iostream>
struct Foo {
virtual int retrieve() = 0;
};
struct Bar : public Foo {
inline int retrieve() {
return 1;
}
@maxdeliso
maxdeliso / makefile.gcc
Last active December 12, 2015 01:39
Safely apply rotation transformation to user specified strings.
CC=gcc
CFLAGS=-std=c99 -O1 -g -Wall -Wextra -pedantic
OUT=rotate
RM=rm
.PHONY: clean
rotate: rotate.c makefile
$(CC) $(CFLAGS) -o $(OUT) rotate.c
@maxdeliso
maxdeliso / restrict.txt
Created February 1, 2013 16:32
restrict in c99
WITHOUT RESTRICT:
$ echo "void frob(unsigned char* first, unsigned char* second, unsigned int count) {
for(int i = 0; i < count; ++i ) {
first[i] = second[i];
}
}" | clang -cc1 -O3 -S -x c -std=c99 -
_frob:
testl %edx, %edx
@maxdeliso
maxdeliso / loop.c
Created January 30, 2013 18:26
Singly linked list loop detection.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct sList {
struct sList * next;
int data;
};
struct sList* sList_new();