Skip to content

Instantly share code, notes, and snippets.

@bradparker
bradparker / Lehmer.hs
Last active April 29, 2024 06:27
Lehmer.hs
module Main where
import Data.Array (Array, elems, listArray, (!))
import Data.List (elemIndex, uncons, unfoldr)
import Data.Maybe (mapMaybe)
encodeLehmer :: forall a. (Eq a) => Array Int a -> [a] -> [Int]
encodeLehmer set =
unfoldr
( fmap
@bradparker
bradparker / librarything-export-to-table
Last active November 6, 2024 12:13
LibraryThing JSON export to Table
#! /usr/bin/env nix-shell
#! nix-shell -i bash -p jq dasel
jq \
'[.[] | { code: .ddc.code[0], author: .primaryauthor, title: .title }] | sort_by(.code)' \
< $1 | dasel -r json -w csv
@bradparker
bradparker / discriminatedChunks.ts
Last active December 15, 2023 02:54
Chunk array of discriminated union by discriminant?
const isDiscriminant = <TypeName extends string, T extends { type: TypeName }>(
type: TypeName,
candidate: { type: string }
): candidate is T => type === candidate.type;
type Chunk<
TypeName extends string,
T extends { type: TypeName } = any & { type: TypeName }
> = {
type: TypeName;
@bradparker
bradparker / app.ts
Last active November 14, 2023 12:11
React?
import { useState, createElement as h, render, type Element } from "./index.js";
const Counters = () => {
const [count, setCount] = useState(0);
return h(
"div",
{ style: "display: flex; flex-direction: column; gap: 1rem" },
h(
"div",
@bradparker
bradparker / recurrence.sql
Last active September 22, 2022 23:20
Spiking iCal recurrence rules in SQL
-- TODO: perhaps better approach is to generate a sequence of days,
-- weeks, months and years. Then join them.
--
-- TODO: I think I want the 'week of month' to work more like the
-- 'recurrence week index', in that it could still respect "real" weeks. I
-- think the same questions can be answered that way while being a little
-- less surprising (that a string of 7 1s doesn't mean Mon-Sun).
CREATE OR REPLACE FUNCTION recurrence_days (start_at timestamp, end_at timestamp, timezone text)
RETURNS TABLE (
@bradparker
bradparker / relation.rb
Last active June 1, 2022 04:22
Notes from going through Time and Relational Theory by C.J. Date, Hugh Darwen and Nikos A. Lorentzos
require "set"
module ArrayExtras
refine Array do
def second
self[1]
end
def split_at(i)
[first(i), drop(i)]
@bradparker
bradparker / .gitignore
Last active April 29, 2022 05:54
Streaming server
cabal.project.local
dist-newstyle
*.prof*
*.pem
*.html*
{-# LANGUAGE ExplicitForAll #-}
module Combinations (combinations) where
import Control.Arrow ((&&&))
import Control.Monad (replicateM)
import Control.Monad.State (StateT (StateT), evalStateT)
import Data.List (uncons, unfoldr)
unconses :: forall a. [a] -> [(a, [a])]
@bradparker
bradparker / Main.hs
Last active May 10, 2021 10:16
Learning about recursion schemes
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
module Main where
@bradparker
bradparker / Main.hs
Last active April 15, 2021 07:18
Some funny problem from a blog post (https://blog.ploeh.dk/2021/04/12/threading-context-through-a-catamorphism/). Excuse for recursion schemes
{-# LANGUAGE BlockArguments #-}
{-# OPTIONS_GHC -Wall #-}
-- nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/bed08131cd29a85f19716d9351940bdc34834492.tar.gz -p 'haskellPackages.ghcWithPackages (p: [p.recursion-schemes])'
module Main where
import Control.Monad.Reader (Reader, asks, local, runReader)
import Data.Foldable (traverse_)
import Data.Functor.Base (TreeF (NodeF))