Skip to content

Instantly share code, notes, and snippets.

View JamesKyburz's full-sized avatar
🎸
🎸 🪕

James Kyburz JamesKyburz

🎸
🎸 🪕
View GitHub Profile
@JamesKyburz
JamesKyburz / composed.rb
Created August 9, 2011 10:02
Composed in Ruby
module Kernel
def composed(actions)
lambda do |*args, &block|
actions.reverse.reduce(*args) do |result, next_op|
result = next_op.call(result, &block)
end
end
end
end
@JamesKyburz
JamesKyburz / symbol_to_proc2.rb
Created August 9, 2011 11:31
Allow sending extra parameters when using #symbol.to_proc
class Symbol
def [](*args)
proc {|obj|
obj.send(self,*args)
}
end
end
__END__
@JamesKyburz
JamesKyburz / string.to_f.js
Created August 10, 2011 13:34
string to function
String.prototype.to_f = function() {
var method = this;
var arity = method.match(/arity:(\d+)/);
var argument_count = -1;
if (arity) {
arity = arity[1];
argument_count = 1 + arity;
}
method = method.replace(/\s*arity:\d+/, '');
@JamesKyburz
JamesKyburz / cycle.js
Created August 25, 2011 17:29
cycle.js
var cycle = function() {
var i = 1;
return function(first, second) {
if (first === 'reset') {
i = 1;
return;
}
var value = [first, second][i = 1 - i];
return value;
};
@JamesKyburz
JamesKyburz / magic.rb
Created November 23, 2011 07:45
Magic card "guessing" game
#!/usr/bin/env ruby
deck = (1..21).to_a.shuffle
show_deck = -> {
n = 0
deck.each_slice(7) { |pile| puts "\npile #{n+=1}\n\n#{pile.join("\n")}\n\n" }
}
select_pile = -> pile {
piles = *deck.each_slice(7)
@JamesKyburz
JamesKyburz / setup_git_config.sh
Created December 1, 2011 09:15
My git config
# run like this USER_NAME="?" USER_EMAIL="?" GITHUB_USER="?" GITHUB_TOKEN="?" bash < <(curl -s "https://raw.github.com/gist/1415243/setup_git_config.sh")
git config --global user.name "$USER_NAME"
git config --global user.email "$USER_EMAIL"
git config --global color.branch "auto"
git config --global color.diff "auto"
git config --global color.interactive "auto"
git config --global color.status "auto"
git config --global branch.autosetuprebase "always"
git config --global alias.co "checkout"
@JamesKyburz
JamesKyburz / Dispatcher.cs
Created January 11, 2012 20:40
Dynamic dispatch in c#
static ScriptEngine engine = Ruby.CreateRuntime().GetRubyEngine();
static dynamic dispatcher = null;
public static dynamic DynamicDispatcher(this object Source)
{
dispatcher = dispatcher ?? engine.Execute("->(o){->(*args, &block){o.send(args.shift(), *args, &block)}}");
return dispatcher(Source);
}
// example command.DynamicDispatcher("Caller=", new Caller("blah.."));
@JamesKyburz
JamesKyburz / npm-patch.sh
Created July 2, 2012 19:56
npm install --save --devonly
#!/usr/bin/env bash
if command -v npm-$1 > /dev/null
then
npm-$1 ${@:2}
else
TMP1=`mktemp /tmp/tmp.XXXXXX`
trap "rm -f $TMP1" SIGINT SIGTERM SIGHUP SIGQUIT
@JamesKyburz
JamesKyburz / typed_array_polyfil.js
Created October 19, 2012 13:43
Typed array polyfil
!function(global) {
var typedArrays = ['Int8Array', 'Uint8Array', 'Int16Array', 'Uint16Array', 'Int32Array', 'Uint32Array', 'Float32Array', 'Float64Array'];
for(var i=0; i < typedArrays.length; i++) {
if(typeof global[typedArrays[i]] === 'undefined') {
global[typedArrays[i]] = Array;
}
}
}(this);
@JamesKyburz
JamesKyburz / AdHocQuery.cs
Last active December 13, 2015 23:59
Ad hoc Simple.Data queries
[HttpGet]
public virtual ActionResult Query()
{
var result = entity.All();
var q = Request.QueryString;
if (null != q["select"])
{
var selection = q["select"].Split(',').Select(col => entity[col] as SimpleReference).ToArray();
result = result.Select(selection);