Skip to content

Instantly share code, notes, and snippets.

View wpcarro's full-sized avatar

William Carroll wpcarro

View GitHub Profile
@wpcarro
wpcarro / cipher.hs
Created June 8, 2020 15:52
Encode and decode text using a Vigenere cipher
module Scratch where
-- This impl. is incomplete. I'm sketching out a basic working proof-of-concept to help
-- me better understand the Vigenere cipher.
--
-- Conceptually, a faster, more complete version exists; this isn't it.
import Data.Function ((&))
import Data.Map (Map)
import qualified Data.Map as Map
@wpcarro
wpcarro / buildHaskell.nix
Last active August 12, 2020 22:27
Troubleshooting broken stripe-haskell in <nixpkgs>
{ pkgs, ... }:
{
# Build a Haskell executable. This assumes a project directory with a
# top-level Main.hs. It also applies a few commonly used language extensions.
# Here is an overview of the arguments:
# - `name`: You can find the result at ./result/$name
# - `srcs`: Will be passed to `srcs` field of `pkgs.stdenv.mkDerivation`.
# - `deps`: A function that accepts `hpkgs` and returns a list of Haskell
# dependencies.
@wpcarro
wpcarro / parse_json.py
Last active November 9, 2020 11:51
Proof-of-concept JSON parser
# As an exercise to stress-test my understanding of recursive descent parsers,
# I'm attempting to write a JSON parser without referencing any existing BNF
# descriptions of JSON or existing JSON parser implementations.
#
# I'm only parsing a subset of JSON: enough to parse `sample`. Here is the BNF
# that I wrote to describe my expected input:
#
# expression -> object
# object -> '{' ( STRING ':' expression ) ( ',' STRING ':' expression )* '}'
# | array
@wpcarro
wpcarro / hash.py
Created November 16, 2020 10:54
Hand-rolled Hash Table in Python
def compute_hash(x):
"""
Compute a unique fingerprint for the string input, `x`, as an integer using
the following equation:
x[0] * P^0 + x[1] * P^1 + ... x[n-1] * P^(n-1) % M
P and M are constants where P represents the next available prime number
that's GTE the number of unique characters you'll be hashing. In the case of
all lowercase characters, of which there are 26, the next available prime
@wpcarro
wpcarro / rabin_karp.py
Created November 16, 2020 11:42
Implementing a string-matching algorithm that operates in linear time.
def substring_exists(corpus, pattern):
"""
Return True if `pattern` appears in `corpus`.
This function runs in O(m) time where n is equal to the length of
`corpus`. To improve the efficiency of this algorithm, use a hashing
function the reduces the number of collisions, which will consequently
reduce the number of string-to-string, linear comparisons.
"""
m, n = len(corpus), len(pattern)
@wpcarro
wpcarro / script.sql
Created June 27, 2022 22:46
Synchronize MSSQL RDS backup/restore
Declare @T Table (
task_id int,
task_type VARCHAR(250),
database_name VARCHAR(250),
"% complete" INT,
"duration(mins)" INT,
lifecycle VARCHAR(250),
task_info VARCHAR(1000),
last_updated DATETIME,
created_at DATETIME,
@wpcarro
wpcarro / default.nix
Created July 29, 2022 00:37
Run Elisp unit test during nix-build
{ pkgs, depot, ... }:
let
al = pkgs.callPackage
({ emacsPackages }:
emacsPackages.trivialBuild {
pname = "al";
version = "1.0.0";
src = ./al.el;
packageRequires =
@wpcarro
wpcarro / while_loop.ml
Created October 10, 2022 17:40
OCaml experiments
(* 1/n experiments in replacing Python with OCaml for some tasks. *)
let n = ref 0 in
while !n < 10 do
print_string ".\n";
n += !n + 1;
done;;
@wpcarro
wpcarro / fix-broken-symlinks.nix
Last active October 12, 2022 15:20
sanity-checking some cursed Nix
@wpcarro
wpcarro / one.ml
Created October 14, 2022 01:18
Update association list?
[("fname", "John"); ("lname", "Cleese"), ("age", 82)]
|> List.remove_assoc "fname"
|> List.cons ("fname", "Bill")