Skip to content

Instantly share code, notes, and snippets.

View awwsmm's full-sized avatar

Andrew awwsmm

View GitHub Profile
/*
* 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%"
@awwsmm
awwsmm / addCSVcols.bat
Created July 19, 2018 16:29
Add constant columns to CSV with batch script
@echo off
setlocal enabledelayedexpansion
set row=0
set inFile=merged.csv
set outFile=final.csv
if exist "%outFile%" del "%outFile%"
@awwsmm
awwsmm / SimplestCSVParser.java
Last active August 2, 2018 13:09
The simplest way to parse a CSV in Java, period. < 30 lines, including comments and imports.
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SimplestCSVParser {
// private constructor -- utility class
private SimplestCSVParser(){}
// where the magic happens
@awwsmm
awwsmm / CountLines.java
Last active August 2, 2018 12:48
Count the number of lines in a flat text file
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
// NOTE: adapted from http://bit.ly/2LOQwIP
public class CountLines {
// private constructor in utility class
@awwsmm
awwsmm / LastLine.java
Created August 2, 2018 13:14
Finds the last non-empty line/row of a CSV or XLS(X) file
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
@awwsmm
awwsmm / Settings.java
Last active August 17, 2018 14:10
Load Java program configuration / settings at runtime (key-value pairs)
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
// Compile and then run! Change the settings.conf file and rerun without recompiling!
// Settings are updated without needing to recompile.
public class Settings {
public static boolean DEBUG = false;
@awwsmm
awwsmm / Entries.java
Created August 17, 2018 14:11
Read external file at Java runtime
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class Entries {
public static void main (String[] args) {