Skip to content

Instantly share code, notes, and snippets.

View dmwit's full-sized avatar

Daniel Wagner dmwit

View GitHub Profile
-- dmwit style
module Parser where
import Data.Time
data Transaction = Transaction
{ amount :: Double
, time :: UTCTime
, tags :: [String]
class Stream
def initialize(&more)
@sofar = []
@more = more
end
def [](i)
while i >= @sofar.length do
@sofar += @more.call(@sofar)
end
{-# LANGUAGE FlexibleInstances #-}
import Data.Functor.Compose
import Control.Monad
joinEntry :: Maybe [Maybe [a]] -> Maybe [a]
joinEntry = fmap join . join . fmap sequenceA
instance Monad (Compose Maybe []) where
-- or use liberal applications of coerce to avoid the calls to Compose and getCompose
x >>= f = Compose . joinEntry . getCompose . fmap (getCompose . f) $ x
@dmwit
dmwit / ghc-version
Created February 24, 2019 19:21
A shell script to switch which version of GHC the non-versioned names point to
#!/bin/zsh
set -e
setopt null_glob
case $# in
1) true;;
*)
echo Usage: ghc-version VERSION
ghc --version || true
{-# LANGUAGE TypeFamilies #-}
class Foo a where
type I a t
foo :: I a t -> t -> a
instance Foo () where
type I () t = ()
foo _ _ = ()
instance Foo b => Foo (a, b) where
\xs -> sum $ map (sum . toDigits) xs
= { f $ x = f x }
\xs -> sum (map (sum . toDigits) xs)
= { function application associates to the left }
\xs -> sum ((map (sum . toDigits)) xs)
= { (f . g) x = f (g x), with f = sum, g = (map (sum . toDigits)) }
\xs -> (sum . (map (sum . toDigits))) xs
= { eta reduction }
(sum . (map (sum . toDigits)))
= { remove unnecessary clarifying parentheses }
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
import GHC.Exts
type family AllAllowed k xs where
AllAllowed k '[] = ()
AllAllowed k (x : xs) = (Allowed k x, AllAllowed k xs)
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE TypeFamilies #-}
import GHC.Exts
class (forall a. A t a => A t [a]) => B t where type A t a :: Constraint
instance B t => B [t] where type A [t] a = A t a
@dmwit
dmwit / gist:c764c9b46b08c9aa88fd2d3a5f1f010a
Created March 24, 2019 13:17
applicative initialization
import Control.Monad.Writer hiding (liftIO)
import Data.Functor.Compose
type InitIO = Compose (Writer (IO ())) IO
-- | Do no initialization
liftIO :: IO a -> InitIO a
liftIO = Compose . pure
liftInit :: IO () -> InitIO ()
import Data.List
import Data.Maybe
import Data.Ord
shareArea = 85
pizzaArea size = pi * (size/2)^2
totalPizzaArea = sum . map pizzaArea
sufficient area sizes | area < 0 = [[]]
sufficient area [] = []