Skip to content

Instantly share code, notes, and snippets.

View matey-jack's full-sized avatar

Robert Jack Will matey-jack

View GitHub Profile
@matey-jack
matey-jack / tampermonkey.userscript.js
Last active June 2, 2017 12:35
Monthly Payments for Buying a Home on ImmobilienScout24.de
// ==UserScript==
// @name Monthly Payments for Buying a Home
// @namespace http://tampermonkey.net/
// @version 0.1
// @description show monthly payments (Hausgeld + mortgage rate) given a sales expose and user-definable down payment
// @author matey.jack@gmail.com
// @match https://www.immobilienscout24.de/*
// @grant none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
@matey-jack
matey-jack / AnagramDetector
Last active June 29, 2017 22:34
Nice Anagram Detector Example in Java 8, with examples in JUnit
import java.util.HashMap;
import java.util.Map;
public class AnagramDetector {
public Map<Character, Integer> countLetters(String s) {
Map<Character, Integer> result = new HashMap<>();
for (char c : s.toLowerCase().toCharArray()) {
result.put(c, result.getOrDefault(c, 0) + 1);
// alternative, not reading as nicely, but using one less Map lookup:
// result.compute(c, (ignored, oldCount) -> oldCount == null ? 1 : oldCount+1);
@matey-jack
matey-jack / test.ts
Created November 6, 2017 10:35
example: how to easily run any TypeScript code on your command line
#!/usr/bin/env ts-node
/*
You can run simple scripts in TypeScript without compiling or any other other
additional steps!
npm i -g typescript ts-node
chmod 755 test.ts
./test.ts
// "release version" of script is at https://openuserjs.org/scripts/matey-jack/rot17
// ==UserScript==
// @name tatta
// @namespace http://tampermonkey.net/
// @version 0.1
// @description mangle text so user can appreciate the layout better!
// @author You
@matey-jack
matey-jack / .bashrc
Last active November 15, 2017 15:13
type "gradle" in bash and run the next-closest gradlew from current working directory, falling back to globally installed one.
function gradle() {
local path=$(pwd)
while
test ! -f $path/gradlew -a / != "$path" -a -n "$path"
do
path=$(dirname $path)
done
if
test -f $path/gradlew
then
@matey-jack
matey-jack / closure.js
Created January 16, 2018 10:01
Closures manipulate variables directly (but they have to be spelled right!)
'use strict';
function scope() {
var i = 0;
var s = "original";
var b = true;
var o = { field: 0 };
return {
inc: function () { i++; },
@matey-jack
matey-jack / regex-done-right.mjs
Last active July 10, 2018 12:46
NodeJS v8.10.0 also backtracks on regexen: insert a space in the email address at the beginning or middle and it will be a bit slow. insert space at end and it will be very slow...
#!/usr/bin/node --experimental-modules
// fetch the dependency with: yarn add re2
// I tested with re2@1.8.2
import RE2 from "re2";
const emailRegex = new RE2("^[A-Za-z0-9_%+-](\.?[A-Za-z0-9_%+-]+)+@[A-Za-z0-9-](\.?[A-Za-z0-9-]+)+\.[A-Za-z]{2,6}$");
console.log("This is fine: " + emailRegex.test("robert.jack.frederic.manfred.otto.hermann.luis.egon.erwin@friday.de"));
@matey-jack
matey-jack / regex-test.html
Last active July 10, 2018 17:14
Google Chrome and Mozilla Firefox both seem to use backtracking Regex engines and fail at stuff like this!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>This regex is so slow</title>
</head>
<body>
<p>
Be careful! Adding a space in the email address will freeze this page and the entire browser tab.
@matey-jack
matey-jack / DuckType.java
Created July 16, 2018 18:05
Nice Duck Typing in Java: Wrap a class in an interface just as you would in Rust or Go
package example;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* Allows "duck typing" or dynamic invocation based on method signature rather
* than type hierarchy. In other words, rather than checking whether something
* IS-a duck, check whether it WALKS-like-a duck or QUACKS-like a duck.
@matey-jack
matey-jack / A_Location.java
Last active August 31, 2018 13:53
Initialising immutable fields – in a few modern languages
// Java - has not changed since Java 1.0 up until Java 11.
// yes, you need to name each field exactly four times and its type two times.
class Location {
final double lon;
final double lat;
Location(double lon, double lat) {
this.lon = lon;
this.lat = lat;
}