Skip to content

Instantly share code, notes, and snippets.

View Profpatsch's full-sized avatar
🌮

Profpatsch

🌮
View GitHub Profile
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 / JsonWithSummedTypes.hs
Created November 12, 2023 16:26
Parser for a json dialect which supports tagged values/sums of the syntax: `< "key": value >`
module Abc (jsonWith') where
import Data.Aeson hiding (Value (..))
import Data.Aeson.Key qualified as Key
import Data.Aeson.KeyMap qualified as KM
import Data.Aeson.Parser.Internal hiding (jsonWith')
import Data.Attoparsec.ByteString qualified as A
import Data.Attoparsec.ByteString.Char8 (Parser, char, string)
import Data.Function (fix)
import Data.Functor (($>))
@Profpatsch
Profpatsch / PostgresDecoder.hs
Created October 25, 2023 19:31
nest json parser into postgres parsers
module Postgres.Decoder where
import Control.Applicative (Alternative)
import Data.Aeson qualified as Json
import Data.Aeson.BetterErrors qualified as Json
import Data.Error.Tree
import Data.Typeable (Typeable)
import Database.PostgreSQL.Simple.FromField qualified as PG
import Database.PostgreSQL.Simple.FromRow qualified as PG
import Json qualified
@Profpatsch
Profpatsch / summary.org
Last active October 19, 2023 11:53
Summary of the Talk “Literate DevOps With Emacs”
@Profpatsch
Profpatsch / !nix-build-if-changed.py
Last active July 13, 2023 18:35
build a nix expression, but only if it isn’t already available in substituters; skip downloading the results if that is the case.
#!/usr/bin/env python3
# Small wrapper around nix-instantiate and `nix-store --realize`
# that checks whether the output path is already in a cache
# and if it isn’t, builds it.
#
# The arguments you pass will be given to nix-instantiate,
# not to nix-store --realize
# (This might be a TODO for the future).
@Profpatsch
Profpatsch / Label.hs
Last active May 19, 2023 11:12
Labelled Tuples/Enums in GHC >9.2 Haskell (Hackage: https://hackage.haskell.org/package/pa-label)
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE LambdaCase #-}
module Label
( -- * Labels
Label,
label,
label',
getLabel,
@Profpatsch
Profpatsch / IntegerLiteralOnly.hs
Last active April 3, 2023 17:01
Implement only `fromInteger` for types where num-literals are possible, with less boilerplate
-- | Implement this class if you want your type to only implement the part of 'Num'
-- that allows creating them from Integer-literals, then derive Num via 'NumLiteralOnly':
--
-- @
-- data Foo = Foo Integer
-- deriving (Num) via (NumLiteralOnly "Foo" Foo)
--
-- instance IntegerLiteral Foo where
-- integerLiteral i = Foo i
-- @
@Profpatsch
Profpatsch / Enc.hs
Last active March 24, 2023 20:19
Simple Json encoder library wrapping `aeson`s `Encoding` in a better interface.
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE QuasiQuotes #-}
module Json.Enc where
import Data.Aeson (Encoding, Value (..))
import Data.Aeson.Encoding qualified as AesonEnc
import Data.Aeson.Key qualified as Key
@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: ""
{-# 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