Skip to content

Instantly share code, notes, and snippets.

View frasertweedale's full-sized avatar

Fraser Tweedale frasertweedale

View GitHub Profile
@frasertweedale
frasertweedale / news-2018-02.rst
Last active February 13, 2018 05:07
BFPG news 2018-02
@frasertweedale
frasertweedale / create-empty-branch.sh
Created January 5, 2018 02:37
Git: create an empty, orphan branch, in the background (e.g. gh-pages)
[ -z "$1" ] && echo "usage: $0 BRANCH_NAME" && exit 1
export GIT_INDEX_FILE=$PWD/.git/index-empty
REF=refs/heads/$1
git read-tree --empty
TREE=$(git write-tree)
COMMIT=$(git commit-tree "$TREE" -m "new empty orphan branch")
git update-ref "$REF" "$COMMIT"
@frasertweedale
frasertweedale / Foo.hs
Last active October 20, 2017 04:17
switching mode phantom
{-# LANGUAGE ScopedTypeVariables #-}
class HasMode a where
mode :: Proxy a -> Mode
instance HasMode 'BrowseMail where
mode _ = BrowseMail
-- | instantiate this class to specify a valid mode transition
class ModeTransition a b where
@frasertweedale
frasertweedale / news.rst
Last active October 9, 2017 02:02
BFPG news 2017-10
@frasertweedale
frasertweedale / ghci-transcript.txt
Created August 2, 2017 00:49
Parsing ranges with Attoparsec
λ> let offset = T.Parser $ \t pos more lose succ -> succ t pos more (T.fromPos pos)
λ> data Range = Range Int Int deriving (Eq, Show)
λ> let skipTill s = manyTill anyWord8 (lookAhead $ string s) *> pure ()
λ> let range s = Range <$> offset <* skipTill s <*> offset
λ> parseOnly (range "--") "....--"
Right (Range 0 4)
λ> parseOnly (range "--" <* string "--") "....--"
Right (Range 0 4)
λ> parseOnly (range "--" <* string "--") "....--..."
Right (Range 0 4)
@frasertweedale
frasertweedale / Newton.hs
Last active March 2, 2019 14:59
Newton's method (Haskell)
{-# LANGUAGE RankNTypes #-}
import Numeric.Natural (Natural)
import Numeric.AD (diff)
newtonRoot
:: (Floating a, Ord a)
=> Natural -- ^ iterations
-> a -- ^ epsilon
-> a -- ^ starting guess
@frasertweedale
frasertweedale / optparse_demo.py
Created July 25, 2017 06:45
optparse 'constructor' option demo
from copy import copy
from optparse import Option, OptionParser, OptionValueError
class Stars(object):
def __init__(self, stars):
stars = float(stars)
if stars < 0.5 or stars > 5 or stars % 0.5 != 0:
raise ValueError(
"Stars must be a whole or half number between 0.5 and 5")
@frasertweedale
frasertweedale / Semicategory.hs
Last active June 21, 2017 08:12
Semicategory (partial semigroup)
import Data.Semigroup
import Data.List.NonEmpty
import Data.Semigroup.Foldable
import Test.QuickCheck
data Partial a = Undefined | Defined a
deriving (Show)
class Semicategory m where
(<>?) :: m -> m -> Maybe m
@frasertweedale
frasertweedale / news.rst
Last active June 14, 2017 04:45
BFPG news; 2017-06-13