Skip to content

Instantly share code, notes, and snippets.

View dmolesUC's full-sized avatar

David Moles dmolesUC

View GitHub Profile
@dmolesUC
dmolesUC / fold-and-reduce.md
Created January 25, 2018 20:13
foldLeft(), Stream.reduce(), and Observable.reduce()

Vavr List:

List<String> str = List.of("A", "B", "C");
HashSet<String> res = str.foldLeft(
  HashSet.empty(), 
  (set, s) -> set.add(s)
);
@dmolesUC
dmolesUC / combine.py
Last active March 26, 2018 18:14
Python script implementing "Combining Multiple Averaged Data Points and Their Errors" by Ken Tatebe
from typing import Tuple, List
"""A 'bin' of observations in the form (average, error, # of observations)"""
Bin = Tuple[float, float, int]
"""Based on http://isi.ssl.berkeley.edu/~tatebe/whitepapers/Combining%20Errors.pdf"""
def combine(a: Bin, b: Bin) -> Bin:
a_avg, a_err, n_a = a
b_avg, b_err, n_b = b
n = n_a + n_b
@dmolesUC
dmolesUC / wait-for-ajax.rb
Last active July 10, 2018 20:18
Capybara: wait for Ajax with Prototype.js
# in spec/features_helper.rb
# adapted from https://robots.thoughtbot.com/automatically-wait-for-ajax-with-capybara
# for legacy Rails apps still using Prototype.js
def wait_for_ajax!
Timeout.timeout(Capybara.default_max_wait_time) do
# Error pages etc. may not have Ajax defined
loop until page.evaluate_script("(typeof Ajax === 'undefined') ? 0 : Ajax.activeRequestCount").zero?
end
end
@dmolesUC
dmolesUC / webfonts-to-local.rb
Created July 31, 2018 17:18
Script for downloading webfont files from Google Web Fonts CSS and converting CSS to point to local copies
#!/usr/bin/env ruby
require 'pathname'
infile = ARGV[0]
fonts_dir = ARGV[1]
unless infile && fonts_dir
puts "Usage: webfonts-to-local.rb <INPUT-CSS> <FONTS-DIR>"
exit(1)
@dmolesUC
dmolesUC / aws_credentials_timeout.go
Last active January 7, 2019 21:13
Demonstration of AWS credentials timeout issue (https://github.com/aws/aws-sdk-go/issues/2392)
package main
import (
"fmt"
"io/ioutil"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
@dmolesUC
dmolesUC / seed_dump-standalone.md
Created February 12, 2020 17:42
Using the seed_dump gem with StandaloneMigrations

The seed-dump gem generates db/seeds.rb for a Rails project from an existing database. It also works outside of Rails with standalone-migrations, but you need to jump through a couple of hoops to set up the Rake task and give it access to your model classes.

In your Gemfile:

gem 'seed_dump'
@dmolesUC
dmolesUC / sqlite3-pcre.md
Last active July 9, 2020 23:41
Installing sqlite3-pcre on macOS
  1. Decide where you're going to put the compiled library, e.g. ~/lib. We'll call this <target-dir> below.

  2. Clone ralight/sqlite3-pcre

  3. Ensure pcre is installed (brew install pcre).

  4. In the sqlite3-pcre directory, compile with:

    cc \
      -shared \

-o /sqlite3-pcre.so \

@dmolesUC
dmolesUC / ruby-1.8.7-catalina.md
Created August 27, 2020 16:45
Installing Ruby 1.8.7 on macOS Catalina with Homebrew and 0penSSL 1.0

Cf. this StackOverflow answer:

brew install rbenv/tap/openssl@1.0

rvm reinstall 1.8.7-head --with-openssl-dir='/usr/local/Cellar/openssl@1.0/1.0.2t' \
  --with-openssl-lib='/usr/local/Cellar/openssl@1.0/1.0.2t/lib' \
  --with-openssl-include=/usr/local/Cellar/openssl@1.0/1.0.2t/include

rvm use 1.8.7
@dmolesUC
dmolesUC / rake-task-monkey-patch.rb
Last active October 16, 2020 23:45
Monkey-patch Rake::Task to show all task invocations
# In your Rakefile (after Rails.application.load_tasks, in a Rails project):
module TaskExtensions
def invoke
(@logger ||= Logger.new($stderr)).tap do |logger|
logger.info("invoke #{self.name}") # note: you can also dump environment variables etc. here
end
super
end
end
@dmolesUC
dmolesUC / entrypoint.sh
Created October 21, 2020 18:12
Start and terminate multiple sevices
# ########################################
# Start server and manager in background
/start-server.sh &
SERVER_PID=$!
/start-manager.sh &
MANAGER_PID=$!
# ########################################