Skip to content

Instantly share code, notes, and snippets.

View Profpatsch's full-sized avatar
🌮

Profpatsch

🌮
View GitHub Profile
@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 / 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 / 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
-- @
{-# 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 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 / Label.hs
Created December 15, 2022 07:50
Labelled values in Haskell that are accessible with record dot syntax
{-# 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
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.
#