Skip to content

Instantly share code, notes, and snippets.

View jasonporritt's full-sized avatar

Jason Porritt jasonporritt

View GitHub Profile
@jasonporritt
jasonporritt / memoization_no_input.cs
Created October 27, 2011 13:56
Memoization in C#: No Input
public static Func<TReturn> Memoize<TReturn>(Func<TReturn> func)
{
object cache = null;
return () =>
{
if (cache == null)
cache = func();
return (TReturn)cache;
};
}
@jasonporritt
jasonporritt / Generics.cs
Created August 26, 2011 11:34
Generics in C#
using System;
using System.Collections.Generic;
using NUnit.Framework;
namespace GoodParts
{
[TestFixture]
public class GenericsTest
{
[Test]
@jasonporritt
jasonporritt / Lambdas.cs
Created August 26, 2011 11:30
A few C# lambdas
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
namespace GoodParts
{
class Lambdas
{
@jasonporritt
jasonporritt / FirstOr404.cs
Created August 26, 2011 11:11
First or 404
using System.Linq;
public static class EnumerableUtils {
// First item, or throw 404 exception
public static TSource FirstOr404<TSource>(this IEnumerable<TSource> source)
{
try
{
return source.First();
@jasonporritt
jasonporritt / coffeescript_subclass.coffee
Created May 6, 2011 20:46
CoffeeScript declaration of Backbone.View subclass
class window.TodoView extends Backbone.View
tagName: "li"
template: _.template($('#item-template').html())
events:
"click .check" : "toggleDone"
"dblclick div.todo-content" : "edit"
"click span.todo-destroy" : "clear"
"keypress .todo-input" : "updateOnEnter"
@jasonporritt
jasonporritt / backbone_class.coffee
Created May 6, 2011 20:45
Backbone class declaration
window.TodoView = Backbone.View.extend
tagName: "li"
template: _.template($('#item-template').html())
events:
"click .check" : "toggleDone",
"dblclick div.todo-content" : "edit",
"click span.todo-destroy" : "clear",
"keypress .todo-input" : "updateOnEnter"
@jasonporritt
jasonporritt / best_todos.coffee
Created May 6, 2011 12:33
Use @vars -- translation of Backbone.js example app
initialize: ->
@model.bind('change', this.render)
@model.view = this
render: =>
$(@el).html(this.template(@model.toJSON()))
this.setContent()
return this
@jasonporritt
jasonporritt / better_todos.coffee
Created May 6, 2011 12:32
Use the fat arrow -- translation of Backbone.js example app
initialize: ->
this.model.bind('change', this.render)
this.model.view = this
render: =>
$(this.el).html(this.template(this.model.toJSON()))
this.setContent()
return this
@jasonporritt
jasonporritt / naive_todos.coffee
Created May 6, 2011 12:31
Naive translation of Backbone.js example app
initialize: ->
_.bindAll(this, 'render', 'close')
this.model.bind('change', this.render)
this.model.view = this
render: ->
$(this.el).html(this.template(this.model.toJSON()))
this.setContent()
return this
@jasonporritt
jasonporritt / gist:676364
Created November 15, 2010 02:56
File upload testing
[When("I choose a photo to upload")]
public void UploadImageFor()
{
// Where Browser is your current Browser object
var fileUpload = Browser.FileUpload(x => x.Name == "Photo");
var assembly = Assembly.GetExecutingAssembly();
var fileStream = assembly.GetManifestResourceStream(assembly.GetName().Name + ".TestData.image.jpg");
var fileData = new byte[fileStream.Length];
fileStream.Read(fileData, 0, fileData.Length);