This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections.abc | |
import heapq | |
class HeapQueue(collections.abc.Container, collections.abc.Sized): | |
""" | |
A min-heap class using the standard library's heapq module. | |
This comes in handy if you need a priority-queue algorithm | |
but you don't need the heavy machinery of queue.PriorityQueue | |
(like blocking calls, timeouts, thread-safety, and so on). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DeriveFoldable #-} | |
{-# LANGUAGE DeriveFunctor #-} | |
{-# LANGUAGE DeriveTraversable #-} | |
{-# LANGUAGE TupleSections #-} | |
{-# LANGUAGE TypeOperators #-} | |
import Control.Comonad | |
import Control.Monad | |
import Data.Functor.Reverse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE InstanceSigs #-} | |
{-# LANGUAGE TypeOperators #-} | |
{-# LANGUAGE PolyKinds #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
import Control.Monad | |
import Data.Constraint | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DeriveFunctor #-} | |
{-# LANGUAGE FunctionalDependencies #-} | |
{-# LANGUAGE UndecidableInstances #-} | |
import Control.Monad (ap) | |
import Control.Monad.Free | |
import Control.Comonad | |
import Control.Comonad.Cofree | |
import Data.List |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
permutations :: [a] -> [[a]] | |
permutations [] = [[]] | |
permutations (x:xs) = concatMap (insertions x) (permutations xs) | |
where | |
insertions x [] = [[x]] | |
insertions x zs@(y:ys) = (x:zs) : [y:ins | ins <- insertions x ys] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ISetter<in S, out T, out A, in B> | |
{ | |
T Rewrite(S oldContainer, Func<A, B> rewriter); | |
} | |
interface IFold<in S, out A> | |
{ | |
R Aggregate<R>(S container, R seed, Func<R, A, R> aggregator); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open import Agda.Primitive | |
module _ {l : Level} where | |
record Category : Set (lsuc l) where | |
infixr 10 _~>_ | |
infixr 90 _o_ | |
field | |
Obj : Set l | |
_~>_ : Obj -> Obj -> Set | |
id : forall {x} -> x ~> x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Word (Word8) | |
rleBytes :: [Word8] -> [Word8] | |
rleBytes = concatMap (\(n, x) -> [n, x]) . break8 . rle | |
rle :: Eq a => [a] -> [(Int, a)] | |
rle = map len . runs | |
where | |
len (x, xs) = (length xs + 1, x) | |
runs [] = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fmap f x <*> fmap g y | |
(pure f <*> x) <*> (pure g <*> y) -- definition of fmap | |
pure (.) <*> (pure f <*> x) <*> pure g <*> y -- composition | |
pure (.) <*> pure (.) <*> pure f <*> x <*> pure g <*> y -- composition | |
pure ((.) .) <*> pure f <*> x <*> pure g <*> y -- homomorphism | |
pure ((.) . f) <*> x <*> pure g <*> y -- homomorphism | |
pure ($ g) <*> (pure ((.) . f) <*> x) <*> y -- interchange | |
pure (.) <*> pure ($ g) <*> pure ((.) . f) <*> x <*> y -- composition | |
pure (($ g) .) <*> pure ((.) . f) <*> x <*> y -- homomorphism | |
pure (($ g) . ((.) . f)) <*> x <*> y -- homomorphism |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- cabal: | |
build-depends: base, lens, generic-lens | |
-} | |
{-# LANGUAGE DeriveGeneric #-} | |
{-# LANGUAGE OverloadedLabels #-} | |
{-# LANGUAGE RankNTypes #-} | |
import Prelude hiding ((<>)) | |
import Control.Lens |