Skip to content

Instantly share code, notes, and snippets.

View awwsmm's full-sized avatar

Andrew awwsmm

View GitHub Profile
@awwsmm
awwsmm / ColorConversions.scala
Created October 26, 2017 20:37
Methods for converting RGB <-> HSV <-> Hex <-> decimal in Scala / Java
// converts Int -> 6-digit hex String
def dec2hex(dec: Int): String = String.format("%6s", dec.toHexString.toUpperCase).replace(' ','0')
// converts any hex String to Int
def hex2dec(hex: String): Int = Integer.parseInt(hex, 16)
// convert Hue, Saturation, Value -> Red, Green, Blue
// ([Any]%360, [0,1], [0,1]) -> ([0,255], [0,255], [0,255])
def hsv2rgb(hsv: List[Double]): List[Double] = {
val colorCode = Color.HSBtoRGB(hsv(0).toFloat, hsv(1).toFloat, hsv(2).toFloat)
@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)
@awwsmm
awwsmm / regex_MicrosoftCSV.md
Last active August 2, 2023 03:36
Regex for parsing Microsoft-style CSV data

Parse Microsoft-style CSV data with regex

Background

CSV (comma-separated values) files organise their data by separating them with newlines and commas. If a desired piece of data, say a string of text, itself contains a comma, then this string must be surrounded by double quotes:

5,7,8 -- three values

"5,7",8 -- two values

@awwsmm
awwsmm / regex_escapeSequences.md
Last active September 30, 2018 13:19
Notes on escape sequences in Java Strings

To check if a string contains any escape characters:

jshell> String mytest1 = "this is a string with no escape sequences"
mytest1 ==> "this is a string with no escape sequences"

jshell> String mytest2 = "this string has a \n line break"
mytest2 ==> "this string has a \n line break"

jshell> Collections.disjoint(Arrays.asList(mytest1.split("")), Arrays.asList("\n"))
$21 ==> true

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]*)
@awwsmm
awwsmm / Typifier.java
Last active June 22, 2023 10:45
Infer type of data from String representation in Java
/*
Copyright 2022 Andrew Watson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR