Demonstrate how to get things to magically happen when you import instances of a class.
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 RankNTypes #-} | |
import Data.Foldable (foldl') | |
import Data.Monoid (Any) | |
import Data.Vector (Vector, (!)) | |
import qualified Data.Vector as V | |
import Control.Lens hiding ((:<)) | |
import Control.Lens.Internal.Context (runPretext) | |
import Control.Comonad.Cofree |
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
data Ptree a = Pnode a [Ptree a] deriving (Eq, Show) | |
ptLeaf :: Char -> Ptree Char | |
ptLeaf c = Pnode c [] | |
ptInsert :: String -> Ptree Char -> Ptree Char | |
pInsert [] tree = tree | |
ptInsert (c:cs) (Pnode v children) = case children of | |
[] -> undefined -- the node has no children | |
(current:rest) | v == c -> undefined -- the node has children, and the first one is what we're looking for |
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 LambdaCase, DeriveFunctor, RecursiveDo #-} | |
-- parsing | |
import Text.Earley (Grammar, Prod, | |
parser, fullParses, rule, satisfy, token, list) | |
import Control.Applicative ((<|>), some) | |
import Data.Char (isDigit) | |
-- recursion schemes | |
import Data.Functor.Foldable (Fix(..), cata) |
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 Text.Megaparsec | |
import Text.Megaparsec.Char (space1) | |
import Text.Megaparsec.Char.Lexer (decimal) | |
import Control.Monad (replicateM) | |
import Control.Lens hiding (children) | |
import qualified Data.Vector as V | |
-- data type | |
data Tree = Tree (V.Vector Tree) (V.Vector Int) deriving (Eq, Show, Ord) |
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
~/hask$ cabal v2-repl --build-depends lens --build-depends megaparsec | |
Resolving dependencies... | |
Build profile: -w ghc-8.6.1 -O1 | |
In order, the following will be built (use -v for more details): | |
- fake-package-0 (lib) (first run) | |
Configuring library for fake-package-0.. | |
Preprocessing library for fake-package-0.. | |
Warning: No exposed modules | |
GHCi, version 8.6.1: http://www.haskell.org/ghc/ :? for help | |
Prelude> :load AoC0818.hs |
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 qualified Data.Vector as V | |
import qualified Data.Vector.Unboxed as VU | |
power :: Int -> Int -> Int -> Int | |
power serial x y = hundreds - 5 | |
where | |
hundreds = (multed `div` 100) `mod` 10 | |
multed = rackId * added | |
added = serial + base | |
base = rackId * y |
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 TypeFamilies, GADTs #-} | |
{-# LANGUAGE StandaloneDeriving, EmptyDataDecls #-} | |
data Z | |
data S n | |
data Vec n a where | |
Nil :: Vec Z a | |
Cons :: a -> Vec n a -> Vec (S n) a | |
deriving instance Show a => Show (Vec n a) |
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
-- The ancient symbol consisting of a serpent or dragon devouring its | |
-- own tail. This is the central loop of a BFS implementation that | |
-- used laziness to share the list of enqueued elements for future | |
-- processing with the output list of visited elements. | |
-- | |
-- That implementation detail isn't that important for using it, | |
-- though. The first argument is a processing function that takes the | |
-- current element and state, and returns any number of new elements | |
-- to process along with a new state for processing the next | |
-- element. The second argument is an initial list of elements to |
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 PolyKinds, GADTs, ScopedTypeVariables, TypeApplications #-} | |
module HasochistIntersperse (intersperse) where | |
import Data.Singletons | |
data Between a v = Empty | Has v deriving (Eq, Ord, Show) | |
instance forall k (a :: k) v. | |
(SingI a, SingKind k, v ~ Demote k, Semigroup v) => |
OlderNewer