Skip to content

Instantly share code, notes, and snippets.

View Profpatsch's full-sized avatar
🌮

Profpatsch

🌮
View GitHub Profile
@Profpatsch
Profpatsch / string-builder.nix
Created November 19, 2022 19:21
“Efficient” string builder in 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
{-# LANGUAGE LambdaCase #-}
module Pretty
( -- * Pretty printing for error messages
Err,
printPretty,
-- constructors hidden
prettyErrs,
message,
messageString,
let
pkgs = import <nixpkgs> {};
lib = pkgs.lib;
allOptions = (import <nixpkgs/nixos> {}).options;
# ** some helpers
# Like mapAttrs, but if `null` is returned from the mapping function,
# the element is removed from the attrset.
#
@Profpatsch
Profpatsch / Permissions.hs
Created July 16, 2022 13:16
A simple record-based capability system in Haskell
{-# 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
# 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
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)
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))
@Profpatsch
Profpatsch / 1_encoding_sum_types_in_json-schema.md
Last active October 27, 2021 09:09
Encoding sum types with json-schema

The goal is to express an ADT like the following in json schema:

data Sum 
  = Foo { hi :: Int }
  | Bar { x :: Bool }

or in rust syntax:

@Profpatsch
Profpatsch / BitString.hs
Last active July 27, 2021 22:59
dumb BitString wrapper around ByteString that allows to slice it on bits
{-# LANGUAGE TypeApplications, ExplicitForAll, ScopedTypeVariables, BinaryLiterals, NumericUnderscores, TupleSections, ExistentialQuantification, KindSignatures, DataKinds, MultiWayIf, TypeFamilies, ConstraintKinds, TypeOperators, DerivingStrategies, GeneralizedNewtypeDeriving, InstanceSigs, MultiParamTypeClasses, FlexibleInstances #-}
module Main where
import qualified Data.Bits as Bits
import Data.Word
import qualified Data.List as List
import qualified Data.Text as Text
import qualified Data.ByteString as Bytes
import Data.ByteString (ByteString)
@Profpatsch
Profpatsch / .hlint.yaml
Last active July 19, 2022 14:07
hlint, the helpful parts
# HLint configuration file
# https://github.com/ndmitchell/hlint
# Run `hlint --default` to see the example configuration file.
##########################
# Ignore some builtin hints
# often functions are more readable with explicit arguments
- ignore: { name: Eta reduce }