Skip to content

Instantly share code, notes, and snippets.

View maybe.js
(function() {
// using `value =>` makes value the 'this' of the functions below
const MaybeMonad = value => ({
value,
flatMap(f) {
// The point of the maybe is to prevent problems.
// Having a "nothing value" indicates a problem, so we short circuit
// the return.
// This is like SQL NULL in that any operation on nothing
View XIIR.sql
CREATE FUNCTION array_xirr(flows double precision[], force_period double precision) RETURNS double precision
LANGUAGE plpgsql
AS
$$
DECLARE
v FLOAT8;
rate FLOAT8;
firstday FLOAT8;
period FLOAT8;
exp FLOAT8;
View override_dotnet_headers.cs
var webHeaderCollectionType = typeof(WebHeaderCollection);
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(webHeaderCollectionType.TypeHandle);
try
{
var hInfoFieldInfo = webHeaderCollectionType.GetField("HInfo", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Static);
var hInfo = hInfoFieldInfo.GetValue(null);
var hInfoType = hInfo.GetType().GetField("HeaderHashTable", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Static);
var hashtableOfHeaderInfos = hInfoType.GetValue(hInfo) as Hashtable;
foreach (var e in hashtableOfHeaderInfos.Values)
{
View .gitconfig
# this can be put in [repo]/.git/config for local settings
# or ~/.gitconfig for global settings
# create a difftool "nodiff" that just returns true
# this path is for Mac. On linux it's /bin/true i guess
[diff "nodiff"]
command = /usr/bin/true
# make git ignore white space differences, many different possibilites here
View needsless_action.cs
// Do not do:
var l = new List<Action>{ f, g, h };
l.ForEach(x=>x());
// Instead of:
f();
g();
h();
@tanglebones
tanglebones / needless_switch.cs
Created Jul 10, 2013
Bad Code: obfuscation via switch
View needless_switch.cs
// Do not do:
enum What { A, B, C };
void F(string data, What what)
{
switch (what) {
case A: DoA(data); break;
case B: DoB(data); break;
case C: DoC(data); break;
}
@tanglebones
tanglebones / needless_if.cs
Created Jul 10, 2013
Bad Code: Needless if
View needless_if.cs
// do not write:
if (!expr)
{
return false;
}
return true;
// instead of
@tanglebones
tanglebones / app-client-send-s3.cs
Last active Dec 17, 2015
Sending a file to S3
View app-client-send-s3.cs
private static byte[] GetMultipartFormData(
IDictionary<string, string> parameters,
string caName,
byte[] fileContents,
string boundary)
{
var formDataStream = new MemoryStream();
byte[] formData;
using(formDataStream)
@tanglebones
tanglebones / app-backend-s3policy.cs
Last active Dec 17, 2015
Creating a signed S3 policy for a jpeg image.
View app-backend-s3policy.cs
var policy = new JObject();
policy["expiration"] = DateTime.UtcNow.AddMinutes(10).ToString("u").Replace(" ", "T");
var cond = new JArray();
policy["conditions"] = cond;
cond.Add(new JObject {{"bucket", Bucket}});
cond.Add(new JObject {{"key", filename}});
cond.Add(new JObject {{"acl", "public-read"}});
cond.Add(new JObject {{"Content-Type", "image/jpeg"}});
cond.Add(new JArray("content-length-range", 0, 1024*1024));
@tanglebones
tanglebones / app-backend-sign-s3policy.cs
Last active Dec 17, 2015
Computing the signature for an s3 policy
View app-backend-sign-s3policy.cs
static string ComputeSignature(string policyEncoded, string privateKey)
{
using (var hmac = new HMACSHA1(Encoding.UTF8.GetBytes(privateKey), true))
{
return Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(policyEncoded)));
}
}
You can’t perform that action at this time.