Skip to content

Instantly share code, notes, and snippets.

View adam-singer's full-sized avatar
👾

Adam Singer adam-singer

👾
View GitHub Profile
@adam-singer
adam-singer / encoder.dart
Created April 25, 2012 03:05
encoder for redis with dart
encode(cmd, [List args=null]) {
StringBuffer sb = new StringBuffer();
sb.add("*${args.length+1}\r\n\$${cmd.length}\r\n${cmd}\r\n");
if (args is List) {
args.forEach((parameter) {
sb.add("\$${parameter.length}\r\n${parameter}\r\n");
});
}
Utils.getLogger().debug("encode = ${sb.toString()}");
@adam-singer
adam-singer / decoder.dart
Created April 25, 2012 04:24
redis dart decoder
#import("../lib/redis.dart");
#import("dart:utf");
void main() {
String okStr = "+OK";
String getStr = "\$16\r\nmy spaced string\r\n";
String listStr = "*4\r\n\$5\r\nWorld\r\n\$5\r\nHello\r\n\$3\r\nbar\r\n\$3\r\nfoo\r\n";
Utils.setVerboseState();
Decoder decoder = new Decoder(listStr);
var i = decoder.decode();
class MyClass{
static MyClass _ref;
static MyClass get context() => _ref == null ? new MyClass() : _ref;
factory MyClass(){
if (_ref != null) return _ref;
_ref = new MyClass._internal();
@adam-singer
adam-singer / FileHandler.dart
Created May 1, 2012 02:34 — forked from iggymacd/FileHandler.dart
Sample FileHandler
class FileHandler {
FileHandler(){
}
void onRequest(HttpRequest request, HttpResponse response, [String fileName = null]){
final int BUFFER_SIZE = 4096;
if (fileName == null) {
fileName = request.path.substring(1);
}
@adam-singer
adam-singer / NotFoundHandler.dart
Created May 1, 2012 02:34 — forked from iggymacd/NotFoundHandler.dart
Sample NotFound Handler
class NotFoundHandler {
NotFoundHandler(){
}
List<int> _notFoundPage;
static final String notFoundPageHtml = """
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
@adam-singer
adam-singer / symlinkbug.dart
Created May 13, 2012 02:05
Bug with DirectoryLister not able to handle broken symbolic links
#import('dart:io');
main() {
/*
Create a bad symlink and run 'dart symlinkbug.dart'
ln -s /usr/local/doesnotexist badlink
*/
var files = [];
var dirs = [];
var d = new Directory("/tmp");
@adam-singer
adam-singer / test_isolate.dart
Created May 20, 2012 03:33
mongo dart bug in isolate. Illegal instruction: 4
#import("dart:isolate");
#import('../../mongo-dart/lib/mongo.dart');
/*
1) Clone the mongo dart project and reference the correct import location
2) dart test_isolate.dart
13:37:00-adam@Adams-MacBook-Air:~/dart/test
$ dart test_isolate.dart
enter find
Illegal instruction: 4
@adam-singer
adam-singer / dartVisitorDesignPattern.dart
Created June 26, 2012 03:41
Basic Visitor Pattern Example
// Reference: http://en.wikipedia.org/wiki/Visitor_pattern
interface CarElementVisitor {
void visitWheel(Wheel wheel);
void visitEngine(Engine engine);
void visitBody(Body body);
void visitCar(Car car);
}
interface CarElement {
void accept(CarElementVisitor visitor); // CarElements have to provide accept().
@adam-singer
adam-singer / test.dart
Created July 3, 2012 02:33
Sample async split of control structure for futures
http://goo.gl/xDA15
I don't like nesting either, but I think trying to hide your flow control by writing a branching set of logic as a 'pipeline' is a mistake.
I would refactor into methods, like this (again, guessing at what you're trying to do):
main() => getEntries("wibble.txt");
/// Reads the files listed in [filename] and returns their contents as strings
/// or null if [filename] doesn't exist
Future<List<String>> getEntries(String filename) {
final file = new File(filename);
@adam-singer
adam-singer / fannkuchredux.dart
Created July 6, 2012 03:47 — forked from gozzoo/fannkuchredux.dart
fannkuchredux benchmark implementation in dart
#import('dart:core');
class FannkuchreduxTest {
static int fannkuchredux(int n) {
var perm = new List(n);
var perm1 = new List(n);
var count = new List(n);
int maxFlipsCount = 0;
int permCount = 0;