A fun joke based on Evolution of a Haskell Programmer.
require "minitest/autorun"| IDENTITY = -> v { v } | |
| def proc_line(**fns) | |
| fns.reduce(IDENTITY) { |fn, (m, args)| | |
| fn >> m.to_proc.then { |m_proc| -> v { m_proc.call(v, *args) } } | |
| } | |
| end | |
| [50, 100, 300, 5000].map(&proc_line( | |
| to_s: 2, chars: nil, reverse: nil, join: nil, to_i: 2 |
| module DeepGroup | |
| class Grouping | |
| IDENTITY = -> v { v } | |
| def initialize(state) | |
| @state = state | |
| @groupings = [] | |
| @mapping = IDENTITY | |
| end |
A fun joke based on Evolution of a Haskell Programmer.
require "minitest/autorun"| require "sorbet-runtime" | |
| module ADT | |
| module DeconstructableSorbetStruct | |
| def deconstruct | |
| self.class.props.map { send(_1) } | |
| end | |
| def deconstruct_keys(keys) | |
| return self.class.props.to_h { |k| [k, send(k)] } |
| module CodeMigrations | |
| # Number isn't important, vague syntax, and not all | |
| # of these are specifically Rails examples | |
| version from: 4.0, to: 5.0 | |
| # The easiest case, literally A to B direct translation. | |
| class FilterToAction < Migration | |
| expect_translation( | |
| "after_filter" => "after_action", | |
| # ...snipped excess for example |
| require "rubocop" | |
| # Useful for debugging and seeing how the nodes deconstruct | |
| def deep_deconstruct(node) | |
| return node unless node.respond_to?(:deconstruct) | |
| node.deconstruct.map { deep_deconstruct(_1) } | |
| end | |
| def block_to_shorthand?(a, b) |
| require "rubocop" | |
| def tree_distance(object_a, object_b) | |
| tree_distance_matrix(object_a, object_b).last.last | |
| end | |
| def tree_distance_matrix(object_a, object_b) | |
| a_size, b_size = object_a.size, object_b.size | |
| # Rows and columns ascend from 0 to size + 1 |
| require 'proc_to_ast' | |
| def where(&fn) | |
| ast = fn.to_ast | |
| source = fn.to_source | |
| pp ast:, source: | |
| end | |
| where { age > 18 && occupation.downcase == 'plumber' } |
| c = '' << 101 | |
| SUB = /___/ | |
| subs = -> s { s.gsub(SUB, c) } | |
| fn = "/tmp/runs.rb" | |
| # Patch it in for fun | |
| `#{c}cho 'alias run #{subs['___val']}' > #{fn}` | |
| load fn | |
| mm = <<~ABC |
| require 'benchmark/ips' | |
| class TimeFilter | |
| attr_reader :start, :finish | |
| def initialize(start, finish) | |
| @start = start | |
| @finish = finish | |
| end |