Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
JoshCheek / linked_list.py
Last active March 19, 2021 14:32
linked list in Python
import functools
class LList:
@classmethod
def fromArray(cls, array):
return functools.reduce(lambda ll, val: ll.append(val), array, cls())
def __init__(self): self.isEmpty = True
def append(self, val): return LNode(val, self)
def __iter__(self): return self
@JoshCheek
JoshCheek / how_to_implement_service_objects.rb
Last active March 12, 2021 21:16
A myriad of ways to implement "service objects"
# Note1: all `...` below mean "etcetera", like psuedocode, not implying the new Ruby 3 syntax
# Note2: `obj.()` is syntactic sugar for `obj.call()`
# Given an invocation like this:
OnboardOrganization.(arg1, arg2, ...)
# Here are some possible ways you could implement it:
# 1. If the definition fits in a s single method, you can toss it on a module
# You'd choose a module b/c you don't want a class here since you don't want to
# This fixes a bug that the code below triggered. SiB uses String#<< on this line:
# https://github.com/JoshCheek/seeing_is_believing/blob/46d49a53a68ec22edd52322ad0f1192ef2d77f51/lib/seeing_is_believing/event_stream/producer.rb#L155
# So it should include this alias in the Safe refinements here:
# https://github.com/JoshCheek/seeing_is_believing/blob/46d49a53a68ec22edd52322ad0f1192ef2d77f51/lib/seeing_is_believing/safe.rb#L47-L51
module SeeingIsBelieving::Safe
refine String do
alias << <<
end
end
@JoshCheek
JoshCheek / example.rb
Created February 25, 2021 00:12
Example of making an arg parser from a lambda signature https://twitter.com/josh_cheek/status/1364729672492277762
# Example works if done in args instead of local var assignment:
-> a, b=nil, c { {a: a, b: b, c: c} }[1, 2] # => {:a=>1, :b=>nil, :c=>2}
# Lib could, eg reflect on param types
def arg_parser(&block)
lambda do |argv|
argv, args = argv.dup, []
keywords = block.parameters.filter_map { |t, n| [n, false] if t == :keyreq }.to_h
while (arg = argv.shift)
next args << arg unless /^--?(.*)/ =~ arg
@JoshCheek
JoshCheek / subclassing_nil.rb
Last active February 23, 2021 07:31
Subclassing nil in Ruby 2.7.2 (the old version broke)
RUBY_VERSION # => "2.7.2"
require 'fiddle'
User = Struct.new :name
class NullUser < NilClass
# We have no way to allocate a new NullUser... BUT! We can hack around this
# by making a new object and setting its class pointer to point at NullUser
def self.new(*)
instance = Object.new
@JoshCheek
JoshCheek / tco_example.sh
Created February 22, 2021 17:44
Tailcall optimization in Ruby
# 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 February 18, 2021 21:39
How to get Rails' radio buttons to work.
%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 February 16, 2021 04:27
PostgreSQL recursive querying to build a hierarchy
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 February 5, 2021 18:29
Refactoring Suggestions

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 January 24, 2021 21:50
Example of a hostile Seeing is Believing environment
# 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