Skip to content

Instantly share code, notes, and snippets.

View threeifbywhiskey's full-sized avatar

Dorien Snake threeifbywhiskey

View GitHub Profile
@threeifbywhiskey
threeifbywhiskey / string_xor.rb
Created January 25, 2014 03:31
Adds Perl's XOR-able strings to Ruby, except that an argument longer than the receiver will be cycled as needed and a Fixnum argument frobnicates (see memfrob(3)) the receiver, though not necessarily with 42. Also, non-alphanumeric XOR pairs for gits and shiggles.
module XOR
def ^ other
case other
when Fixnum
chars.map { |c| (c.ord ^ other).chr }.join
when String
chars.zip(other.chars.cycle).map { |a, b| (a.ord ^ b.ord).chr }.join
else
raise ArgumentError, "must be String or Fixnum"
end
module Refinements
def self.included core
core.send :include, Refinements.const_get(core.to_s)
end
module String
def ^ other
chars.zip(other.chars.cycle).map { |a, b| (a.ord ^ b.ord).chr }.join
end
end
@threeifbywhiskey
threeifbywhiskey / enumerabull.rb
Last active August 29, 2015 13:56
Enumerabull pollutes Kernelspace with inverted versions of Enumerable's instance methods. It's like unleashing a bull in your Ruby shop to get "functional programming".
module Kernel
Enumerable.instance_methods.each do |meth|
define_method(meth) do |*args|
obj = args.pop
case pred = args.first
when Proc, Symbol
obj.send meth, *args.drop(1), &pred
else
obj.send meth, *args
end
@threeifbywhiskey
threeifbywhiskey / multiprint.rb
Created March 7, 2014 22:37
In which an object responds to its printer five different ways.
require 'pp'
class Thing
def to_s
case caller[0]
when /write/ then 'print'
else 'puts'
end
end
@threeifbywhiskey
threeifbywhiskey / less_cryptic_153_easy.rb
Created March 17, 2014 09:12
A solution to /r/dailyprogrammer's 153rd Easy challenge using only lambdas, essentially.
factorial = -> n {
n < 2 ? 1 : n * factorial[n - 1]
}
size = -> ary {
sz = 0
(cut = -> {
sz += 1
(ary = ary[1..-1]) == [] ? sz : cut[]
})[]
@threeifbywhiskey
threeifbywhiskey / legibler_brainfuck.rb
Last active August 19, 2016 01:45
This is a brainfuck interpreter written using little more than lambdas, numbers, and a whole lot of symbols.
SIZE = -> coll {
coll == [] || coll == '' ? 0 : 1 + SIZE[coll[1..-1]]
}
MAP = -> coll, &fn {
coll == [] ? [] : [fn[coll[0]]] + MAP[coll[1..-1], &fn]
}
KEYS = -> hash { MAP[[*hash], &-> h { h[ 0] }] }
VALUES = -> hash { MAP[[*hash], &-> h { h[-1] }] }
@threeifbywhiskey
threeifbywhiskey / brainfuck_church.rb
Last active August 29, 2015 13:57
A brainfuck interpreter written using mostly lambdas, helped along by the Church gem.
require 'church'
include Church
brainfuck = -> prog {
tape = []
sz = SIZE[prog]
pc = bp = i = 0
jumps, stack = {}, []
(jumper = -> {
class String
def >> obj
obj.send self
end
end
class Symbol
def >> obj
obj.send self
end
@threeifbywhiskey
threeifbywhiskey / gorellian_alphabet_sort.c
Created March 19, 2014 08:21
This is a solution to /r/dailyprogrammer's Intermediate challenge #154, Gorellian Alphabet Sort.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXWORD 80
char alphabet[27];
int gorellian_strcmp(const void *va, const void *vb)
{
size_t i;
@threeifbywhiskey
threeifbywhiskey / 154i_church.rb
Created March 19, 2014 17:54
This is a solution to /r/dailyprogrammer's Intermediate challenge #154 written using the Church gem.
require 'church'
include Church
GORELLIAN = -> n, alphabet, words {
alphabet = MAP[CHARS[alphabet], &-> a { ORD[a] | 32 }]
EACH[SORT_BY[words, &-> w {MAP[CHARS[w],
&-> c { INDEX[alphabet, ORD[c] | 32] }] }], &PUTS]
}