Skip to content

Instantly share code, notes, and snippets.

View monadplus's full-sized avatar
🐧

Arnau Abella monadplus

🐧
View GitHub Profile
@monadplus
monadplus / inequality.hs
Created May 27, 2021 10:47
Inequality in Haskell
import Data.Kind (Type)
-- Let's proof that inequality relation forms a total order
-- i.e. reflexive, transitive, antisymmetric and total.
-- https://en.wikipedia.org/wiki/Inequality_(mathematics)#Formal_definitions_and_generalizations
type (<=) :: Nat -> Nat -> Type
data n <= m where
ZLE :: Z <= m
SLE :: m <= n -> S m <= S n
@monadplus
monadplus / monaderror.hs
Created April 28, 2021 12:13
MonadError + Hierarchical Error
import Control.Monad.Except
import Data.Generics.Product
import Data.Generics.Sum
import Data.Kind
import GHC.Generics
newtype Err1 = Err1 String
deriving stock (Generic)
newtype Err2 = Err2 String
@monadplus
monadplus / readFile.md
Last active March 19, 2021 08:46
readFile

readFile is problematic

System.IO.readFile/writeFile uses the default encoding of the system which may be problematic (see real world issue).

readFile is lazy by default:

  • readFile "text" >>= writeFile "text" . map toUpper will fail (resource is busy) unless readFile is strict.
  • Data.ByteString.Lazy.readFile closes the file after all bytes are read (which may never happen).
  • System.IO.withFile closes it for you but it is dangerous e.g. withFile "input.txt" ReadMode hGetContents >>= write "output.txt" throws hGetContents: illegal operation (delayed read on closed handle)the file is closed before the data is actually read (the data is lazily read).
@monadplus
monadplus / Scratch.hs
Last active November 9, 2020 15:38
Simple case: output depending on input
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
module Scratch where
import Data.Kind
data Protection = None | Password
@monadplus
monadplus / deriving_via_paper.hs
Last active August 15, 2021 10:44
Deriving Via
{-# LANGUAGE AllowAmbiguousTypes #-}
-- {-# LANGUAGE ApplicativeDo #-}
-- {-# LANGUAGE Arrows #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE BinaryLiterals #-}
-- {-# LANGUAGE BlockArguments #-}
-- {-# LANGUAGE CApiFFI #-}
-- {-# LANGUAGE ConstrainedClassMethods #-}
{-# LANGUAGE ConstraintKinds #-}
-- {-# LANGUAGE CPP #-}
@monadplus
monadplus / DerivingVia.hs
Last active August 16, 2021 06:58
Deriving via: example
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveFunctor #-}
import Data.Foldable
@monadplus
monadplus / randomized_median.md
Last active October 13, 2021 10:39
Latex on markdown

Randomized Median Algorithm

Input: A set S of n elements over a totally ordered universe.

Output: The median element of S, denoted by m.

  1. Pick a (multi-)set R of elements in S, chosen independently and uniformly at random with replacement.
  2. Sort the set R.
  3. Let d be the ()th smallest element in the sorted set R.
  4. Let u be the ()th smallest element in the sorted set R.
@monadplus
monadplus / use_the_index_luke.txt
Created September 17, 2020 17:31
Use the Index Luke
_ _ _ _ _ _ _ _
| | | | | | | | (_) | | | | | |
| | | |___ ___ | |_| |__ ___ _ _ __ __| | _____ __ | | _ _| | _____
| | | / __|/ _ \ | __| '_ \ / _ \ | | '_ \ / _` |/ _ \ \/ / | | | | | | |/ / _ \
| |_| \__ \ __/ | |_| | | | __/ | | | | | (_| | __/> < _ | |___| |_| | < __/
\___/|___/\___| \__|_| |_|\___| |_|_| |_|\__,_|\___/_/\_( ) \_____/\__,_|_|\_\___| https://use-the-index-luke.com/sql/
|/
Data is stored in heap with no order. Index are redundant data structures for fast lookup.
@monadplus
monadplus / postgres.txt
Created September 17, 2020 17:30
Postgres: notes
_ _
| | | |
_ __ ___ ___| |_ __ _ _ __ ___ ___ __ _| |
| '_ \ / _ \/ __| __/ _` | '__/ _ \/ __|/ _` | |
| |_) | (_) \__ \ || (_| | | | __/\__ \ (_| | |
| .__/ \___/|___/\__\__, |_| \___||___/\__, |_|
| | __/ | | |
|_| |___/ |_|
@monadplus
monadplus / CTE.sql
Created September 17, 2020 17:29
Postgresql: Common Term Expressions (CTE)
-- Common Table Expressions
-- : Problem
-- 1) For each film count the number of actors starring in it
SELECT f.id, count(*) as actors FROM films f LEFT JOIN films_actors fa ON f.id = fa.film_id GROUP BY f.id;
-- 2) For each film, #actors, its prequel and sequel count number of actors starring in them.
SELECT films.id, films.prequel_id, films.sequel_id,
films.actors AS actors,