Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
JoshCheek / tco_example.sh
Created Feb 22, 2021
Tailcall optimization in Ruby
View tco_example.sh
# Remove the `-tco` from the end to run it without tailcall optimization
env RUBY_THREAD_VM_STACK_SIZE=100 ruby -e '
RubyVM::InstructionSequence.new(<<~RUBY, tailcall_optimization: !!$tco).eval
def fibo(n, a=0, b=1)
return a if n.zero?
fibo n-1, a+b, a
end
RUBY
p fibo 500' -s -- -tco
@JoshCheek
JoshCheek / index.haml
Last active Feb 18, 2021
How to get Rails' radio buttons to work.
View index.haml
%ul
- @omghis.each do |omghi|
%li
= omghi.wtf
= image_tag omghi.media.variant(resize: "339x243") if omghi.media.present?
:css
.omgsection { border: 1px solid black; }
= form_for @omghi, controller: :omghi, method: :create do |f|
@JoshCheek
JoshCheek / pg_recursive_query.rb
Last active Feb 16, 2021
PostgreSQL recursive querying to build a hierarchy
View pg_recursive_query.rb
require 'pg'
PG.connect(dbname: 'postgres').tap do |db|
db.exec "drop database if exists postgresql_experiments"
db.exec "create database postgresql_experiments"
end
PG.connect(dbname: 'postgresql_experiments').exec(<<~SQL).to_a
create table tree (id int, parent_id int);
@JoshCheek
JoshCheek / refactoring_suggestions.md
Last active Feb 5, 2021
Refactoring Suggestions
View refactoring_suggestions.md

Refactoring Suggestions

Hey, this morning I was thinking about the code we looked at yesterday, and how I'd refactor it. I came up with a few ideas, generally with overlapping reasons for the changes. I'm on a long train ride to St. Louis, and figured I'd write up what things I noticed and how I would make changes based on those things. It got a little long, b/c I had a few goals with it:

  • I wanted to make sure I explained my reasoning so that it wasn't just a religious
@JoshCheek
JoshCheek / example_of_hostile_sib_environment.rb
Last active Jan 24, 2021
Example of a hostile Seeing is Believing environment
View example_of_hostile_sib_environment.rb
# Users can fork the process, SiB still works
if !fork
Process.pid # => 6669
else
Process.pid # => 6668
# Users can exec, SiB still works
exec "echo users can exec, SiB still works" # printing is displayed at eof
end
@JoshCheek
JoshCheek / rubys_20_string_literals.rb
Last active Jan 23, 2021
Ruby's 20 string literals
View rubys_20_string_literals.rb
# 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.
View classes_to_and_from_json_with_inheritance_and_such.rb
# 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 Jan 6, 2021
Sed is not a good language :(
View README.md

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 Jan 5, 2021
Finally figured out this challenge from Clash of Code
View clash_of_code.rb
# 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 Dec 18, 2020
Can Corey Haines figure out how to write the `block` function? (probablah nah)
View lambda-calculus-challenge.js
// 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