Skip to content

Instantly share code, notes, and snippets.

View neilmayhew's full-sized avatar

Neil Mayhew neilmayhew

View GitHub Profile
@neilmayhew
neilmayhew / git-problem-repro.sh
Created November 13, 2024 19:41
A script to reproduce `git fsck` failures on `.gitmodules` history
#!/usr/bin/env -S bash -euo pipefail
# Reproduce git fsck problem
DIR=$(mktemp -d)
trap 'cd /; rm -rf "$DIR"' EXIT
cd "$DIR"
git init
@neilmayhew
neilmayhew / GracefulTermination.hs
Last active April 6, 2023 20:43
Graceful handling of SIGTERM for Haskell programs
module System.GracefulTermination where
import Control.Concurrent.Async (race)
import Control.Concurrent.MVar
import Data.Functor (void)
import System.Posix.Signals
-- | Ensure that @SIGTERM@ is handled gracefully, because it's how containers are stopped.
--
-- @action@ will receive an 'AsyncCancelled' exception if @SIGTERM@ is received by the process.
@neilmayhew
neilmayhew / ToXML.hs
Created January 13, 2023 19:21
Convert Haskell datatypes to XML using their ToJSON instance
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}
import Data.Aeson (ToJSON, Value(..), eitherDecode, toJSON)
import Data.Aeson.Key (toString)
import Data.Aeson.KeyMap (fromList, toAscList)
@neilmayhew
neilmayhew / Makefile
Last active December 15, 2022 03:38
Advent of Code 2022
check:
runghc -Wall -Wcompat aoc-5.hs <aoc-5.input.txt | diff aoc-5.1.output.txt -
runghc -Wall -Wcompat aoc-5.hs 2 <aoc-5.input.txt | diff aoc-5.2.output.txt -
@neilmayhew
neilmayhew / Identity-HowTo.md
Created December 16, 2021 18:26
Configuring git to use a different GitHub/GitLab identity for some projects

Using a different GitHub/GitLab account for some projects

If you're working on a project that requires you to use a separate account on GitHub/GitLab, it can be a bit tricky to set up. Your ssh key identifies you uniquely, so you can't use the same key for two different accounts. However, git doesn't have a simple way for you to configure an ssh key for a repo or a group of repos. You have to do it by creating an alias for the host name and associating a different key with that host name. Here's one way to do it.

Note: Replace all instances of something with the project or client name.

Create a new ssh key

ssh-keygen -f ~/.ssh/id_rsa-something [other-options]
@neilmayhew
neilmayhew / Median2.cs
Last active March 19, 2021 13:53
Median of two sorted lists
// Return the median of a ∪ b ≠ ∅ if a and b are sorted
static double Median2(int[] a, int[] b)
{
int m = a.Length;
int n = b.Length;
int h = (m + n + 1) / 2; // 'Half' - the number elements in the subset
int Search(int lo, int hi) {
if (lo == hi)
return lo; // No other choices left
@neilmayhew
neilmayhew / isPrefix.cc
Created December 4, 2020 20:17
A test harness for the isPrefix function
#include <iostream>
#include <string>
inline int isPrefix(std::string pre, std::string str)
{
return str.compare(0, pre.size(), pre);
}
int main(int argc, char const *argv[])
{
@neilmayhew
neilmayhew / update-oem-vmware.sh
Last active July 22, 2023 21:42
Update a Flatcar installation on VMWare to use the latest OEM content
#!/usr/bin/env bash
# Update a Flatcar installation on VMWare to use the latest OEM content
#
# Copyright 2020, Neil Mayhew <[email protected]>
# LICENSE: MIT
set -ex
shopt -s extglob nullglob
@neilmayhew
neilmayhew / ordering-semigroup.hs
Created August 7, 2020 03:51
Ordering Semigroup instance
class Eq a => Ord a where
compare :: a -> a -> Ordering
class Semigroup a where
(<>) :: a -> a -> a
instance Semigroup Ordering where
(<>) = undefined
func :: (Ord u, Ord v) => (u, u) -> (v, v) -> Ordering
@neilmayhew
neilmayhew / NaturalSort.hs
Last active April 28, 2020 13:32
Natural Sort Algorithm
import Data.Char (isDigit)
import Data.List (sortBy)
naturalCompare :: String -> String -> Ordering
naturalCompare s@(c:s') t@(d:t')
| isDigit c && isDigit d = numericCompare 0 s t
| otherwise = compare c d <> naturalCompare s' t'
naturalCompare s t = compare s t
numericCompare :: Int -> String -> String -> Ordering