Skip to content

Instantly share code, notes, and snippets.

@cronin101
cronin101 / decorator_assertions.rb
Last active August 29, 2015 13:57
Test Driven Definition: Behaviour assertions in decorators example.
def must_contain_plus meth
raise unless RubyVM::InstructionSequence.disasm(method(meth)).include? 'opt_plus'
meth
end
must_contain_plus(
def add(x, y)
x + y
end)
#=> :add
@cronin101
cronin101 / ruby_developers.rb
Last active August 29, 2015 13:57
What is scope? Baby don't hurt me.
main ⭔ magic_word = 'boomstick'
#=> "boomstick"
main ⭔ String.class_eval { define_method(:say_the_magic_word) { puts magic_word } }
#=> :say_the_magic_word
main ⭔ "wat".say_the_magic_word
#> boomstick
#=> nil
@cronin101
cronin101 / smj.py
Last active August 29, 2015 13:57
Sort-Merge Join, from ADBS Lecture 10
class MergeJoiner(object):
def __init__(self, input_a, input_b):
self.input_a, self.input_b = input_a, input_b
def joined(self):
# Using relation names from slides
R, S = self.input_a[:], self.input_b[:]
join = []
@cronin101
cronin101 / y_com.clj
Last active August 29, 2015 13:56
Y Combinator Example in Clojure
(defn Y [f]
((fn [x] (x x))
(fn [x]
(f (fn [& args]
(apply (x x) args))))))
;; Recursive step for apply_lots function:
; Return accumulator if no iterations remaning otherwise call self with modified stack.
(defn lots_step [partial_fn]
(fn [f r a]
@cronin101
cronin101 / filter.clj
Last active August 29, 2015 13:56
I have no idea what I am doing
(defn my_even? [n]
(= 0 (rem n 2)))
(defn my_filter [test_fn input_list]
(defn recstep [remaining_input acc]
(if (= 0 (count remaining_input))
acc
@cronin101
cronin101 / product.rb
Last active August 29, 2015 13:56
Array#Product
people = %i{Jacob James Maithu}
# => [:Jacob, :James, :Maithu]
bike_colours = %i{Red Black White}
# => [:Red, :Black, :White]
helmet_choices = [true, false]
# => [true, false]
people.product(bike_colours, helmet_choices).map do |p, c, h|
@cronin101
cronin101 / y.rb
Last active August 29, 2015 13:56
Y Combinator example in Ruby
def apply_lots(function: ->(x) { x }, times: 1, initial: nil)
y_combinator = ->(f) {
->(x) { x.(x) }.(
->(x) { f.(->(*v) { x.(x).(*v) }) }
)
}
times_step = ->(partial_fn) {
->(fn, rem, acc) { rem.zero? ? acc : partial_fn.(fn, rem - 1, fn.(acc)) }
}
#!/usr/bin/env python
from __future__ import braces
True, False = False, True
class ExamBoard(object):
def submit(self, work):
work_is_passing_grade = len([work]) > 0
#!/usr/bin/env python
True, False = False, True
class ExamBoard(object):
def submit(self, work):
work_is_passing_grade = len([work]) > 0
return work_is_passing_grade
@cronin101
cronin101 / features.rb
Last active August 29, 2015 13:56
All of the features
Benchmark.realtime do
File.new('./one_to_a_million.txt')
.readlines.map(&:to_i)
.map { |x| x - 999_990}
.select { |x| x >= 0 }
end
#=> 0.598499
Benchmark.realtime do
File.new('./one_to_a_million.txt')[Int]