Skip to content

Instantly share code, notes, and snippets.

@bradparker
bradparker / Balance.hs
Created December 5, 2018 23:17
Wrong answer ... but fun
data Balance =
Balance !Int
!Int
deriving (Eq, Show)
instance Semigroup Balance where
Balance a b <> Balance c d
| b <= c = Balance (a + c - b) d
| otherwise = Balance a (d + b - c)
@bradparker
bradparker / ExpandMap.hs
Created December 4, 2018 22:42
Expanding int maps
module ExpandMap
( expandIntMap
) where
import Control.Monad.State (evalState, get, put)
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.Traversable (for)
-- >>> expandIntMap '0' 50 (IntMap.fromList (zip [0,5..50] ['a' .. 'z']))
@bradparker
bradparker / Main.hs
Created November 26, 2018 10:32
Ok, let's have a crack at learning FRP
-- To run:
-- $ nix-shell -p "haskellPackages.ghcWithPackages (ghc: [ghc.reactive-banana])" --run "runhaskell ./Main.hs"
module Main where
import Control.Concurrent (threadDelay)
import Control.Event.Handler (newAddHandler)
import Control.Monad (replicateM_)
import Reactive.Banana.Combinators
( Event
@bradparker
bradparker / List.hs
Last active November 14, 2018 03:24
Foldr all the way
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
module List where
import Data.Bool (Bool)
import Data.Function (const)
import Data.Ord ((<=))
import GHC.Int (Int)
import GHC.Num ((-))
@bradparker
bradparker / BankersQueue.hs
Created November 8, 2018 10:15
Okasaki notes
module BankersQueue where
import Prelude hiding (head, tail)
data Queue a =
Queue [a] !Int [a] !Int
deriving (Show)
empty :: Queue a
empty = Queue [] 0 [] 0
@bradparker
bradparker / Main.hs
Created October 24, 2018 11:48
Haskell state machines -- impossible states unrepresentable :D
{-# LANGUAGE GADTs #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wall #-}
module Main where
import Data.Function ((&))
data Locked
@bradparker
bradparker / Constrained.hs
Last active October 16, 2018 09:22
Constrained
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.Constrained
( Constrained(unconstrained)
, constrain
) where
import Data.Bool ((&&), otherwise)
@bradparker
bradparker / Sized.hs
Created October 15, 2018 12:14
Playing around with length indexed text
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ExplicitNamespaces #-}
{-# LANGUAGE TypeOperators #-}
module Data.Text.Sized
( Text
@bradparker
bradparker / Main.hs
Last active November 21, 2018 22:10
Essence of the iterator pattern word count
module Main where
import Data.Bool (bool)
import Control.Monad ((<=<))
import Control.Monad.State (State, evalState, get, put)
import Data.Char (isSpace)
import Data.Foldable (traverse_)
import Data.Functor.Compose (Compose(Compose, getCompose))
import Data.Functor.Const (Const(Const, getConst))
import Data.Functor.Product (Product(Pair))
@bradparker
bradparker / Main.hs
Last active February 8, 2019 05:36
Parser
module Main where
import Data.Functor (void)
import Control.Applicative ((<|>), empty, many, some, optional)
import Control.Monad (msum, replicateM)
import Control.Monad.State (StateT(runStateT))
import Control.Monad.State.Class (get, put)
import Data.Char (isHexDigit, chr, isDigit, isSpace)
import Data.Map (Map)
import Data.Maybe (listToMaybe, fromMaybe)