Skip to content

Instantly share code, notes, and snippets.

View khellang's full-sized avatar

Kristian Hellang khellang

View GitHub Profile
public class MyModule : NancyModule
{
public MyModule()
{
Post["/scores"] = _ =>
{
var queries = this.Bind<IEnumerable<QueryParameters>>();
return queries.Select(GetResult);
}
@khellang
khellang / iffe.md
Last active January 3, 2016 20:59
IFFE termination

IIFE Termination

If you don't care about the return value of the IIFE, it could be any of the following:

!function(){}();  // => true
~function(){}(); // => -1
+function(){}(); // => NaN
-function(){}();  // => NaN

Let's explore this a bit more.

// Before:
public static class StringExtensions
{
public static string Join(this string separator, params string[] values)
{
return Join(separator, values.AsEnumerable());
}
public static string Join(this string separator, IEnumerable<string> values)
{
public class AppSettings : IAppSettings
{
private readonly ConcurrentDictionary<string, object> _cachedValues = new ConcurrentDictionary<string, object>();
private readonly Func<ISettingsRepository> _repositoryFactory;
public AppSettings(Func<ISettingsRepository> repositoryFactory)
{
_repositoryFactory = repositoryFactory;
}
public class PasswordHash
{
private const int SaltSize = 10;
private const int HashSize = 40;
private const int Iterations = 10000;
private PasswordHash(byte[] hashBytes, byte[] saltBytes)
{
AppDomainAssemblyTypeScanner.DefaultAssembliesToScan = new Func<Assembly, bool>[]
{
x => x == typeof(NancyEngine).Assembly,
x => ShouldReference(x)
};
public static bool ShouldReference(Assembly assembly)
{
var assemblyName = assembly.GetName().Name;
protected override void RequestStartup(ILifetimeScope container, IPipelines pipelines, NancyContext context)
{
pipelines.AfterRequest.AddItemToEndOfPipeline(ctx =>
{
if (ctx.Response.StatusCode != HttpStatusCode.InternalServerError)
{
container.Resolve<DbContext>().SaveChanges();
}
});
}
using System.Text.RegularExpressions;
private const string DelimiterRegex = @"\[(.*?)\]";
public int Add(string numbers)
{
if (string.IsNullOrEmpty(numbers)) return 0;
var delimiters = new List<string> { "," };
public static class NegotiateExtensions
{
public static Negotiator WithCrazyModel(this Negotiator negotiator, object model)
{
return negotiator
.WithMediaRangeModel(MediaRange.FromString("application/foo"), new FooWrapper(model))
.WithMediaRangeModel(MediaRange.FromString("application/bar"), new BarWrapper(model));
}
}
public class Bootstrapper : DefaultNancyBootstrapper
{
protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
{
pipelines.OnError.AddItemToEndOfPipeline((ctx, ex) =>
{
var exception = ex.GetRootException();
return ctx.Negotiate().WithModel(new Error
{