Skip to content

Instantly share code, notes, and snippets.

@chrisdavies
chrisdavies / channel_1m.go
Created February 11, 2014 15:35
A test of a million communications between channels.
import (
"fmt"
)
func main() {
num := 1000000
tots := 0
bg_chan := make(chan int, num)
fg_chan := make(chan int, num)
@chrisdavies
chrisdavies / gist:b49d3a702830c4ab6c14
Last active August 29, 2015 14:03
Better URL binding in WebAPI
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
@chrisdavies
chrisdavies / bad-markup.html
Created January 17, 2015 16:14
This markup has a problem. Can you spot it?
<!-- Notice that the quoatation marks are not consistent. They should all be
" but some are “ Note, this last one is slanted. I'm not even sure how that
one got in, but I noticed some students had those slanty quotes! That's not
valid HTML! -->
<form method=“get" action=“/save-user”>
<div class="form_div personal">
<label>Name<br><input type=“text” name=“userName” required="required"></label>
</div>
</form>
@chrisdavies
chrisdavies / theme-blue.scss
Created August 20, 2015 16:23
File-scoped variables in SASS
// A single mixin per file allows file-scoped variables
@mixin theme-blue {
// File-scoped variables
$main-bg: white;
$main-fg: #333;
$accent-bg: #00B4EF;
$accent-fg: white;
$content-width: 700px;
$gutter-width: 1rem;
@chrisdavies
chrisdavies / SparcetGroupBySample
Created February 1, 2012 22:37
Count the number of silver, gold, platinum, and unique recipients of awards in the Sparcet system.
Select
FromUserId,
Count(case when AwardType = 0 then 1 end) as silver,
Count(case when AwardType = 1 then 1 end) as gold,
Count(case when AwardType = 2 then 1 end) as plat,
Count(distinct ToUserId) as [unique],
Count(*) as total
From
Awards
Group By FromUserId;
@chrisdavies
chrisdavies / SparcetGroupBySampleMongo
Created February 1, 2012 22:43
Count the number of silver, gold, platinum, and unique recipients of awards in the Sparcet system.
function map() {
var val = {
silver: 0,
gold: 0,
platinum: 0,
total: 1,
email: this.From.Email,
unique: { },
uniqueCount: [this.To._id]
};
@chrisdavies
chrisdavies / SparcetGroupBySampleMongo.js
Created February 1, 2012 22:43
Count the number of silver, gold, platinum, and unique recipients of awards in the Sparcet system.
function map() {
var val = {
silver: 0,
gold: 0,
platinum: 0,
total: 1,
email: this.From.Email,
unique: { },
uniqueCount: [this.To._id]
};
@chrisdavies
chrisdavies / mongoupsert.cs
Created July 27, 2012 19:14
Upsert and push an item onto an array
var query = Query.And(
Query.EQ("_id", post.Id),
Query.NE("Comments._id", comment.Id));
var options = new MongoUpdateOptions {
Flags = UpdateFlags.Upsert,
SafeMode = SafeMode.False
};
var update = Update.AddToSetWrapped("Comments", comment)
@chrisdavies
chrisdavies / gist:4569299
Created January 18, 2013 22:38
In Rails (3.2.8), I needed to allow different browser tabs to behave as if they were different apps (my rails app will be embedded in an iframe in various apps). For a variety of reasons, I couldn't track apps via sub-domains. Cookies failed, because browser tabs all share the same cookies, so there were race-conditions and other buggery. My sol…
require 'addressable/uri'
module MyApp
# This rack-filter gets run before any urls are processed by the routing
# subsystem, enabling us to extract the app_id query parameter and cache it for
# the duration of the current request thread.
class ClientIdManager
def initialize(app)
@app = app
end
@chrisdavies
chrisdavies / qunit-zombie.js
Last active April 12, 2016 18:20
Gulp task for running qUnit with zombiejs
// This is what your gulp task should look like
gulp.task('qunit', function(done) {
var files = glob.sync('./test/**/*.html');
runAllQunits(files);
});
// Runs through each qunit file (one at a time, though this could be relatively easily parallelized)
function runAllQunits(testFiles) {
var browser = new Zombie();