Skip to content

Instantly share code, notes, and snippets.

@eddieantonio
eddieantonio / fake_db.rb
Last active August 29, 2015 14:21
Using exceptions for control flow. Yep.
#!/usr/bin/env ruby
require 'english'
class FailedTransaction < StandardError; end
def log(color, message)
color_to_ansi = {
:bold => 1, :green => 32, :red => 31, :italic => 3
}
@eddieantonio
eddieantonio / glozell.rb
Created May 21, 2015 04:57
Is you okay?
require './attr_boolean'
class GloZellGreen
attr_boolean :has_milk?, :has_cereal?
attr_boolean :has_bathtub_filled_with_milk_and_cereal
def initialize(supplies = {})
milk = supplies.fetch(:milk, 0)
cereal = supplies.fetch(:cereal, 0)
bathtub = supplies.fetch(:bathtub, false)
class Object
# Opens a browser window showing the documentation for the given
# class or method on ruby-doc.org.
#
# Usage:
#
# instance.doc -> Goes to class documentation.
# instance.doc :method -> Goes to method documentation.
def doc(name = nil)
cls = self.class
#!/usr/bin/env ruby
module BlackJack
SUITS = [:spades, :hearts, :diamonds, :clubs]
FACES = [:jack, :queen, :king]
# Turns a symbol to an index.
SUIT_TO_NUM = {spades: 0, hearts: 1, diamonds: 2, clubs: 3}
CARD_TO_NUM = {ace: 1, jack: 11, queen: 12, king: 13}
CARD_TO_NUM_UNICODE = {ace: 1, jack: 11, knight: 12, queen: 13, king: 14}
-module(binding).
-export([create/1, create/2]).
% Shortcut for value/option.
create(Value, Options) ->
create(Options#{value => Value}).
% Regular string!
create(Value) when is_binary(Value) ->
#{type => literal,
@eddieantonio
eddieantonio / Number.prototype.iterator.js
Last active October 8, 2015 15:16
Allows for `for (let i of 10) { ... }`
Number.prototype[Symbol.iterator] = function* () {
for (let i = 0; i < this; i++) {
yield i;
}
};
@eddieantonio
eddieantonio / mavenify.py
Created July 22, 2013 18:43
A useless Python script I used to collect JARs and WARs from a directory of Maven projects.
#!/usr/bin/env python
"""
Build all Maven files and dump all of the jars into a directory.
"""
import os
import sys
import re
@eddieantonio
eddieantonio / mahastats.py
Created July 29, 2013 01:26
From a comment near the bottom of the file: "I wanted to know if 'Awakening' was significantly different from the rest. It isn't. "
#!/usr/bin/env python
# Copyright 2013 Eddie Antonio Santos
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
import Data.List (sort)
-- Given a degree sequence, determines whether it is graphical. Returns a list
-- of steps from the base case to the original degree sequence.
graphical :: [Int] -> Maybe [[Int]]
graphical ds | even $ sum ds = graphical' ds []
-- By the handshaking lemma, cannot be a graph with odd sum:
| otherwise = Nothing
graphical' ds previous
#!/usr/bin/env python
from itertools import groupby
def k_sized_groups(sequence, k):
"""
Generates iterable groups with a maximum of size of K members from the
given sequence.
>>> [list(g) for g in k_sized_groups([1, 2, 3, 4], 2)]