Skip to content

Instantly share code, notes, and snippets.

View dmolesUC's full-sized avatar

David Moles dmolesUC

View GitHub Profile
@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 / 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 / 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 / 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 / Arguments.java
Created January 3, 2018 23:40
Argument assertions
public class Arguments {
// ------------------------------------------------------------
// Checks
public static <T> T require(T argument, Predicate<T> condition, Supplier<String> msgSupplier) {
return require(argument, () -> condition.test(argument), msgSupplier);
}
public static <T> T requireNot(T argument, Predicate<T> condition, Supplier<String> msgSupplier) {
@dmolesUC
dmolesUC / jruby-openssl-x509-storeerror.txt
Created January 3, 2018 18:42
Stack trace: "OpenSSL::X509::StoreError: setting default path failed: No password supplied for PKCS#12 KeyStore"
$ ./gradlew check
> Configure project :buildSrc
2018-01-03 10:41:04.718:INFO::Daemon worker Thread 10: Logging initialized @6632166ms
2018-01-03 10:41:04.763:INFO:oejs.Server:Daemon worker Thread 10: jetty-9.2.12.v20150709
OpenSSL::X509::StoreError: setting default path failed: No password supplied for PKCS#12 KeyStore.
set_default_paths at org/jruby/ext/openssl/X509Store.java:165
SSLContext at uri:classloader:/META-INF/jruby.home/lib/ruby/shared/jopenssl19/openssl/ssl-internal.rb:31
SSL at uri:classloader:/META-INF/jruby.home/lib/ruby/shared/jopenssl19/openssl/ssl-internal.rb:22
@dmolesUC
dmolesUC / IndexColumnDemo.java
Created December 19, 2017 19:12
Displaying JavaFX TableView row number in a column
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.ObjectBinding;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
@dmolesUC
dmolesUC / TableHeightDemo.java
Created December 19, 2017 19:08
Showing only populated rows in a JavaFx TableView
import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.skin.TableHeaderRow;
import javafx.stage.Stage;
@dmolesUC
dmolesUC / supplementary.md
Created December 18, 2017 23:12
Match Unicode supplementary characters (U+10000–U+EFFFF) with regex in Java

A naive regex to match supplementary characters in the range U+10000–U+EFFFF produces nonsensical results:

jshell> Pattern.matches("[\u10000-\uEFFFF]+", "abc")
    ==> true

Likewise the version using regex escapes rather than character escapes:

jshell> Pattern.matches("[\\u10000-\\uEFFFF]+", "abc")
    ==> true
@dmolesUC
dmolesUC / compareTo.java
Last active November 30, 2017 22:19
Java compareTo() in functional style
import io.vavr.collection.Array;
import java.net.URI;
import java.util.Objects;
import java.util.function.IntSupplier;
import java.util.stream.Stream;
class Vocab implements Comparable<Vocab> {
private final String prefix;
private final URI uri;