Skip to content

Instantly share code, notes, and snippets.

@johnhearn
johnhearn / Sandpile.jl
Created January 8, 2020 11:15
Simple sandpile model in Julia
using GR, Random, Memoize
const xmin = 0
const ymin = 0
const xmax = 1
const ymax = 1
const resx = 512
const resy = 512
const hx = resx>>1
const hy = resy>>1
@johnhearn
johnhearn / Wolfram.jl
Last active August 13, 2019 18:32
Wolfram CA in Julia
using GR, Random
rule30(p,q,r) = (p) ⊻ (q | r)
rule90(p,q,r) = p ⊻ r
rule110(p,q,r) = (p | q) ⊻ (p & q & r)
rule = rule110
xmin = ymin = 0
xmax = 1
using GR, Random, Memoize
xmin = ymin = -3
xmax = ymax = 3
resx = resy = 400
setviewport(0, 1, 0, 1)
setwindow(xmin, xmax, ymin, ymax)
setcolormap(3)
@johnhearn
johnhearn / HashBag.java
Last active May 15, 2017 14:06
Simple HashBag implementation
public class HashBag<T> {
HashMap<T, AtomicInteger> map = new HashMap<>();
public void increment(T key) {
AtomicInteger value = map.get(key);
if (value == null) {
map.put(key, new AtomicInteger(1));
return;
}
@johnhearn
johnhearn / Capture.java
Last active May 14, 2017 22:50
Capture stdOut for testing
private static class Capture implements AutoCloseable {
private final PrintStream oldStdOut;
private final PrintStream newStdOut;
private StringWriter writer = new StringWriter();
private Capture() {
oldStdOut = System.out;
newStdOut = new PrintStream(new WriterOutputStream(writer, StandardCharsets.UTF_8));
System.setOut(newStdOut);
@johnhearn
johnhearn / Google Sheets Export as Properties
Last active April 7, 2017 15:55
Export a Google Sheet with translations into a properties file:
function myFunction() {
var datasheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getDataRange().getValues();// if other columns, change index values in the arrays : 0=A, 1=B ...
var text = "";
for(l=1;l<10;l++) {
text = text + "# idioma " + datasheet[0][l] + "\n";
for(i=1;i<datasheet.length;++i) {
if(datasheet[i][0] != '') {
text = text + datasheet[i][0] + " = " + datasheet[i][l] + "\n";
}
}
@johnhearn
johnhearn / gist:779ba282164b3c705ae9
Created July 28, 2014 11:37
Simple reflection based toString utility.
/**
* Simple reflection based toString() method. Dumps object field values (except <code>serialVersionUID</code>) to a
* string in a JSON type format.
*
* @param object the object to get a string representation of.
* @return a string with the fields and values of the object.
*/
public static <T> String toString(T object) {
Class<?> c = object.getClass();
StringBuilder s = new StringBuilder(c.getSimpleName()).append(" { ");