Skip to content

Instantly share code, notes, and snippets.

Avatar

Steve Purcell purcell

View GitHub Profile
@purcell
purcell / shell.nix
Last active May 22, 2020
shell.nix with nimlsp
View shell.nix
let
# Assumes niv sources, which should include the repos for
# nimlsp, ast-pattern-matching and jsonschema.
sources = import ./nix/sources.nix;
pkgs = import sources.nixpkgs {};
unpackedSource = d: d.overrideAttrs (orig: {
name = "${orig.pname}-source";
phases = ["unpackPhase" "patchPhase" "installPhase"];
installPhase = "mkdir $out; cp -aR . $out/.";
View Req-reformatted.hs
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
@purcell
purcell / eglot-with-flycheck.el
Created Oct 10, 2019
Make eglot use flycheck
View eglot-with-flycheck.el
(defvar-local flycheck-eglot-current-errors nil)
(defun flycheck-eglot-report-fn (diags &rest _)
(setq flycheck-eglot-current-errors
(mapcar (lambda (diag)
(save-excursion
(goto-char (flymake--diag-beg diag))
(flycheck-error-new-at (line-number-at-pos)
(1+ (- (point) (line-beginning-position)))
(pcase (flymake--diag-type diag)
@purcell
purcell / auto-python-venv.md
Last active Apr 23, 2020
Sane automatic python + virtualenv
View auto-python-venv.md

Automatic python version plus venv

Goal

For easy editor integration and command-line usage, we'd like to be able to specify a Python version per project, with its own virtualenv to isolate its libraries from those of other projects.

We're willing to change $PATH globally once, but not per project. And we'd like to avoid having to run every python command invocation in a special subshell created by a shell wrapper. Instead, simply invoking "python" or "pip" etc. should do the right thing, based on the directory in which it is invoked.

It turns out this is possible!

@purcell
purcell / taskqueues.sql
Last active Apr 26, 2020
Easy task queues using PostgreSQL
View taskqueues.sql
-- Let's say you have a table full of work:
CREATE TABLE tasks (
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
status TEXT NOT NULL DEFAULT 'pending',
payload JSON NOT NULL, -- or just have meaningful columns!
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
View elisp-shuffles.el
(defun key-quiz--shuffle-list (list)
"Shuffles LIST randomly, modying it in-place."
(dolist (i (reverse (number-sequence 1 (1- (length list)))))
(let ((j (random (1+ i)))
(tmp (elt list i)))
(setf (elt list i) (elt list j))
(setf (elt list j) tmp)))
list)
@purcell
purcell / HereQ.hs
Last active Apr 28, 2019
Quasi-quoter for postgresql-simple
View HereQ.hs
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -fno-warn-missing-fields #-}
-- | Interpolate any type that inhabits both IsString and Semigroup
-- This code is based on the "here" package. It would be nice to strip
-- leading whitespace, as the "neat-interpolation" package does.
-- Additionally, "neat-interpolation" has a simpler interpolation
@purcell
purcell / Aggregation.purs
Last active Nov 18, 2019
Multiple aggregates in a single pass, using Purescript
View Aggregation.purs
-- This is based on ideas from the excellent article "Beautiful Aggregations
-- with Haskell" by Evan Borden: https://tech.freckle.com/2017/09/22/aggregations/
module Aggregation where
import Prelude
import Data.Foldable (foldMap)
import Data.Monoid.Additive (Additive(..))
import Data.Newtype (un)
@purcell
purcell / with-docker-db.sh
Last active Mar 6, 2018
Run a command against a PostgreSQL DB installed and started on demand using Docker
View with-docker-db.sh
#!/bin/bash -e
image=mdillon/postgis:9.6-alpine
container_name=my-app-postgresql
if [ -z "$1" ]; then
echo "Run command with a dockerised PostgreSQL DB.
usage: $(basename "$0") command
@purcell
purcell / migrations.sql
Last active Sep 16, 2017
PostgreSQL schema migrations system
View migrations.sql
This code now lives in its own repo at https://github.com/purcell/postgresql-migrations
You can’t perform that action at this time.