Skip to content

Instantly share code, notes, and snippets.

View kntajus's full-sized avatar

Stephen Salt kntajus

View GitHub Profile
@kntajus
kntajus / SpeechElements
Last active August 29, 2015 13:55
Turns an integer into an array of strings that correspond to the correct wav file names to read out.
let emptyIfZero f x =
match x with
| 0 -> []
| _ -> f x
let elementsBetween0And20 = emptyIfZero (fun x -> [ string x ])
let elementsBetween21And99 x = string (x - (x % 10)) :: elementsBetween0And20 (x % 10)
let ifGreaterThan y f g x = if x > y then f x else g x
let elementsFor2DigitNumber = ifGreaterThan 20 elementsBetween21And99 elementsBetween0And20
let elementsBetween100And999 x = string (x / 100) :: "hundred" :: emptyIfZero (fun x -> "and" :: elementsFor2DigitNumber x) (x % 100)
@kntajus
kntajus / LocalStorage
Last active August 29, 2015 14:04
Used to show broken localStorage in IE11
<html>
<head>
<title>LocalStorage fundamentally broken in IE11</title>
</head>
<body>
String length: <input id="length" type="text" value="2000" /><br />
<button onclick="set(null);">Clear</button>
<button onclick="store('A');">Store w/ A</button>
<button onclick="store('B');">Store w/ B</button>
<button onclick="show();">Show value</button>
using System;
namespace Diuturnal {
[Serializable]
public class Range<T> where T : IComparable<T> {
private T _start;
private T _end;
public Range(T first, T second) {
if (first.CompareTo(second) > 0) {
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace Diuturnal {
/// <summary>
/// Represents a UK National Insurance number.
/// </summary>
/// <remarks>
/// Based on the specification found at http://www.hmrc.gov.uk/manuals/nimmanual/NIM39110.htm.
@kntajus
kntajus / CompilerFail.cs
Created January 31, 2013 13:29
Break the compiler by building this in Visual Studio 2010!
public class CompilerFail {
public int[] Numbers { set { ; } }
public int FirstNumber { get { return Numbers[0]; } }
}
@kntajus
kntajus / HttpRequest.cs
Created January 31, 2013 13:35
Manually creating an HttpRequest for testing
// Whip up a fake HttpRequest
HttpRequest httpRequest = new HttpRequest("default.aspx", "http://www.diuturnal.com/default.aspx", string.Empty);
// Have to access the Headers collection first as this creates the internal collection we're about to hack
NameValueCollection headers = httpRequest.Headers;
// Accessing Headers property above will have created the private _headers member, so now we can grab it
headers = (NameValueCollection) httpRequest.GetType().GetField("_headers", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(httpRequest);
PropertyInfo readOnlyInfo = headers.GetType().GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
@kntajus
kntajus / DoubleKeyedCollection.cs
Created January 31, 2013 13:54
Multiple keys with KeyedCollection
namespace Diuturnal.Utility {
[Serializable]
public abstract class DoubleKeyedCollection<TKey, TSecondKey, TItem>
: KeyedCollection<TKey, TItem> {
private Dictionary<TSecondKey, TKey> _secondKeyIndex =
new Dictionary<TSecondKey, TKey>();
public TItem GetItem(TSecondKey secondKey) {
@kntajus
kntajus / DirectoryHelper.cs
Created January 31, 2013 13:56
Multiple search patterns with DirectoryInfo.GetFiles()
namespace Diuturnal.Utility {
public class DirectoryHelper {
public static FileInfo[] GetFiles(DirectoryInfo directory,
params string[] searchPatterns) {
List<FileInfo> allFiles = new List<FileInfo>();
foreach (string pattern in searchPatterns) {
allFiles.AddRange(directory.GetFiles(pattern));
}
@kntajus
kntajus / Postcode.cs
Created January 31, 2013 13:39
UK Postcode Implementation
namespace Diuturnal.Utility {
[Serializable]
public struct Postcode {
private const string ValidationString =
@"^\s*(?<out>[A-Z]{1,2}[0-9R][0-9A-Z]?) *(?<in>[0-9][A-Z-[CIKMOV]]{2})\s*$";
private static readonly Regex _validator =
new Regex(ValidationString);
public static readonly Postcode Empty =
new Postcode(string.Empty, string.Empty);
private string _outCode;
@kntajus
kntajus / FormView.aspx
Created January 31, 2013 16:17
Databinding with a LoginView inside a FormView
<asp:FormView ID="fvApplication" runat="server">
<ItemTemplate>
<h1>Application Form</h1>
<!-- Lots of detail about application in here -->
<asp:LoginView ID="lvOptions" runat="server">
<RoleGroups>
<asp:RoleGroup Roles="Provider">
<ContentTemplate>
<asp:LinkButton runat="server" OnClick="Offer_Click"
Visible='<%# ApplicationStatus.Sent == (ApplicationStatus) Eval("Status") %>'>Make Offer</asp:LinkButton>