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
View ColorConversions.scala
// 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 / regex_MicrosoftCSV.md
Last active August 2, 2023 03:36
Regex for parsing Microsoft-style CSV data
View regex_MicrosoftCSV.md

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 / Typifier.java
Last active June 22, 2023 10:45
Infer type of data from String representation in Java
View Typifier.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
@awwsmm
awwsmm / clearContents.gs
Last active September 11, 2022 18:22
Google Apps Script macro to clear contents in a Google Sheet, skipping protected columns and rows
View clearContents.gs
/** @OnlyCurrentDoc */
clearTransactions(3, 1, 6, 6, true);
function clearTransactions(nHeaderRows, nHeaderColumns, nAccountsPerSection, nRowsPerDay, DEBUG = false) {
// BEGIN https://stackoverflow.com/a/21231012/2925434
function columnToLetter(column) {
var temp, letter = '';
while (column > 0) {
@awwsmm
awwsmm / webpack.config.js
Last active March 13, 2022 21:02 — forked from thatisuday/webpack.config.js
A simple Webpack configuration to compile TypeScript projects with fork-ts-checker-webpack-plugin.
View webpack.config.js
const path = require( 'path' );
const ForkTsCheckerWebpackPlugin = require( 'fork-ts-checker-webpack-plugin' );
module.exports = {
// generate source maps
devtool: 'source-map',
// bundling mode
mode: 'production',
@awwsmm
awwsmm / tryWIthResources.scala
Created November 8, 2021 12:04
Java's try-with-resources pattern, but in Scala.
View tryWIthResources.scala
trait SourceUtils {
/**
* Opens a `resource`, passes it to the given function `f`, then
* closes the resource, returning the value returned from `f`.
*
* Example usage:
* {{{
* val myString: String =
* using(scala.io.Source.fromFile("file.name")) { source =>
@awwsmm
awwsmm / music.scala
Last active November 6, 2021 12:23
Create audio from raw bits in Scala
View music.scala
package com.awwsmm
import javax.sound.sampled.{AudioFormat, AudioSystem, SourceDataLine}
// Inspired by https://community.oracle.com/tech/developers/discussion/1273219/example-code-to-generate-audio-tone
// and https://stackoverflow.com/questions/1932490/java-generating-sound/47916383
// still some "crackling" / "popping" at the end of the tune, but most of the way there
object Main extends App {
@awwsmm
awwsmm / another-example.scala
Last active July 23, 2021 20:25
Finite State Machine with state-specific commands in Scala using Akka
View another-example.scala
import akka.actor.typed.Behavior
import akka.actor.typed.scaladsl.Behaviors
object Test extends App {
sealed trait AnimalCommand
sealed trait DogCommand extends AnimalCommand
object Bark extends DogCommand
@awwsmm
awwsmm / modalModule.R
Created July 12, 2019 13:19
An R Shiny Modal in a Module
View modalModule.R
# define the module UI
moduleUI <- function(id) {
# create the namespace from the id
ns <- NS(id)
# As this is a function, the last statement will be the return value.
# Make sure it contains all of the UI elements you want to display
fluidPage(
@awwsmm
awwsmm / gotta_click.R
Created October 30, 2020 18:42
Minimal Idle / Incremental Clicking "Game" in R Shiny
View gotta_click.R
library(shiny)
ui <- basicPage(
actionButton("theButton", "gotta click"),
verbatimTextOutput("theNumber")
)
server <- function(input, output, session) {
vals <- reactiveValues(counter = 0)