Skip to content

Instantly share code, notes, and snippets.

View mythz's full-sized avatar

Demis Bellot mythz

View GitHub Profile
@mythz
mythz / NonBlockingReadBufferedStream alpha
Created January 26, 2011 02:24
Naive Example of a 1 Read + 1 Write thread buffer
/* Incomplete un-tested buffered implementation designed to accomodate 1 write thread and 1 read thread only
* Not implemented yet, but is expected to be pooled.
*/
public class NonBlockingReadBufferedStream
: Stream, IEnumerable<Action<Action<object>, Action<Exception>>>, IDisposable
{
private const int MtuAppSize = 1450;
private const int BufferAllocationSize = 32 * 1024;
internal int ResetClearsBufferOfMaxSize = 4 * 1024 * 1024; //4MB
@mythz
mythz / gist:844885
Created February 26, 2011 02:54
Refactored to working example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack;
using ServiceStack.Common;
using ServiceStack.Common.Extensions;
using ServiceStack.Common.Utils;
using ServiceStack.Redis;
using ServiceStack.Redis.Generic;
@mythz
mythz / install_nginx.sh
Created February 28, 2011 06:54
Install Nginx 0.9.5 on OSX
#!/bin/bash
## DOWNLOADS
sudo curl -OL h ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.12.tar.gz > /usr/local/src/pcre-8.12.tar.gz
sudo curl -OL h http://nginx.org/download/nginx-0.9.5.tar.gz > /usr/local/src/nginx-0.9.5.tar.gz
## Install PCRE
sudo mkdir -p /usr/local/src
cd /usr/local/src
tar xvzf pcre-8.12.tar.gz
@mythz
mythz / StringBuffer.js
Created March 4, 2011 17:31
Stand-alone version Google goog.string.StringBuffer for efficient string catenation on all browsers
var hasScriptEngine = 'ScriptEngine' in window;
var HAS_JSCRIPT = hasScriptEngine && window['ScriptEngine']() == 'JScript';
var IS_IE = HAS_JSCRIPT;
var StringBuffer = function(opt_a1, var_args) {
this.buffer_ = HAS_JSCRIPT ? [] : '';
if (opt_a1 != null) {
this.append.apply(this, arguments);
}
@mythz
mythz / FastJQueryForIE7
Created March 8, 2011 17:54
jQuery is very slow at handling poor browsers, this script added a 7.8x perf improvement
/*
* Faster jQuery DOM traversal for <= IE7.
* Usage:
* $Q() is a drop-in replacement for $(). Returns same jQuery object.
*
* //$Q = $; //Un-comment, to switch to use jQuery for benchmark comparisons
* My avg benchmarks in IE7 for traversing a 20x30 table was:
* 786ms vs 6140.67ms - Chrome can do it natively in 28ms
*
* Limitations:
@mythz
mythz / js-utils.js
Created March 8, 2011 19:00
javascript utils
var fromDtoDate = function(dateStr) {
if (!dateStr) return null;
return new Date(parseFloat(/Date\(([^)]+)\)/.exec(dateStr)[1]));
}
var formatDtoDate = function(dateObj) {
if (!dateObj) return "";
if (dateObj != typeof "date") dateObj = fromDtoDate(dateObj);
var day = dateObj.getDate(), month = dateObj.getMonth() + 1;
if (day < 10) day = "0" + day;
@mythz
mythz / cash-register-mixin.coffee
Created May 20, 2011 02:15
Rewriting Peep Code app.coffee example
# CoffeeScript With Mixins
$ ->
template = _.templateFor '#meal-template'
meal = new Meal
_.focusOn '#entry'
_.onSumbitOf '#entry_form', ->
meal.add new Dish _.valOf '#entry'
_.setHtmlOf 'ul#meal', template meal.toJSON()
@mythz
mythz / cash-register-types.coffee
Created May 20, 2011 02:45
Cash Register classes in CoffeeScript and Javascript
class Dish
constructor: (rawDescription="") ->
[all, @title, @price] = @parseRawDescription rawDescription
@price = new Money @price
parseRawDescription: (rawDescription) ->
pattern = ///
([^$]+) #Title
(\$\d+\.\d+) #Price
///
@mythz
mythz / OrmLiteAuthorSample.cs
Created June 17, 2011 01:59
Shorthand OrmLite db access with IDbConnectionFactory extension methods
IDbConnectionFactory dbFactory = new OrmLiteConnectionFactory("...",
SqlServerOrmLiteDialectProvider.Instance);
Author author = dbFactory.Exec(dbCmd => dbCmd.GetById<Author>(1));
List<Author> rows = dbFactory.Exec(dbCmd =>
dbCmd.Select<Author>("Username LIKE {0}", "%example.com"));
@mythz
mythz / weighted_sequences.coffee
Created September 2, 2011 04:21
Generate a weighted sequence of numbers with an optional multiplier
# Generate a sequence of numbers:
# - ordered with the highest values
# - has a sum of 1
# - has an optional multiplier to alter the decreasing rate
sequence = (n,m=0) ->
f = (p,multiplier) -> p * p + multiplier
s = ""