Skip to content

Instantly share code, notes, and snippets.

Avatar

Paolo Veronelli paolino

  • Global Access GmbH
  • mountains near Parma (Italy)
View GitHub Profile
View contralogging.hs
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ViewPatterns, TemplateHaskell #-}
module Data.Tracer where
import Data.Functor.Contravariant
import Data.Time (getCurrentTime)
import Control.Lens
@paolino
paolino / atto.hs
Last active Sep 13, 2020
extract an enclosed subtext with attoparsec
View atto.hs
import Control.Applicative ((<|>))
import Data.Attoparsec.Text hiding (match)
import Data.Text (Text)
atto :: Text -> Text -> Text -> Either String [Char]
atto x y = parseOnly $ untilWord x >> untilWord y
untilWord :: Text -> Parser [Char]
untilWord x = mempty <$ string x <|> (:) <$> anyChar <*> untilWord
@paolino
paolino / extract.hs
Last active Sep 13, 2020
extract an enclosed sublist
View extract.hs
extract :: Eq a => [a] -> [a] -> [a] -> Maybe [a]
extract start end xs = do
ys <- snd <$> split start xs
fst <$> split end ys
split :: Eq a => [a] -> [a] -> Maybe ([a], [a])
split p = go id
where
go _ [] = Nothing
@paolino
paolino / divModIso.hs
Last active Sep 1, 2020
optics for things like div mods
View divModIso.hs
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
import Control.Lens -- (over)
@paolino
paolino / curry record.hs
Last active Aug 27, 2020
how to apply a function on a record directly to its fields
View curry record.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- needed for example
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
View L_makeLens.hs
{-
dependencies:
- lens
- template-haskell
create all lenses for a record type preponing an l_ instead of removing the underscore
-}
import Control.Lens ((.~), lensRules, lensField, makeLensesWith, DefName(TopName), FieldNamer)
import Language.Haskell.TH (DecsQ, Name, nameBase, mkName)
View L_makeLens.hs
{-
dependencies:
- lens
- template-haskell
create all lenses for a record type preponing an l_
-}
import Control.Lens ((.~), lensRules, lensField, makeLensesWith, DefName(TopName), FieldNamer)
import Language.Haskell.TH (DecsQ, Name, nameBase, mkName)
@paolino
paolino / ExpressiveShouldBe.hs
Last active Aug 19, 2020
pretty report inequality
View ExpressiveShouldBe.hs
{-
dependencies:
- base
- protolude
- tree-diff
-}
import Data.Typeable (Typeable)
import Data.TreeDiff (toExpr, exprDiff, ansiWlEditExpr, ToExpr)
import Text.Printf (printf)
@paolino
paolino / parse.hs
Created Jul 14, 2020
parse a list of different types driven by parser type
View parse.hs
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module Lib.Parse where
import Control.Lens (Getting, (^?), _Just)
View selda augmented.hs
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE NoImplicitPrelude #-}