Skip to content

Instantly share code, notes, and snippets.

View built's full-sized avatar

Matt Youell built

View GitHub Profile
@built
built / gist:42971
Created January 4, 2009 01:07 — forked from anonymous/gist:42949
Didn't like that I had to reassemble the Coins list. Took advantage of parametric temp variables (See Coins variable)
-module(change2).
-export([find_variations/2]).
-export([count_change/1]).
find_variations(0, _) -> 1; % No amount given.
find_variations(_, []) -> 0; % No coins given.
find_variations(Amount, _) % Amount is impossible.
when Amount < 0 -> 0;
find_variations(Amount, [Coin|Rest]=Coins) ->
# One of several ways to do a ternary operation in Python
# For more discussion, see: http://drj11.wordpress.com/2007/05/25/iversons-convention-or-what-is-the-value-of-x-y/
# "AB"[True] => A, likewise "AB"[False] => B
# In this way you can index any two item list with a test:
card_type = ["Low", "High"]][card_value < 10]
#Play with dispatch ideas and guards in Python.
class Guard:
dispatch_table = []
@classmethod
def register(cls, func, condition):
cls.dispatch_table += [(func, condition)]
sub get_operand {
my $arg = shift;
my $listref = @$arg;
if (UNIVERSAL::isa($listref, 'ARRAY')) {
my $x = @$listref;
print length($x).join(",",$x) . "\n"
# WTF?
}
#!/usr/bin/env perl
use strict;
use constant StartY => 123;
use constant BP => 0;
use constant PATTERN_BUFFER => 0x7F800;
sub register
{
my $register = shift;
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]
}
@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
@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 / 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 / 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)