Skip to content

Instantly share code, notes, and snippets.

View awwsmm's full-sized avatar

Andrew awwsmm

View GitHub Profile
@awwsmm
awwsmm / SColor.scala
Last active October 29, 2017 21:11
Scala extension to java.awt.Color with Hex, HSV, RGB, and decimal methods, color conversions, and gradient methods
import java.awt.Color
import scala.math._
////////////////////////////////////////////////////////////////////////////////
///
/// SColor extends java.awt.Color and provides the additional methods:
///
/// asRGB(): List[Int]
/// provides a (red: Int, green: Int, blue: Int) representation of the
/// color, where each component has a range [0,255]
@awwsmm
awwsmm / SigDig.java
Created November 9, 2017 14:27
Round a double to any number of significant figures, and control whether the number is rounded up or down
import java.lang.Math;
public class SigDig {
public static void main(String[] args) {
System.out.println(" -123.456 rounded up to 2 sig figures is " + sigDigRounder(-123.456, 2, 1));
System.out.println(" -0.03394 rounded down to 3 sig figures is " + sigDigRounder(-0.03394, 3, -1));
System.out.println(" 474 rounded up to 2 sig figures is " + sigDigRounder(474, 2, 1));
System.out.println("3004001 rounded down to 4 sig figures is " + sigDigRounder(3004001, 4, -1));
}
@awwsmm
awwsmm / LiveReader.scala
Last active November 9, 2017 16:44
Read a file while it's being written (good for analysis of streaming data)
import java.io.BufferedInputStream
import java.io.BufferedReader
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStreamReader
import java.lang.Thread
import java.nio.charset.StandardCharsets
import scala.io.StdIn
@awwsmm
awwsmm / syntax.scala
Created November 29, 2017 11:31
My Scala syntax conventions
////////////////////////////////////////////////////////////////////////////////
///
/// MY SCALA SYNTAX CONVENTIONS
///
///
/// - "_=" convention for getters, _-prepended class constructor arguments, __-prepended local variables
/// - lowerCamelCase for variable/method names, UpperCamelCase for class names
/// - all local variables are private by default
/// - setter arguments use newValue format (lowerCamelCase with prepended "new")
/// - all classes have __className immutable with String name of class, along with className getter
@awwsmm
awwsmm / DisableReflectiveAccessWarning.java
Created February 1, 2018 16:50
Disable "illegal reflective access" warnings in JDK 9
import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class DisableReflectiveAccessWarning {
///===========================================================================
/// disableWarning():
/// disable "illegal reflective access" warnings in JDK 9
/// author: http://bit.ly/2FblrHA (apangin on StackOverflow)

Regex to find numbers

The following regex finds hexadecimal, octal, and decimal numbers:

((?:[+-])?(?:(?:0?\.[0-9]*)|(?:[1-9]+\.?[0-9]*))(?:[eE][+-]?(?:(?:0?\.[0-9]*)|(?:[0-9]+\.?[0-9]*)))?)|(0x[0-9a-fA-F]+)|(0[0-9]*)

Escaped ("string safe"):

((?:[+-])?(?:(?:0?\\.[0-9]*)|(?:[1-9]+\\.?[0-9]*))(?:[eE][+-]?(?:(?:0?\\.[0-9]*)|(?:[0-9]+\\.?[0-9]*)))?)|(0x[0-9a-fA-F]+)|(0[0-9]*)
/*
* Copyright (c) 2009-2017, Peter Abeles. All Rights Reserved.
*
* Translated from Java to Scala by Andrew Watson, Jan 2018.
*
* This file is part of Efficient Java Matrix Library (EJML).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@awwsmm
awwsmm / FDatum.java
Created May 13, 2018 09:26
Convert generic Java Collection to a (sort of) generic array
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.lang.reflect.Array;
import java.util.Iterator;
// this class has two constructors:
// 1. takes a generic array and assigns it to a generic array
// 2. takes a generic collection and assigns it to a "generic" array
@awwsmm
awwsmm / findAndReplace.sh
Created May 13, 2018 09:32
Bash functions to find and replace across multiple files / rename multiple files
##----------------------------------------------------------------------------
##
## functions to find and replace within multiple files or in file names
##
##----------------------------------------------------------------------------
# if dir, return dir; if symlink, return dir that symlink points to
function unsym(){
echo "$(python -c "import os; print(os.path.realpath('"$1"'))")"
}
@awwsmm
awwsmm / mergeCSV.bat
Created July 19, 2018 16:29
Merge CSV files with batch script (must have same columns)
@echo off
setlocal enabledelayedexpansion
set first=1
set tmpFile=merged.csv.tmp
set outFile=merged.csv
if exist "%tmpFile%" del "%tmpFile%"
if exist "%outFile%" del "%outFile%"