Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
JoshCheek / rubys_20_string_literals.rb
Last active January 23, 2021 16:00
Ruby's 20 string literals
# Backticks and %x make a string literal and implicitly pass it to the backticks method,
# so if we define backticks to be identity, then that makes then equal to normal string literals.
# Though in practice, I've always taken advantage of the implicit invocation instead of nooping it.
def `(str)
str
end
# I'm ignoring different delimiters, on the percent literals.
# eg I consider %q(str) and %q[str] to be equivalent, because otherwise there'd
# probably be another 500 possibilities.
# A potential way to deal with https://twitter.com/postmodern_mod3/status/1348330707420991497
# include it to get reasonable JSONability
module JsonCreatable
def self.append_features(base)
base.extend ClassMethods
base.include InstanceMethods
end
module ClassMethods
@JoshCheek
JoshCheek / README.md
Last active January 6, 2021 13:49
Sed is not a good language :(

Can you redeem sed's honour?

The premise

Been doing coding challenges. One of them is a golfing challenge, and you can use bash, I've got a really good Ruby solution, but figured I could get it even shorter if I used sed.

@JoshCheek
JoshCheek / clash_of_code.rb
Created January 5, 2021 11:47
Finally figured out this challenge from Clash of Code
# https://www.codingame.com/clashofcode/clash/report/15311917e86988f7c91d80a62ec9bf1022c0607
# It's a multiplexer, first bits select an output bit from the remaining bits.
# Only one person actually figured it out, and the others seem to think that person is a bot, so :shrug:
DATA.read.split("\n\n").map(&:lines).each do |test|
width, num_rows = test.shift.split.map(&:to_i)
rows = test.take(num_rows)
expecteds = test.drop(num_rows).map(&:chomp)
outputs = rows.map do |row|
# first $width bits choose which of the remaining bits we select
@JoshCheek
JoshCheek / lambda-calculus-challenge.js
Last active December 18, 2020 14:28
Can Corey Haines figure out how to write the `block` function? (probablah nah)
// EXPLANATION / CHALLENGE: https://vimeo.com/492391391
// The program in JavaScript: `for(let i=10; i; --i) console.log(i)`
// But THIS!! is the program in "lambda calculus JavaScript"!!!!
// Functoins that begin with a dollar sign cheat and use JS stuff
const $DIE = arg => { throw new Error("should not call!") }
// The rest of these cheat by assigning to constants instead of passing into variables,
// but none of them are self refferential, so that's just to make it easier to read
@JoshCheek
JoshCheek / active_record_for_databases_with_different_conventions.rb
Last active November 19, 2020 14:38
Example of how you can connect ActiveRecord to a preexisting database with different conventions
require 'active_record'
# An in-memory sqlite database, this is configured in config/database.yml
# Note that you will need the `sqlite3` gem installed
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
# The db schema and data which do not follow the Rails convention,
# these might come from the Python app
sqlite = ActiveRecord::Base.connection.raw_connection
sqlite.execute 'create table user(id integer primary key, name string)'
@JoshCheek
JoshCheek / simulation.rb
Created October 30, 2020 16:14
Gender gap in chess explained by sample sizes (simulating the thought experiment)
# Simulating the thought experiment in this paper:
# https://en.chessbase.com/post/what-gender-gap-in-chess
def simulate(n:, samples: 100_000)
sum, maxes = 0, 0
samples.times do
scores = n.times.map { rand 1..100 }
maxes += scores.max
sum += scores.sum
end
@JoshCheek
JoshCheek / json_parse.rb
Created April 22, 2020 20:43
Not sure why, but I suddenly felt compelled to make a JSON parser 🤷‍♂️
def json_parse(json)
success, index, _ws = json_parse_optional_whitespace(0, json)
success, index, value = json_parse_value(index, json)
raise "Could not parse" unless success
value
end
def json_parse_value(index, json)
%I[
json_parse_null
@JoshCheek
JoshCheek / refining_enumerable_reduce.rb
Last active February 27, 2020 02:39
Using refinements to monkey patch a reversed `reduce` in Ruby
module Consistency
original = Enumerable.instance_method :reduce
refine Enumerable do
# I didn't actually check it against the real impl or docs or anything, but its something like this
alias_method :inject, define_method(:reduce) { |*args, &block|
block = args.pop if args.size == 2
Enumerator.new do |y|
original.bind(self).call(*args) { |l, r| y.yield r, l }
end.each &block
}
@JoshCheek
JoshCheek / rot_13.rb
Created January 30, 2020 15:21
Rot13
def rot13(ascii_char)
ascii_offset = 65
alphabet_size = 26
full_index = ascii_char.ord - ascii_offset
cap_bit = full_index & 0b100000
half_index = full_index & 0b011111
rotated = (half_index + alphabet_size/2) % alphabet_size
(rotated + cap_bit + ascii_offset).chr
end