Skip to content

Instantly share code, notes, and snippets.

@plredmond
plredmond / withforkio.hs
Last active April 2, 2023 06:45
An attempt to model the masking and interrupt-safety of IO actions with phantom types on the IO monad.
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE Rank2Types #-}
import Control.Concurrent (ThreadId, forkIO, myThreadId, killThread)
import Control.Exception (Exception, SomeException, throwIO, throwTo, catch, mask, uninterruptibleMask)
@plredmond
plredmond / withwarp.hs
Last active April 2, 2023 07:25
Treat a warp webserver as a resource in the bracket pattern which can be created and safely terminated multiple times ina program.
{-# LANGUAGE OverloadedStrings #-} -- only used in the example
import Control.Concurrent (forkIO, forkFinally, killThread)
import Control.Concurrent.MVar (newEmptyMVar, tryPutMVar, takeMVar)
import Control.Monad (void)
import Control.Exception (mask, uninterruptibleMask_, onException, finally)
import Network.HTTP.Types.Status (status200) -- only used in the example
import Network.Wai (Application, responseLBS) -- only used in the example
import Network.Wai.Handler.Warp (Settings, runSettings, defaultSettings, setHost, setInstallShutdownHandler, setGracefulShutdownTimeout)
@plredmond
plredmond / ConstructorType.hs
Last active September 2, 2022 00:08
A template haskell function to produce a constructor-specific type and introduction/elimination functions. (Previously at https://github.com/plredmond/lh-playground/blob/main/lib/TH.hs)
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE LambdaCase #-}
module ConstructorType (constructorType) where
import Control.Monad (replicateM)
import Data.Char (toLower)
import Data.List (unfoldr, foldl1')
import Language.Haskell.TH
#!/usr/bin/env nix-shell
#!nix-shell --pure -i runhaskell -p "haskellPackages.ghcWithPackages (p: [p.wai p.warp p.http-types p.stm p.async])"
{-# LANGUAGE OverloadedStrings #-}
import qualified Control.Concurrent.Async as Async
import qualified Control.Concurrent.STM as STM
import qualified Network.HTTP.Types as HTTP
import qualified Network.Wai as Wai
@plredmond
plredmond / temp.hs
Last active February 28, 2022 19:22
adjust cpu maximum clock frequency in response to cpu temperature
#!/usr/bin/env nix-shell
#!nix-shell -i runhaskell -p "haskellPackages.ghcWithPackages (p: [p.async p.stm p.dimensional p.asciichart p.foldl])"
{-# OPTIONS_GHC "-Wno-missing-signatures" #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE DataKinds #-} -- Only to write down some types. Not doing anything fancy here.
@plredmond
plredmond / env.py
Created April 20, 2021 19:04
linux has a useful tool `env` to run a subprocess in a modified environment; i wrote a quick minimal version of it in python which might help windows users
#!/usr/bin/env python
import os
import sys
import argparse
import subprocess
desc = '''
run a program in a modified environment
example: run a tiny python script that reads the value of the variable "FOO"
@plredmond
plredmond / parse.hs
Last active December 19, 2020 02:55
solution for part 2 of https://adventofcode.com/2020/day/18; the commentted part of `main` expects two files (one with the inputs and one with the outputs for each of the day-18 test expressions)
#!/usr/bin/env nix-shell
#!nix-shell --pure -i runhaskell -p "haskellPackages.ghcWithPackages (p: [p.doctest p.parsec])"
{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC "-Wall" #-}
import Test.DocTest (doctest)
import Text.Printf (printf)
import qualified Text.Parsec as P
import qualified Text.Parsec.Char as PC
@plredmond
plredmond / wakeup.py
Created December 11, 2020 21:48
easily adjust sources of wakeup from sleep in linux using a list of devices to enable/disable and an allow-list
#!/usr/bin/env python3
import sys
import csv,io
import argparse
import collections
class Wakeups:
status_prop = 'Status'
def __init__(self, path):