Skip to content

Instantly share code, notes, and snippets.

View mark's full-sized avatar
💭
Smoove

Mark Josef mark

💭
Smoove
View GitHub Profile
@mark
mark / .gitignore
Created October 12, 2013 02:56 — forked from zobar/.gitignore
Option monad in Ruby
.rbenv-gemsets
@mark
mark / ultimatum_ecosystem.shard.rb
Created October 10, 2013 03:54
Simulate an ecosystem of the ultimatum game (http://en.wikipedia.org/wiki/Ultimatum_game)
class Array
def random
self[ rand(length) ]
end
def weighted_random(weights=nil)
return weighted_random(map {|n| n.send(weights)}) if weights.is_a? Symbol
weights ||= Array.new(length, 1.0)
@mark
mark / stone_age_dice.shard.rb
Created October 10, 2013 03:51
Stone Age—plot the distribution for # of workers & resource difficulty
TRIALS = 100_000
def d6
rand(6) + 1
end
def nd6(n)
sum = 0
n.times { sum += d6 }
sum
@mark
mark / explanation.md
Created October 10, 2013 03:46
Numbers without zero

Non-zero numbers

This is just something I was toying with—what would our number system be like without a zero digit (which, presumably, was the case before 0 was invented).

I don't think it means anything, but the standard algorithms for addition and multiplication still work.

@mark
mark / bitcount.shard.rb
Created October 10, 2013 03:42
Different ways of counting the # of bits in a number
require 'benchmark'
class Fixnum
def bitcount1
c = 0; n = self
while n > 0
c += n&1
n >>= 1
# encoding: UTF-8
ORTHO = [[0, 1], [0, -1], [1, 0], [-1, 0]]
DIAG = [[1, 1], [1, -1], [-1, 1], [-1, -1]]
BASE_REQUIREMENT = 9
MAP_SIZE = 8
def roll
rand(6) + rand(6) + 2
@mark
mark / description.md
Last active December 24, 2015 23:59
A simple pointer class

Pointer class

Motivation:

Using #tap to operate on and return a value is a useful technique for clean code in Ruby. However, sometimes the value you want to return changes, or you don't even know it at the start. In those cases, you can't use #tap.

This Pointer class defines a #tapp method. #tapp takes a block, just like tap. However, the pointer yielded to the block can be assigned and reassigned using <=, and that assigned value is what is returned from #tapp, not the pointer itself.

class CreditInclusionType < IDontCare
Null = Object.new.tap do |null|
class << null
def by_term?
false
end
def by_credits?
false
@mark
mark / pg_array_to_int_array.rb
Created August 1, 2013 02:14
Reserving primary keys in postgres using Ruby & Sequel
conxn = Sequel.postgres(options)
pg_array = conxn["SELECT reserve_ids(#{table_name}, #{needed}) AS ids"].first[:ids]
new_ids = pg_array.delete('{}').split(',').map(&:to_i)
@mark
mark / include_order.rb
Created July 20, 2013 01:06
sadtrombone.wav
gem 'minitest'
require 'minitest/autorun'
module MyModule; end
module YourModule; end
class YourClass
include YourModule
end