Skip to content

Instantly share code, notes, and snippets.

View copenhas's full-sized avatar

Sean Copenhaver copenhas

  • Amazon, Inc.
  • Colorado
View GitHub Profile
@copenhas
copenhas / gist:664062
Created November 5, 2010 12:27
C Macro that effectively does hierarchical type casting. Credit goes out to an unknown Linux kernel developer.
/*
* Name: container_of
* Parameters: ptr - Pointer to the inner structure that you have.
* Parameters: type - The type of outer structure you want to have.
* Parameters: member - The member in the outer structure for the inner.
* Returns: type * - Pointer to the outer structure you wanted.
* Description:
* This is a macro used to get to a child structure when you have a pointer
* to the root structure. Notice that this makes no checks or validation.
* Use when you know what you have and what you want.
@copenhas
copenhas / dstore.erl
Created March 28, 2012 11:14
distributed k/v store in erlang
-module(dstore).
% internal operations
-export([init/0, loop/1, write/3]).
% public system operations
-export([start_link/0, stop/0, sync/2, replicate/1, pid/0]).
% public k/v operations
-export([lookup/1, write/2]).
-define(TIMEOUT, 30000).
@copenhas
copenhas / Program.cs
Last active September 28, 2017 13:44
Moto hash error when using S3
using System;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Amazon.S3;
using Amazon.S3.Model;
namespace moto_test
@copenhas
copenhas / gist:4337456
Created December 19, 2012 15:21
function builder library idea
// requires at least 1 object parameter and optionally a function callback
var foo = sani(['object+|array', 'function?'], function (objs, callback) {
if (callback) callback();
return objs.length;
});
foo(1, 2);
// throws error
foo({}, {});
@copenhas
copenhas / gist:1337394
Created November 3, 2011 18:45
extension methods!
Directory.EnumerateFiles(_backupdir)
.Select(f => new FileInfo(f))
.Where(f => f.CreationTime < DateTime.Now.AddMonths(-1))
.ForEach(f => f.Delete());
//TData is a type parameter name, sort of like a type variable
public class Node<TData> {
public Node(TData data){
Value = data;
ConnectedNodes = new List<Node>();
}
//notice that TData is the type parameter name from above
public TData Value { get; private set; }
public IList<Node<TData>> ConnectedNodes { get; private set; }
@copenhas
copenhas / gist:1079622
Created July 13, 2011 02:51
example.cs
public IEnumerable<int> Fibanocci() {
var x1 = 1;
var x2 = 1;
while(true) {
var x3 = x1 + x2;
x1 = x2;
x2 = x3;
yield return x3;
}
///<summary>
/// Helper class to get the word counts from text
///</summary>
public class WordCount {
///<summary>
/// Takes in a string of text cuts it up and builds a list of the unique words and
/// the number of times they occurred in the list. Optionally takes a regular
/// expression to use as the delimiter. By default the delimiter is whitespace.
///</summary>
@copenhas
copenhas / gist:1010318
Created June 6, 2011 14:03
example couchdb map/reduce
/*
So with documents as such:
{
one_id : 1,
another_id : 22,
created_at : "2011-05-26 23:22:11",
a_name : "Lisa"
}
We want to get a count of the unique 'a_name' values possibly
set_options(Bt, []) ->
Bt;
set_options(Bt, [{split, Extract}|Rest]) ->
set_options(Bt#btree{extract_kv=Extract}, Rest);
set_options(Bt, [{join, Assemble}|Rest]) ->
set_options(Bt#btree{assemble_kv=Assemble}, Rest);
set_options(Bt, [{less, Less}|Rest]) ->
set_options(Bt#btree{less=Less}, Rest);
set_options(Bt, [{reduce, Reduce}|Rest]) when is_function(Reduce, 2) ->
set_options(Bt#btree{reduce=Reduce}, Rest).