Skip to content

Instantly share code, notes, and snippets.

View dmwit's full-sized avatar

Daniel Wagner dmwit

View GitHub Profile
@dmwit
dmwit / typeappvsproxy.hs
Last active December 23, 2019 19:07
TypeApplications how?
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
data Nat = O | S Nat
type family xs ++ ys where
< dmwit> Okay, so "graphical" characters are allowed, which means letters (lower- and upper-case), symbols, punctuation, decimal digits, and a handful of one-off exceptions.
< dmwit> PrivateUse doesn't qualify as any of those categories. (And you can use generalCategory to check any particular character you want to know about.)
< dmwit> The relevant production in the Report is "graphic" at https://www.haskell.org/onlinereport/haskell2010/haskellch2.html#x7-160002.2
< geekosaur> yeh. I actually consider this a ghc wart; it should allow PrivateUse in literals. I end up using escapes in my config
< dmwit> > nub $ generalCategory <$> [minBound..]
<+lambdabot> [Control,Space,OtherPunctuation,CurrencySymbol,OpenPunctuation,ClosePunctuat...
< geekosaur> maybe a Haskell wart if the Report specs this; there's no good reason a Char or String literal shouldn't allow any valid Unicode character
< geekosaur> (note that this does not mean allowing PrivateUse anywhere else in a lexical Haskell setting, ecept maybe comments
find fp = do
entry <- entityFromFile fp
mold <- lookupEntity (entityHash entry)
pure $ case mold of
Nothing -> ImportAdded entry
Just old | entityPath old == entityPath entry -> ImportUpToDate entry
_ -> ImportMoved old entry
act (ImportAdded entry) = do
dbPutEntity entry
foldb1 op = foldb' where
pairwise (x:y:rest) = op x y : pairwise rest
pairwise shortList = shortList
foldb' xs@(_:_:_) = foldb' (pairwise xs)
foldb' [x] = x
foldb op def xs = foldb1 op (def:xs)
{-# LANGUAGE DeriveFunctor #-}
import Control.Monad.Zip
data Zip f a = P a | V (f a)
deriving (Eq, Ord, Read, Show, Functor)
instance MonadZip f => Applicative (Zip f) where
pure = P
P f <*> P x = P (f x)
P f <*> V xs = V (f <$> xs)
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE TypeFamilies #-}
import GHC.Types
class Foo f where
type Ctx f i :: Constraint
extract :: Ctx f i => i -> f i -> String
newtype Los i = Los [String] deriving (Eq, Ord, Read, Show)
(.) :: (b -> c) -> (a -> b ) -> (a -> c)
(,) :: d -> (e -> (d,e))
so a ~ d, and b ~ e -> (d,e), and
\x -> x . (,) :: ((e -> (d,e)) -> c) -> (d -> c)
newtype A = A Int
newtype B = B Int
type Database = String
type JSON = String
class ParseDatabase a where parse :: Database -> Maybe a
instance ParseDatabase A where parse db = A <$> readMaybe s
instance ParseDatabase B where parse db = B <$> readMaybe s
@dmwit
dmwit / idklol
Created September 28, 2019 03:15
derangements :: Integer -> Integer
derangements n = id
. sum
. zipWith (*) signs
. scanl (*) 1
$ [n,n-1..1]
where
signs = drop (n .&. 1) (cycle [1,-1])
c (1, 1) = 1
c (2, 2) = 1
c (i, 1) | i > 2 = -(a ! (i-2, 1))
c (i, j) | i == j = 2 * (a ! (i-1, j-1))
| i > j && j > 1 = 2 * (a ! (i-1, j-1)) - (a ! (i-2, j-1))
| otherwise = 0