Skip to content

Instantly share code, notes, and snippets.

// HERE'S THE PROBLEM I'VE BEEN TRYING TO SOLVE:
// Is it possible (in some type-safe language) to enforce the correctness of a SQL-query-like code joining a transactions table with a conversionRates table (that is, to only allow the amount * rate multiplication if the tables were joined the right way; on both the date and currency columns)?
// SOLUTION ATTEMPT IN SWIFT:
// A "value" represents either a property (cell) of a record, or a record (row) itself
protocol Value {}
@wircho
wircho / chatgpt_loop.py
Last active January 15, 2023 21:18
A chatgpt-wrapper-based Python script that asks ChatGPT questions stored in a Pandas series, and saves the answers in a CSV
import time
import pandas
from chatgpt_wrapper import ChatGPT
chat = ChatGPT()
def run_chat_loop(id_series, question_series, question_prefix, destination_path, pause_duration=1):
assert id_series.shape[0] == question_series.shape[0],\
f"Non matching shapes for id ({id_series.shape}) and question series ({question_series.shape})"
result = pandas.DataFrame({'id': pandas.Series(dtype='str'), 'answer': pandas.Series(dtype='str')})
@wircho
wircho / ABCBug.swift
Created January 16, 2021 05:56
A possible SwiftUI bug?
//
// ContentView.swift
// ABCBug
//
/*
Is this a bug?
Why does pressing the button change
the "outer" value and not the "inner" value?
@wircho
wircho / formula.tex
Created February 18, 2020 05:04
LaTeX template for a single formula.
\documentclass[10pt]{article}
\pagestyle{empty}
\setlength{\topskip}{0pt}
\setlength{\parindent}{0pt}
\setlength{\abovedisplayskip}{0pt}
\setlength{\belowdisplayskip}{0pt}
\usepackage[T1]{fontenc}
\usepackage{geometry}
@wircho
wircho / sqs-latex-selenium.py
Created February 4, 2020 05:07
Selenium script for converting <imgm>formula</imgm> tags into LaTeX images on your Squarespace drafts. Use at your own risk.
from selenium.webdriver.common.action_chains import ActionChains
import selenium.webdriver
import inspect
import time
from selenium.webdriver.common.keys import Keys
import os
import tempfile
import subprocess
import shutil
import sys
@wircho
wircho / simple_server.py
Created February 23, 2019 09:09
A simple Python server superclass
from http.server import BaseHTTPRequestHandler, HTTPServer
import mimetypes
import urllib.parse
import posixpath
import os
from http import HTTPStatus
import shutil
from io import BytesIO
import json
from urllib.parse import urlparse, parse_qs
@wircho
wircho / ThrowingOperators.swift
Created August 4, 2016 15:04
Arithmetic operators that throw on integer type overflow and floating point type errors.
/*
* ThrowingOperators.swift
*/
import Darwin
import CoreGraphics
// MARK: Operators
infix operator &&+ {associativity left precedence 140}
infix operator &&- {associativity left precedence 140}
@wircho
wircho / Usage.swift
Created August 3, 2016 02:34
SafeMatrix methods usage.
/*
* Usage.swift (continued #3)
*/
// MARK: SafeMatrix methods.
func usageMethods() throws {
let W = try SafeMatrix<Matrix,M,N>([1,2,3,4,5,6]) // MxN
let X = try W.transpose() // NxM
let Y = try X * W // NxN
let Z = try Y.inverse() // NxN
@wircho
wircho / MatrixSetup.swift
Last active August 3, 2016 03:40
SafeMatrix methods.
/*
* MatrixSetup.swift (continued #3)
*/
// MARK: SafeMatrix methods
extension SafeMatrixProtocol where MatrixType == Matrix {
func transpose() throws -> SafeMatrix<Matrix,Columns,Rows> {
return try SafeMatrix(matrix: matrix.transpose())
}
}
@wircho
wircho / Matrix.swift
Last active August 3, 2016 00:49
Stubbed methods.
/*
* Matrix.swift (continued)
*/
import Darwin
// MARK: Stubbed methods
extension Matrix {
func transpose() -> Matrix {
abort()