Skip to content

Instantly share code, notes, and snippets.

View jameshulse's full-sized avatar

James Hulse jameshulse

View GitHub Profile
@jameshulse
jameshulse / gist:7035070
Created October 18, 2013 01:26
Lazy dictionary
public class LazyDictionary<TKey, TValue>
{
private readonly Func<TKey, TValue> _factory;
private readonly Dictionary<TKey, TValue> _cache;
public LazyDictionary(Func<TKey, TValue> factory)
{
_factory = factory;
_cache = new Dictionary<TKey, TValue>();
}
@jameshulse
jameshulse / MapExample.cs
Last active December 16, 2015 14:10
Example Automapper API to determine changes made from mapping
var person = new Person(name: "James", age: 27);
var update = new PersonUpdate { age = 28 };
var changes = Map.MapWithChanges(update, person); // Theoretical API
if(changes.Any())
_db.Save(person);
public class CircularBuffer<T>
{
private readonly int _size;
private readonly T[] _items;
private int _writeLocation = -1;
private readonly object _lock = new object();
public CircularBuffer(int size)
@jameshulse
jameshulse / TestDependencyResolver.cs
Last active January 8, 2016 16:07
DI() Resolver for Akka.NET Testing
public class TestDependencyResolver : IDependencyResolver
{
private readonly Dictionary<Type, Props> _registrations = new Dictionary<Type, Props>();
public void Register<T>(Props instance)
{
_registrations.Add(typeof(T), instance);
}
public Type GetType(string actorName)
public static class DeterministicGrouping
{
public static bool IsSelected(Guid entropy, ulong numerator, ulong denominator = 100)
{
if(numerator > denominator)
throw new ArgumentOutOfRangeException(nameof(numerator), "The numerator cannot be larger than the denominator.");
if (numerator <= 0)
throw new ArgumentOutOfRangeException(nameof(numerator), "The numerator must be a positive non-zero value.");
if (denominator <= 0)
throw new ArgumentOutOfRangeException(nameof(denominator), "The denominator must be a positive non-zero value.");
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="BlockInvalidHosts" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="*.justgiving.com" negate="true" />
</conditions>
@jameshulse
jameshulse / Controller.php
Last active July 17, 2017 03:10
Example of non-database bound model binding in Laravel routes
<?php
class Controller
{
function getData(Period $period)
{
return Model::where('created_at', '>', Carbon::create($period->year, $period->month, 1));
}
}
@jameshulse
jameshulse / Page.vue
Created May 30, 2018 21:05
Re-using component structure over multiple pages
<template>
<div>
<model-overview
:title="title"
:models="models"
@select="changeModel"
@add="createModel"
/>
<complex-form
@jameshulse
jameshulse / day1.rs
Created December 2, 2021 06:23
AOC 2021 day1
use itertools::Itertools;
use std::fs;
fn main() {
let lines = fs::read_to_string("input.txt").expect("Couldn't read input file.");
let values: Vec<u32> = lines
.lines()
.map(|val| val.parse::<u32>().expect("Invalid value in input file!"))
.collect();
@jameshulse
jameshulse / day3.rs
Last active December 5, 2021 04:13
Advent of Code 2021 Day 3
use indoc::indoc;
use itertools::Itertools;
fn main() {
let lines = include_str!("input");
dbg!(part_one(lines));
dbg!(part_two(lines));
}