Skip to content

Instantly share code, notes, and snippets.

View copenhas's full-sized avatar

Sean Copenhaver copenhas

  • Amazon, Inc.
  • Colorado
View GitHub Profile
....
#region StateMachine
static ServiceController()
{
Define(() =>
{
Initially(
When(OnStart)
@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 / vimlang.mkd
Created November 11, 2010 21:12
Excerpt from my VIM presentation - the language of editing

Language of Text Editing

vimsentence := [count]vimexpression

vimexpression := vimcommand | <operator><motion>

vimcommand := <action> | <motion>

That is while in NORMAL mode

WTF? (examples)

Delete next 3 words: 3dw

" When we fold or unfold a block of text determine the block
" delimiters via syntax. You can use 'za' to toggle a fold.
" There are several other commands as well.
" 'zM' to close all folds
" 'zR' to open all folds
set foldmethod=syntax
" Lets map 'za' to spacebar while in NORMAL mode
nnoremap <space> za<space>
[0-9]+ :
{token, {number, TokenLine, list_to_integer(TokenChars)}}.
[0-9]+\.[0-9]+ :
{token, {number, TokenLine, list_to_float(TokenChars)}}.
\+ : {token, {'+', TokenLine}}.
\- : {token, {'-', TokenLine}}.
/ : {token, {'/', TokenLine}}.
\* : {token, {'*', TokenLine}}.
Nonterminals expression.
Terminals number '+' '-' '/' '*' '(' ')'.
Rootsymbol expression.
Endsymbol '$end'.
Left 300 '+'.
Left 300 '-'.
Left 400 '*'.
Left 400 '/'.
-module(generator).
-export([generate/1]).
generate([Root, Left, Right]) ->
LeftExp = generate(Left),
RightExp = generate(Right),
RootExp = generate(Root),
LeftExp ++ RightExp ++ RootExp;
generate({number, _Line, Value}) ->
{ok, Tokens, _EndLine} = lexical:string("2 + 1 * 3").
{ok, Ast} = grammar:parse(Tokens).
Program = generator:generate(Ast).
vm:start().
vm:load(Program).
vm:stop().
receive {return, Value} => Value end.
@copenhas
copenhas / htmlhelperextensions.cs
Created February 3, 2011 02:54
simple example of trying to keep htmlhelper extensions clean and simple
public static class HtmlHelperExtensions
{
public static PhoneHtmlBuilder<TModel> Phone(this HtmlHelper<TModel> helper)
{
return new PhoneHtmlBuilder<TModel>(helper);
}
//... a bunch of other things
}
@copenhas
copenhas / PhoneHtmlBuilder.cs
Created February 3, 2011 03:06
example of an html builder that htmlhelper extensions could return
public class PhoneHtmlBuilder<TModel>
{
private HtmlHelper<TModel> _helper;
private Expression<Func<TModel, string>> _phoneNumber;
private Expression<Func<TModel, string>> _extension;
public PhoneHtmlBuilder(HtmlHelper<TModel> helper)
{
_helper = helper;
}