Skip to content

Instantly share code, notes, and snippets.

data MappingRow = MappingRow { date :: Date, prod :: Sym, tenor1v :: Sym, tenor2v :: Sym } deriving Show
convertRow :: [MySQLValue] -> IO MappingRow
convertRow sqlRow = eitherStrToMonadError $
-- I have to define some otherwise-useless x just so that I can use guards. Is there a better way?
-- Why is the syntax for guards so restrictive?
let x
| [MySQLInt32U date, MySQLText prod, MySQLInt16U tenor1v, MySQLInt16U tenor2v] <- sqlRow = do
prod' <- maybeToEither "Bad symbol" $ symFromStrMay prod
let (tenor1v', tenor2v') =
class (Eq a, Hashable a, Show a, Typeable a) => Computable a where
toCCode :: a -> CCode
toCCodeMay :: a -> Maybe CCode
toCCodeMay = map Just toCCode
defValue :: a
isDefValue :: a -> Bool
isDefValue = (== defValue)
instance Computable Foo where
...
@jchia
jchia / gist:4eb956c1244b4829eb1910e0cc55dcd6
Last active May 12, 2018 15:22
Which is clearest, foo1, foo2 or foo3? Is there a better way?
import Control.Monad.Except
bar :: MonadIO m => m Int
baz :: Int -> Either String Int
foo1 :: IO ()
foo1 = runExceptT >=> either fail pure $ do
a <- bar
b <- liftEither . baz $ a
@jchia
jchia / gist:9697ba86030ef5aa0cdfca48e2e0cbe6
Created May 10, 2018 06:43
How to get the HasNames instances less manually?
{-# LANGUAGE OverloadedStrings #-}
import ClassyPrelude
import Control.Lens
newtype Name = Name Text deriving (Monoid, Semigroup, IsString, Show)
newtype LongName = LongName Text deriving (IsString, Show)
data Foo1 = Foo1 Int Name Bool deriving Show
@jchia
jchia / oom.hs
Last active May 2, 2018 14:31
Data.Vector isn't smart/lazy enough?
-- This problem actually goes out of memory.
main :: IO ()
main = do
let oom :: Vector Char
oom = V.replicate 1000000000000 'a'
l = length oom
print l
@jchia
jchia / Lib.hs
Created April 7, 2018 15:22
Trying to make instance of HasField
{-# LANGUAGE DataKinds, KindSignatures, FlexibleInstances, MultiParamTypeClasses, TypeApplications #-}
module Lib where
import ClassyPrelude
import GHC.Records
import GHC.TypeLits (Symbol)
instance forall (field :: Symbol) r a. HasField field r a => HasField field (Identity r) a where
getField (Identity x) = getField @field x
{-# LANGUAGE AllowAmbiguousTypes, ConstraintKinds, FlexibleInstances, KindSignatures, RankNTypes, TypeApplications #-}
module Lib3 where
import ClassyPrelude
import Data.Kind (Constraint)
type MyConstraint a = (Show a, Monoid a)
-- The error goes away if I replace 'MyConstraint with Show' on L10,18, but I want to be able to express multiple constraints on L18.
fooF :: forall a. MyConstraint a => a -> String
class FieldPathPart (k :: Symbol) r where
fieldPathPart :: Proxy k -> r -> Maybe PathBuilder
-- I want this instance to be chosen instead of the other one wherever the constraints are met. GHC complains about duplicate instances.
instance {-# OVERLAPPING #-} (Has k v r, B.Binary v, Show v)
=> FieldPathPart (k :: Symbol) r where
fieldPathPart _ r =
let v = get (Proxy :: Proxy k) r
in Just (B.put v, tshow v)
@jchia
jchia / Lib3.hs
Last active March 29, 2018 12:49
‘foo’ is not in the type environment at a reify
{-# LANGUAGE DataKinds, FlexibleInstances, OverloadedLabels, TemplateHaskell, TypeFamilies, TypeOperators #-}
module Lib3 where
import ClassyPrelude
import Language.Haskell.TH
reifyQ :: Name -> Q Exp
reifyQ name = do
r <- reify name
@jchia
jchia / LabelsTH.hs
Created March 28, 2018 16:15
TH for making 'labels' named tuples
{-# LANGUAGE DataKinds, FlexibleInstances, TemplateHaskell, TypeFamilies, TypeOperators #-}
module LabelsTH where
import ClassyPrelude
import Language.Haskell.TH
import Labels ((:=))
typeForFieldname :: String -> Type
typeForFieldname "x" = ConT ''Int