Skip to content

Instantly share code, notes, and snippets.

View kberridge's full-sized avatar

Kevin Berridge kberridge

View GitHub Profile
@kberridge
kberridge / gist:1348711
Created November 8, 2011 18:51
Powershell: count people's lines of code
$len = (ls -r -filter *.cs | %{ hg blame -u $_.FullName } | ?{ $_ -match '^\s*claco' }).Length; "claco wrote $len lines of code"
@kberridge
kberridge / 1-Simplest.cs
Created February 5, 2012 03:39
The Cost of DI: 4 different versions of the same code, demonstrating the cost of added indirection
public class SpeakerController : Controller
{
public ActionResult Create(int presentationId, FormCollection data)
{
var presentation = Presentation.Find(presentationId);
var vm = SpeakerNewViewModel.Build(presentation);
UpdateModel(vm, data);
var presentationApi = new PresentationApi(presentationId);
@kberridge
kberridge / gist:2660500
Created May 11, 2012 15:37
razor and javascript regex
@{var anEmptyRegex = "";}
<script type="text/javascript">
var r = /@anEmptyRegex/i;
callSomeFunction(r);
</script>
-- Razor Output --
<script type="text/javascript">
@kberridge
kberridge / find-connected-graphs.fsx
Created June 3, 2012 16:07
F# solution to "find connected subgraphs in graph" exercise
type 'a Node = 'a * 'a list
type 'a Graph = 'a Node list
let g = [('a', ['b'; 'd']); ('b', ['a'; 'c'; 'd']); ('c', ['b']); ('d', ['a'; 'b']); ('e', ['f']); ('f', ['e'])]
let findConnectedGraph (map : Map<'a, 'a list>) (firstVal : 'a) =
let rec walk (seen : 'a list) (tosearch : 'a list) =
let isSeen n =
seen |> List.exists (fun i -> i=n)
let notSeenOnly (nodes : 'a list) =
nodes |> List.filter (fun i -> not <| isSeen i)
@kberridge
kberridge / word-ladder.fsx
Created June 10, 2012 01:03
F# solution to the word ladder problem
let words = System.IO.File.ReadAllLines("four-char-dictionary.txt")
let isWordInDict word =
words |> Seq.exists (fun w -> w = word)
let filterToDict words =
words |> List.filter isWordInDict
let filterNotSeen (seen : Set<string>) words =
words |> List.filter (fun w -> not(seen.Contains(w)))
@kberridge
kberridge / canopy-dynamic-load-contexts.fs
Created November 30, 2012 18:01
canopy: dynamically find and initialize contexts in all files in a project
// ContextAttribute.fs
module ContextAttribute
type ContextAttribute() =
inherit System.Attribute()
// example_test_file.fs
module example_test_file
open canopy
@kberridge
kberridge / nuget-restore.ps1
Created December 14, 2012 15:25
This powershell script performs the same function as VS's nuget package restore, but it does so without modifying the project files. So shared projects can still be shared between different solutions with different file layouts (or even some doing package restores and some not). The downside is it doesn't integrate with msbuild, so you have to r…
$packageConfigs = dir -r -include packages.config -exclude **\.hg\** | %{ $_.FullName }
$packageConfigs | %{ nuget.exe install $_ }
@kberridge
kberridge / task.cs
Created December 17, 2012 13:31
How would you slice out the concerns in this object, and what would you name the resulting objects?
public class Task : ActiveRecord
{
public string Name { get; set; }
public int AssignedTo_UserId { get; set; }
public DateTime DueOn { get; set; }
public Task()
{
DueOn = DateTime.Now.AddDays(1);
}
@kberridge
kberridge / ElementScopeExtensions.cs
Created October 22, 2013 16:07
Coypu HasValue extension
public static bool HasValue(this ElementScope scope, string text, Options options = null)
{
var f = scope.GetType().GetField("options", BindingFlags.Instance | BindingFlags.NonPublic);
var config = (Options)f.GetValue(scope);
var robustly = new RetryUntilTimeoutRobustWrapper();
return robustly.Robustly(new HasValueQuery(scope, text, options ?? config));
}
class HasValueQuery : Query<bool>
{
public interface ICommand
{
}