Skip to content

Instantly share code, notes, and snippets.

View built's full-sized avatar

Matt Youell built

View GitHub Profile
@built
built / gist:2896420
Created June 8, 2012 16:00
Dictionary comprehension in C#
// Initial version w/ explicit types:
Dictionary<string, string> users = db.Users.Where(u => u.Account.Id == accountID).ToDictionary<User, string, string>(u => u.Id.ToString(), u => u.Login.Username);
// Without explicit type info.
var users = db.Users.Where(u => u.Account.Id == accountID).ToDictionary(u => u.Id.ToString(), u => u.Login.Username);
@built
built / reddit.com.js
Created March 14, 2012 07:52
An improvement for the Reddit killer
$(document).ready(function() {
$('body').empty();
document.title = "REDDIT IS A WASTE OF TIME";
});
@built
built / gist:1955231
Created March 2, 2012 03:00
Later PDXErlang intersection implementation w/ Tim. Much more performant.
-module(larray).
-include_lib("eunit/include/eunit.hrl").
-export([intersection/2]).
intersection(ListA, ListB) ->
Numbers = lists:foldl( fun(N, Ranking) -> dict:update_counter(N, 1, Ranking) end , dict:new(), ListA ++ ListB),
lists:usort([Unique || Unique <- dict:fetch_keys(Numbers), dict:fetch(Unique, Numbers) > 1]).
%% helper functions to create big lists of random integers
large_random_array(N, MaxValue) ->
@built
built / gist:1955147
Created March 2, 2012 02:43
First naive (n^2) PDXErlang intersection implementation
-module(larray).
-include_lib("eunit/include/eunit.hrl").
-export([intersection/2]).
intersection(ListA, ListB) ->
[X || X <- ListA, lists:member(X, ListB)].
@built
built / csvparser.py
Created February 25, 2012 11:17
Regex for splitting a CSV row while ignoring commas inside quotes
fields = re.findall("(\"[^\"\r]+\"|[^,\r]+)", row)
@built
built / batshit.py
Created February 2, 2012 03:32
Python's Nested List Comprehensions
words = []
for line in sys.stdin.readlines():
for word in line.split():
words.append(word)
# VS.
words = [word for line in sys.stdin.readlines() for word in line.split()]
@built
built / EnglishMotherF.cs
Created January 12, 2012 20:02
Reworked the sample I saw floating around to be closer to how I'd write it. Did not fix bugs.
// Two tips from Brian Kernighan:
// 1. Say what you mean, simply and directly.
// 14. Use symbolic constants for magic numbers. (Applies to chars and strings too.)
//
namespace System
{
public static class SentenceParser
{
protected char BLANK = ' ';
@built
built / session_helper.rb
Created December 22, 2011 00:06
Session
def logged_in?
!! DB.execute("SELECT id FROM sessions WHERE session_key=? LIMIT 1", params[:session_key])
end
def group?(current, last)
current.created_at - last.created_at < 1.minute
end
recent_statuses.inject([]) { |results, status|
next results << [status] if results.empty? # Can't compare first item.
results << group?(status, results.last.last) ? stat : [stat]
}
#!/usr/bin/env perl
use strict;
use constant StartY => 123;
use constant BP => 0;
use constant PATTERN_BUFFER => 0x7F800;
sub register
{
my $register = shift;