Skip to content

Instantly share code, notes, and snippets.

Avatar
🌮

Profpatsch

🌮
View GitHub Profile
View Main.hs
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE DerivingVia #-}
module Main where
import Control.Foldl (Fold)
import Control.Foldl qualified as Fold
import Data.Function ((&))
import Data.Profunctor
import Data.Semigroup
@Profpatsch
Profpatsch / Label.hs
Last active January 31, 2023 11:37
Labelled Tuples/Enums in GHC >9.2 Haskell
View Label.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE LambdaCase #-}
module Label
( -- * Labels
Label,
label,
label',
getLabel,
@Profpatsch
Profpatsch / Label.hs
Created December 15, 2022 07:50
Labelled values in Haskell that are accessible with record dot syntax
View Label.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
module Label
( Label,
label,
label',
)
where
@Profpatsch
Profpatsch / string-builder.nix
Created November 19, 2022 19:21
“Efficient” string builder in nix
View string-builder.nix
let
list =
rec {
empty = { a = null; cons = null; };
singleton = x: { a = x; cons = null; };
cons = x: xs: { a = x; cons = xs; };
# O(n)
foldr = f: zero:
@Profpatsch
Profpatsch / Pretty.hs
Created November 10, 2022 19:25
pretty-printing haskell `Show`able types with nicify-lib and hscolour
View Pretty.hs
{-# LANGUAGE LambdaCase #-}
module Pretty
( -- * Pretty printing for error messages
Err,
printPretty,
-- constructors hidden
prettyErrs,
message,
messageString,
@Profpatsch
Profpatsch / v0.0.1.nix
Created October 25, 2022 20:20
nixos options json schema
View v0.0.1.nix
let
pkgs = import ./. {};
lib = pkgs.lib;
allOptions = (import ./nixos {}).options;
depot = import /home/philip/depot {};
optionSchema = opt: {
type = simpletype opt;
description = opt.description.text or "";
@Profpatsch
Profpatsch / Permissions.hs
Created July 16, 2022 13:16
A simple record-based capability system in Haskell
View Permissions.hs
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
@Profpatsch
Profpatsch / hlint-to-github-warnings.jq
Last active March 21, 2023 13:56
Convert hlint warnings to github Action “smart” log messages
View hlint-to-github-warnings.jq
# the github message format requires newlines to be escaped with the URL-style %0A
# see https://github.com/actions/toolkit/issues/193#issuecomment-605394935
def escapeNewline: gsub("[\\n]"; "%0A");
# depending on the hlint message, we want to display the
# headings and the bodies differently
def prepareTitle:
if .hint == "Unused LANGUAGE pragma"
then { heading: "Unused LANGUAGE pragma: \(.from)"
, suggestion: ""
@Profpatsch
Profpatsch / with.fish
Created February 10, 2022 08:55
Add a package from nixpkgs to your current fish shell
View with.fish
function with --description 'import all arguments as nixpkgs args and put their /bin on PATH'
set arg ( \
printf '
let pkgs = import <nixpkgs> {};
in pkgs.symlinkJoin {
name = "extra-fish-path";
paths = with pkgs; [ %s ];
}
' \
"$argv" \
@Profpatsch
Profpatsch / Incidence.hs
Created November 14, 2021 15:58
How many people do you have to meet to have a 50% chance somebody has Covid at an incidence of 500 (in 100k people)
View Incidence.hs
module Main where
import System.Random
import Data.Ratio
import Data.Bifunctor
-- | Returns for a bunch of people you meet , with a chance of 1/200 for each person, whether any has Covid
randomPeople200 :: (RandomGen gen) => Int -> gen -> (Bool, gen)
randomPeople200 numberOfPeople =
first anyHasCovid . runGeneratorNTimes numberOfPeople 0 (uniformR (1, 200::Int))