This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |