Skip to content

Instantly share code, notes, and snippets.

View wpcarro's full-sized avatar

William Carroll wpcarro

  • Hadrian
  • internet
View GitHub Profile
@wpcarro
wpcarro / compileSort.js
Last active January 23, 2023 17:35
Simple DSL for composing sorting logic
// Convert a sorting expressions (e.g. "Outflow DESC; Date ASC; Category ASC")
// into a function that can be passed to Array.prototype.sort.
function compileSort(expr) {
if (expr === '') {
return function(x, y) { return 0; };
}
return expr.split(/\s*;\s*/).reverse().reduce((acc, x) => {
const [k, dir] = x.split(/\s+/);
if (dir === 'ASC') {
return function(x, y) {
@wpcarro
wpcarro / command.yaml.tmpl
Created January 6, 2023 02:33
Dispatch command to k8s
apiVersion: batch/v1
kind: Job
metadata:
name: foo-command
spec:
ttlSecondsAfterFinished: 60
template:
spec:
containers:
- name: foo-command
@wpcarro
wpcarro / config.yaml
Created December 30, 2022 20:14
YNAB -> SQL with benthos.dev
input:
label: ynab_api
generate:
mapping: root = ""
interval: '@every 5m'
processors:
- cache:
operator: get
resource: pagination
key: latest
@wpcarro
wpcarro / example.yaml
Created December 30, 2022 17:16
Caching with benthos (example)
# Proof-of-concept caching with benthos:
# - read from cache (with default on first run)
# - log value
# - write random value to cache
# - repeat
input:
generate:
mapping: root = ""
processors:
@wpcarro
wpcarro / deploys.nix
Created December 5, 2022 22:29
Instantiate NixOS drv on macOS or Linux, and copy it to a deployment target for building and activating.
{ pkgs, ... }:
let
# Invoked on the developer's machine from the command line as:
# $ deploy supported.foo
# $ deploy supported.bar
deploy = pkgs.writeShellScriptBin "deploy" ''
set -euo pipefail
readonly SYSTEM="$1"
readonly SCRIPT=$(NIXPKGS_ALLOWED_UNSUPPORTED_SYSTEM=1 nix-build --show-trace --no-out-link --attr $SYSTEM.deploy $REPO_ROOT)
@wpcarro
wpcarro / cross.nix
Created December 1, 2022 22:46
Trying to build (stdenv).isLinux drvs on OSX.
let
localPkgs = import <nixpkgs> {};
crossPkgs = import <nixpkgs> { crossSystem = "x86_64-linux"; };
in
{
foo = localPkgs.ripgrep;
bar = crossPkgs.ripgrep;
}
@wpcarro
wpcarro / example.sh
Created December 1, 2022 17:27
Using nix-shell as a shebang
#! /usr/bin/env nix-shell
#! nix-shell -i bash -p rsync
echo "Inside script"
which rsync
echo "Exiting..."
@wpcarro
wpcarro / py_to_nix.py
Created November 28, 2022 23:42
Convert Python data types to Nix data types (generated by OpenAI's text-davinci-003 model)
"""
1. Write a library that transforms python data types into Nix data types.
"""
# This library would need to convert python data types such as integers, strings, booleans, lists, dictionaries, and tuples into the Nix data types of strings, booleans, lists, sets, and recursive values. The library should use functions to convert each type of data.
# For example, a function to convert a python integer to a Nix string would look like this:
def int_to_nix_string(x):
return f'"{x}"'
@wpcarro
wpcarro / immutable_list.py
Created November 28, 2022 23:40
Python immutable list type (generated with OpenAI's text-davinci-003 model)
"""
1. Define an immutable list type.
2. Create a library for manipulating the list.
"""
# Answer
# 1. Define an immutable list type
class ImmutableList:
@wpcarro
wpcarro / macro.el
Created November 25, 2022 05:43
Random Elisp macro for composing functions.
(defmacro >-> (&rest forms)
"Compose a new, point-free function by composing FORMS together."
(let ((sym (gensym)))
`(lambda (,sym)
(->> ,sym ,@forms))))