Skip to content

Instantly share code, notes, and snippets.

@wesalvaro
wesalvaro / SavingFlow.kt
Last active June 1, 2022 15:00
Creating a `MutableStateFlow` from a Jetpack `ViewModel`'s `SavedStateHandle`.
import androidx.lifecycle.SavedStateHandle
import kotlinx.coroutines.flow.MutableStateFlow
private class SavingFlow<T> private constructor(
private val save: (T) -> Unit,
private val msf: MutableStateFlow<T>
) :
MutableStateFlow<T> by msf {
constructor(
@wesalvaro
wesalvaro / denshadego_zuiki_ps.gpc
Created August 10, 2021 03:14
Use the Zuiki Densha-de-GO! controller for the Nintendo Switch on the PlayStation. Out of the box, it functions but the mascon values do not map correctly. This script translates the Nintendo Switch accepted values into values accepted by the PlayStation.
#pragma METAINFO("Zuiki Densha-de-GO! for PS4", 1, 0, "Wes Alvaro")
// Converts NS Zuiki Densha-de-GO! controller mascon values for PS4.
// Mascon values need conversion or else mascon positions are skipped.
// No changes are needed for other controls (including EB).
// Controls should be set to 「スタンダード」"Standard" + 「ダイレクト」"Direct".
#include <switch.gph>
// The mascon is controlled with the left analog stick's y-axis.
// Values are negative for braking, positive for powering, 0 when neutral.
@wesalvaro
wesalvaro / fira-code.css
Created April 23, 2019 04:23
Use Fira Code font via CSS for Secure Shell in ChromeOS.
@font-face{
font-family: 'Fira Code';
src: url('https://raw.githubusercontent.com/tonsky/FiraCode/master/distr/eot/FiraCode-Regular.eot') format('embedded-opentype'),
url('https://raw.githubusercontent.com/tonsky/FiraCode/master/distr/woff2/FiraCode-Regular.woff2') format('woff2'),
url('https://raw.githubusercontent.com/tonsky/FiraCode/master/distr/woff/FiraCode-Regular.woff') format('woff'),
url('https://raw.githubusercontent.com/tonsky/FiraCode/master/distr/ttf/FiraCode-Regular.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
@wesalvaro
wesalvaro / promisify.js
Created February 23, 2019 01:26
Simple function to convert functions with callback APIs to have Promise APIs.
/**
* Converts a function from a callback API to a Promise API.
*
* Example:
* const context = navigator.geolocation;
* const f = context.getCurrentPosition;
* const getCurrentPosition = promisify(f, 0, 1, context);
* const enableHighAccuracy = true;
* getCurrentPosition({enableHighAccuracy})
* .then(r => console.log(r.coords), e => console.error(e));
@wesalvaro
wesalvaro / wdiff.sh
Last active June 25, 2018 09:08
Simple Word Diff (wdiff) Wrapper for Mercurial
function dowdiff() {
wdiff \
-w $'\e[91m' \
-x $'\e[0m' \
-y $'\e[92m' \
-z $'\e[0m' \
$1 $2
}
function findwdiff() {
local diff=$(mktemp /tmp/wdiff-full.XXXXX)
@wesalvaro
wesalvaro / parent_clock.c
Created May 14, 2018 05:45
Citizen Parent Clock (Arduino Nano)
/**
* Citizen Parent Clock Program
* Send an alternating polarity 24V signal to a child clock every 30 seconds.
* This will tick the clock half and full minutes via its internal electromagnet.
*/
#define TICK_LENGTH_MS 500
#define CHILD_CLOCK_TICK_INTERVAL 30000 // 30 Seconds
#define STATE_SLEEP 0 // No power signal
@wesalvaro
wesalvaro / gviz-ext.js
Created September 27, 2016 06:05
Adds iteration and simpler filtering to DataTable and DataView objects.
const extendGoogleVisualization = () => {
const dataView = google.visualization.DataView.prototype;
const dataTable = google.visualization.DataTable.prototype;
dataView.getNiceRow = dataTable.getNiceRow = function(r) {
const row = [];
for (let c = 0; c < this.getNumberOfColumns(); ++c) {
const colId = this.getColumnId(c);
row.push(this.getValue(r, c));
if (colId) {
row[colId] = row[c];
@wesalvaro
wesalvaro / pipe.js
Created September 9, 2016 07:17
Pipelines asynchronous tasks by abstracting the recursive `setTimeout` calls.
const pipe = (...tasks) => {
const dominos = [];
const fellDomino = () => {
setTimeout(() => {
if (!dominos.length) return;
dominos.pop()();
}, 0);
};
tasks.forEach((task, i) => {
dominos.unshift(() => {
@wesalvaro
wesalvaro / weather.sh
Last active August 29, 2016 06:28
Displays radar images from the JMA.
#/bin/sh
mkdir -p /tmp/weather
if [ -n "$1" ]; then
echo "Deleting old images."
rm -rf /tmp/weather/*
fi
cd /tmp/weather
SECS=`date "+%s"`
NUM_FRAMES="50"
while [ $NUM_FRAMES -gt 0 ]; do
@wesalvaro
wesalvaro / aarray.js
Last active August 15, 2016 04:55
Wraps an Array with generator functions.
const it = function(a) {
return new Aarray(a2g(a));
};
const a2g = function*(a) {
for (let v of a) {
yield v;
}
};