Skip to content

Instantly share code, notes, and snippets.

Josh Cheek JoshCheek

Block or report user

Report or block JoshCheek

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@JoshCheek
JoshCheek / lambda-calc.js
Created Sep 23, 2019
lambda calc translation
View lambda-calc.js
($true => $false => $if => y => succ => zero => toJs =>
(one => two => three => add => nil => cons =>
(multiply => map => reduce =>
console.log(reduce(arr => n => [...arr, toJs(n)])(
// Translation of this program into lambda calculus
// https://dev.to/laurieontech/human-readable-javascript-337o
//
// const arr = [1,2,3]
// const timesTwoPlusIndex = (el, index) => (el*2) + index
@JoshCheek
JoshCheek / gettext_erb_html.rb
Last active Sep 22, 2019
Just playing. Gives `gettext` its own HTML tags.
View gettext_erb_html.rb
# Just playing (based on https://twitter.com/tenderlove/status/1175527260671107072)
require 'nokogiri'
require 'erb'
require 'gettext'
class ErbHtml
include GetText # I have no dea how to use this gem :shrug:
# Based on their example: https://github.com/ruby-gettext/gettext/blob/2fae9e520a578df4d5716712fd8b78953f4076f3/lib/gettext.rb#L184
@JoshCheek
JoshCheek / config.ru
Created Sep 13, 2019
Example of making a Rails inspired minimalist web framework for Rack
View config.ru
# ru means "rackup"
class Object
private def erb(text)
ERB.new(text).result(binding)
end
end
class MyRails
attr_accessor :routes
@JoshCheek
JoshCheek / recentness.rb
Created Aug 16, 2019
List db tables by the recentness of their `created_at` column.
View recentness.rb
# example: ruby recentness.rb myapp_development | column -t
dbname = ARGV.fetch(0)
require 'pg'
puts "schema\ttable\tlast-insert"
PG.connect(dbname: dbname).exec(<<~SQL).each { |row| puts row.values.join "\t" }
create function pg_temp.recentness(table_schema text, table_name text, out last_insert timestamp)
as $fn$ begin
execute format('select max(created_at) from %I.%I', table_schema, table_name)
into last_insert;
end $fn$ language plpgsql;
@JoshCheek
JoshCheek / gist:7cc84715dc7ab6deef40adf9982bf755
Created Aug 12, 2019
Using a refinement to scope a monkey patch
View gist:7cc84715dc7ab6deef40adf9982bf755
# code we don't own
class User
def initialize(first_name, last_name)
@first_name, @last_name = first_name, last_name
end
def full_name
@first_name + " " + @last_name
end
end
@JoshCheek
JoshCheek / feature_testing.md
Last active Aug 1, 2019
Thoughts on creating an acceptance/feature test suite for a website with a React frontend and a few services on the backend
View feature_testing.md

Make fewer tests that are larger, because the tests are going to be extremely expensive and fragile. Make assertions that are very permissive, eg assert that you see a success message, but not what the message says. Because there's a million ways for these tests to fail. Someone changes some wording in a message and tests fail. Someone changes the DOM structure and the tests fail. Someone changes a classname and the tests fail. Some async operation takes longer than expected or resolves in some unexpected order, and the tests fail. But in all those situations, the thing you are testing is still correct, it's just that the way the test checks it is fragile, and the thing you are testing is volatile. So you want the tests to only assert the most important things, and the art of it is figuring out how permissive you want to be. Eg you want to be assertive enough that they don't accidentally pass when they actually should fail, but still permissive enough that they can tolerate irrelevant volatility.

For tooling

@JoshCheek
JoshCheek / map_ar_enum_to_pg_enum.rb
Created Jul 31, 2019
ActiveRecord enum / PostgreSQL enum
View map_ar_enum_to_pg_enum.rb
# require 'pg'; PG.connect(dbname: 'postgres').exec('create database josh_testing') # <-- uncomment to create the db (only needs to run once)
# ===== Setup ActiveRecord =====
require 'active_record'
require 'logger'
ActiveRecord::Base.establish_connection adapter: 'postgresql', database: 'josh_testing'
ActiveRecord::Base.logger = Logger.new $stdout
ActiveSupport::LogSubscriber.colorize_logging = false
# ===== Migration =====
@JoshCheek
JoshCheek / google-apps-translate.js
Last active Jul 19, 2019
Google Apps script to translate selections back and forth between English and Spanish
View google-apps-translate.js
/**
* @OnlyCurrentDoc Limits the script to only accessing the current sheet.
*/
function onOpen() {
DocumentApp.getUi()
.createMenu('Translate')
.addItem('Translate selection to Spanish', 'translateSelectionToSpanish')
.addItem('Translate selection to English', 'translateSelectionToEnglish')
.addToUi();
@JoshCheek
JoshCheek / ruby_and_js_vs_lua.sh
Last active Jul 13, 2019
Ruby vs JS vs Lua (all nonlinear, but JS has best perf)
View ruby_and_js_vs_lua.sh
# Code from this tweet: https://twitter.com/josh_cheek/status/1150035017319497728
$ node -e '
const n = 10000
const str = "x=" + "x".repeat(n)
const regex = /.*.*=.*/
const label = `n=${n}`
console.time(label)
const match = str.match(regex)
console.timeEnd(label)
View budding-of-the-brood.js
// "Budding of the Brood" by Josh Cheek (@josh_cheek on the twitter)
// Paste it into here: https://hydra-editor.glitch.me
// Or you can watch me run it here: https://vimeo.com/346283798
// And some explanatino of how it works here: https://vimeo.com/346242814
// Great explanation of the math here: https://www.youtube.com/watch?v=r6sGWTCMz2k
document.getElementsByClassName('console')[0].style.display = 'none'
function getPoint(time, getX) {
time = (time*5) % points.length
const [x1, y1] = points[Math.floor(time)]
You can’t perform that action at this time.