Skip to content

Instantly share code, notes, and snippets.

View schotime's full-sized avatar

Adam Schroder schotime

  • Melbourne, Australia
View GitHub Profile
interface A<T> {
something: T
}
function call(arg: (input: A<void>) => void): () => void;
function call<T>(arg: (input: A<T>) => T): (input: T) => void {
return {} as any;
}
interface Input {
https://www.typescriptlang.org/play/index.html#src=interface%20IContext%3CT%3E%20%7B%0D%0A%09input%3A%20any%2C%0D%0A%09state%3A%20any%2C%0D%0A%09output%3A%20T%2C%0D%0A%09services%3A%20any%0D%0A%7D%0D%0A%0D%0Atype%20IRes%20%3D%20IResult%20%7C%20void%0D%0A%0D%0Ainterface%20IResult%20%7B%0D%0A%09success%3A%20((input%3A%20any)%20%3D%3E%20IRes)%2C%0D%0A%09error%3A%20((input%3A%20any)%20%3D%3E%20IRes)%0D%0A%7D%0D%0A%0D%0Aclass%20SignalBuilder%20%7B%0D%0A%09private%20signals%20%3D%20%5B%5D%3B%0D%0A%09dor%3CT%3E(func%3A%20(input%3A%20IContext%3CT%3E)%20%3D%3E%20void)%3A%20SignalBuilderResult%3CT%3E%20%7B%0D%0A%09%09this.signals.push(func)%3B%0D%0A%09%09return%20new%20SignalBuilderResult%3CT%3E(this%2C%20this.signals)%3B%0D%0A%09%7D%0D%0A%09do%3CT%3E(...func%3A%20((input%3A%20IContext%3CT%3E)%20%3D%3E%20void)%5B%5D)%3A%20SignalBuilder%20%7B%0D%0A%09%09for%20(var%20item%20of%20func)%20this.signals.push(item)%3B%0D%0A%09%09return%20this%3B%0D%0A%09%7D%0D%0A%09build()%20%7B%0D%0A%09%09return%20this.signals%3B%0D%0A%09%7D
@schotime
schotime / HStore.cs
Created August 23, 2013 02:11
PostgreSQL NPoco configuration with HStore and DateTimeOffset support
public class HStore : Dictionary<string, string>
{
private static readonly Regex QuotedString = new Regex(@"""[^""\\]*(?:\\.[^""\\]*)*""", RegexOptions.Compiled);
private static readonly Regex UnquotedString = new Regex(@"(?:\\.|[^\s,])[^\s=,\\]*(?:\\.[^\s=,\\]*|=[^,>])*", RegexOptions.Compiled);
private static readonly Regex HstoreKeyPairMatch = new Regex(string.Format(@"({0}|{1})\s*=>\s*({0}|{1})", QuotedString, UnquotedString), RegexOptions.Compiled);
private static readonly Regex KeyReg = new Regex(@"^""(.*)""$", RegexOptions.Compiled);
private static readonly Regex KeyReg2 = new Regex(@"\\(.)", RegexOptions.Compiled);
private static readonly Regex EscapeRegex = new Regex(@"[=\s,>]", RegexOptions.Compiled);
private static readonly Regex EscapeRegex2 = new Regex(@"([""\\])", RegexOptions.Compiled);
@schotime
schotime / EnumerableReader.cs
Created July 3, 2013 04:00
EnumerableReader
public class EnumerableReader : TextReader
{
private readonly char _separator;
private readonly IEnumerator<string> _enumerator;
private string _s;
private int _pos;
private readonly bool _useSeperator;
private bool _enumeratorDisposed = false;
public EnumerableReader(IEnumerable<string> lines) : this(lines, '\n')
@schotime
schotime / dbconventions.cs
Created June 13, 2013 01:04
My Postgresql conventions
var conventions = FluentMappingConfiguration.Scan(scanner =>
{
scanner.Assembly(typeof(User).Assembly);
scanner.IncludeTypes(x=>x.Namespace.StartsWith(typeof(User).Namespace));
scanner.TablesNamed(x => Inflector.MakePlural(x.Name).BreakUpCamelCaseUsing("_").ToLower());
scanner.Columns.Named(x => x.Name.BreakUpCamelCaseUsing("_").ToLower());
scanner.PrimaryKeysNamed(x => x.Name.BreakUpCamelCaseUsing("_").ToLower() + "_id");
scanner.OverrideMappingsWith(new OverrideMappings());
});
@schotime
schotime / gist:5693072
Created June 2, 2013 08:51
Override URL resolution
<img src="~/test.aspx" />
// This complies to
Response.Write("<img src=\"");
Response.Write(SiteResource("~/test.aspx"));
Response.Write("\" />");
// The problem is that the SiteResource method uses Request.ApplicationPath under the covers, but we are doing some
// URL rewriting so the URL is not the same as the ApplicationPath.
@schotime
schotime / Deadlock.sql
Last active October 26, 2017 08:43
Reproduction of Deadlock on Select in Sql Server
create database deadlocktest
go
SELECT snapshot_isolation_state_desc from sys.databases where name='deadlocktest'
go
--ensure this is off: If Not do the following alters
ALTER DATABASE deadlocktest
SET ALLOW_SNAPSHOT_ISOLATION OFF
go
@schotime
schotime / BindingExtensions.cs
Last active October 8, 2015 11:18
Spark Optional Attributes
public static class BindingExtensions
{
public static HtmlTag Data(this HtmlTag html, Dictionary<string, object> dataattrs)
{
foreach (var item in dataattrs)
{
html.Data(item.Key, item.Value);
}
return html;
}
@schotime
schotime / global.asax.cs
Created June 20, 2012 02:00
FluentValidation Localization Adapter
public class FluentValidationKeys : StringToken
{
public static StringToken CREDITCARD_ERROR = new FluentValidationKeys("'{PropertyName}' is not a valid credit card number.");
public static StringToken EMAIL_ERROR = new FluentValidationKeys("'{PropertyName}' is not a valid email address.");
public static StringToken EQUAL_ERROR = new FluentValidationKeys("'{PropertyName}' should be equal to '{PropertyValue}'.");
public static StringToken EXACT_LENGTH_ERROR = new FluentValidationKeys("'{PropertyName}' must be {MaxLength} characters in length. You entered {TotalLength} characters.");
public static StringToken EXCLUSIVEBETWEEN_ERROR = new FluentValidationKeys("'{PropertyName}' must be between {From} and {To} (exclusive). You entered {Value}.");
public static StringToken GREATERTHAN_ERROR = new FluentValidationKeys("'{PropertyName}' must be greater than '{ComparisonValue}'.");
public static StringToken GREATERTHANOREQUAL_ERROR = new FluentValidationK
@schotime
schotime / gist:2395851
Created April 16, 2012 01:19
Delegating Controller
public class HomeController : DelegatingControllers.DelegatingController
{
public ActionResult Edit(UserEditQueryModel model)
{
var vm = Invoker.Execute<UserEditQueryModel, UserEditViewModel>(model);
return View(vm);
}
public ActionResult Edit(UserEditInputModel model)