Skip to content

Instantly share code, notes, and snippets.

@algal
algal / dateify
Last active February 7, 2024 07:49
Rename a file to prefix with its creation date, or undo this. Makes file match the Denote file-naming scheme
View dateify
#!/bin/bash
# compatibility: works on Debian 12, macOS Sonoma, and in Linux-simulating shell environments in macOS.
# Function to display usage message
usage() {
echo "Usage: $(basename "$0") [OPTION]... [FILE]..."
echo
echo "Rename files by adding or removing a date-time prefix"
echo "in the following format yyyymmddThhmmss--"
@algal
algal / p.sh
Created January 22, 2024 17:31
bash script to query perplexity.ai
View p.sh
#!/usr/bin/env bash
# based off of https://gist.github.com/rauchg/c5f0b1dc245ad95c593de8336aa382ac?permalink_comment_id=4842642#gistcomment-4842642
if [ "$#" -eq 0 ]; then
echo "Usage: $(basename $0) promt_to_send_to_perplexity"
echo ""
echo " Requirements: PERPLEXITY_API, defined; jq and curl, installed; bash, version 3 or higher."
exit 1
fi
function p() {
@algal
algal / run-with-emacs
Last active September 6, 2022 17:25
Boilerplate to write a command line script in emacs lisp
View run-with-emacs
:;exec emacs --no-init-file --no-site-lisp -script "$0" -- "$@" # -*- mode:emacs-lisp -*-
(defun main ()
(require 'cl-lib)
(let ((script-name (nth 2 command-line-args))
(args (cdr command-line-args-left)))
;; assert: ARGS now is a possibly empty list of command line args to the script
;; check for valid arguments here
(when (not args)
(princ (format "usage: %s PATH_TO_FILE" script-name))
@algal
algal / binomial.py
Created May 29, 2021 22:42
binomial coefficients with dynamic programming
View binomial.py
# binomial coefficients
"""
Recurrence relation:
C(n,k) is the count of all k-subsets in a set of n items.
C(n,k) = C(n-1,k-1) + C(n-1,k)
Intuition underlying the relation:
1. pick any item from the n-set, call it the special item.
2. Every k-subset either includes the special item or does not.
View Emacs24bit.md

24 bit color in emacs, under Blink.sh

What's the problem?

The normal way to enable 24 bit color on a recent emacs (27.1) and a recent OS distribution (one which already has the terminal type definition xterm-direct), is just to set TERM=xterm-direct before running emacs.

But this does not work with blink.sh, probably because of some incompatibility in its underlying library, hterm.

What's the solution

@algal
algal / isSubcollection.swift
Last active March 13, 2021 18:37
Look for a subarray in an array in Swift
View isSubcollection.swift
extension Collection {
/// True if `self` is a range within `collection`
/// - Parameter collection: a `Collection` like `Array`
/// - Returns: true or false
///
/// Since this works with `Collection` it works with `Array`, `ArraySlic`, etc..
func isSubcollection<U>(of collection:U) -> Bool
where U:Collection, U.Element == Self.Element, Self.Element:Equatable
{
let (a,b) = (collection,self)
@algal
algal / join_pdf.bash
Last active February 16, 2021 02:00
join PDFs on macOS from the command line with zero dependencies
View join_pdf.bash
#!/bin/bash
if [ "$#" -lt 2 ]; then
echo "usage: $0 output_pdf_path input_path1..."
echo
echo "This script joins the PDFs at input_path1, input_path2, etc."
echo "into one pdf at output_pdf_path"
echo
echo "Known good: macOS 11.1, and below for many versions"
echo
exit 1
@algal
algal / mdfindfile
Last active November 12, 2020 01:09
Easily search for files based their names or glob patterns, by wrapping Spotlight.
View mdfindfile
#!/usr/bin/env bash
# case-insensitive search for $1, maybe in dir $2
case "$#" in
0) echo "usage: $(basename $0) PATTERN [DIR]"
echo ""
echo " Lists paths of files matching PATTERN in DIR or below"
;;
1) exec mdfind "kMDItemDisplayName == '$1'c"
;;
*) exec mdfind "kMDItemDisplayName == '$1'c" -onlyin "$2"
@algal
algal / plot_cm.py
Created September 9, 2020 20:08
Plot a confusion matrix in scikitlearn from data not from an estimator
View plot_cm.py
# This uses scikit learn internals, since the sk public API requires you to pass
# in an estimator and sometimes you just want to pass in the some data you'd
# use to calculate a raw CM
def plot_cm(y_true,y_pred,labels):
from sklearn.metrics._plot.confusion_matrix import ConfusionMatrixDisplay
sample_weight = None
normalize = None
include_values = True
@algal
algal / SFUSDCalendar.md
Created August 29, 2020 20:20
To use an SFUSD Gmail account to subscribe to a non-SFUSD Google Calendar
View SFUSDCalendar.md

Subscribing to a Google Calendar from an SFUSD Google Account

SFUSD Google accounts can only send and receive emails from SFUSD teachers, so they cannot receive calendar invitations from normal gmail accounts, like the gmail accounts of their parents. So you cannot invite them to join a calendar as usual.

Here's a workaround.

In the normal gmail account:

  1. Create the calendar to share from a normal gmail account.
  2. Go to Calendar Settings / General