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 / sinatra.shard.rb
Last active December 25, 2015 08:19
Running a sinatra app from a shard...
require 'sinatra'
get '/hi' do
"Hello World, brought to you by a shard!"
end
Sinatra::Application.run! # Because we're not running this file directly...
@mark
mark / .gitignore
Created October 12, 2013 02:56 — forked from zobar/.gitignore
Option monad in Ruby
.rbenv-gemsets
@mark
mark / basic.shard.rb
Created October 12, 2013 02:58 — forked from shard-test/basic.shard.rb
Shard: sample shard
class Greeter
def initialize(who)
@who = who
end
def greet
puts "Hello, #{ @who }!"
end
@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
@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)
# 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