Skip to content

Instantly share code, notes, and snippets.

NotExist: Doesn't exist
NotPresent: Not submitted
NotNumber t@Text: "#{t}" is not a number
NotLetter t@Text: "#{t}" doesn't only contain letters
NotValidEmail t@Text: "#{t}" is not a valid email address
NotValidName t@Text: "#{t}" is not a valid name
ParsecError t@Text: Syntax error: #{t}
LengthGreater t@Text i@Int: "#{t}" is longer than #{show i} characters
LowerThan ii@Integer: is greater than #{show ii}
Invalid t@Text: #{show t} is invalid
@YoEight
YoEight / Dyna.hs
Last active August 2, 2022 19:44
Computes Fibonacci number with a histomorphism -- correction: Actually it's a dynamorphism as it uses an anamorphism to generate intermediary step
data Cofree f a = a :< (f (Cofree f a))
-- Fix point
newtype Mu f = Mu { unMu :: f (Mu f) }
extract :: Cofree f a -> a
extract (a :< _) = a
-- catamorphism
cata :: Functor f => (f b -> b) -> Mu f -> b
{-# LANGUAGE GADTs, RankNTypes #-}
import Prelude hiding (read, print)
-- The "operational" form
data Action a where
Print :: String -> Action ()
Read :: Action String
{-
@YoEight
YoEight / Code.hs
Created March 9, 2014 08:43
Yoneda lemma example
{-# LANGUAGE GADTs #-}
data Free f a = Done a | Free (f (Free f a))
instance Functor f => Monad (Free f) where
return = Done
Done a >>= f = f a
Free s >>= f = Free $ fmap (f =<<) s
@YoEight
YoEight / Code.hs
Last active August 29, 2015 13:57
My answer to http://codepad.org/roOZpzQQ exercise
import Data.Functor.Foldable
-- | takeStrictlyLessThan take elements of a list whils their sum is
-- _strictly_ less than a given number
--
-- Point-free: I didnt' try without parameter, you can easily "hide" the 2nd
-- parameter (ie. takeStrictlyLessThan x = )
-- Level: MEDIUM
--
-- Examples:
@YoEight
YoEight / SnapAeson.hs
Created March 4, 2014 08:11
Aeson parsing and Snap
import Data.Traversable (for)
import Data.Aeson (Value, json)
import Data.AttoParsec (parseOnly)
import Snap.Core
getPostJsonParam :: MonadSnap m => ByteString -> m (Maybe Value)
getPostJsonParam key = do
bsOpt <- getPostParam key
for bsOpt $ \bs ->
@YoEight
YoEight / Code.hs
Created February 27, 2014 20:54
My answer to http://codepad.org/McxPLp1l exercise
import Data.Functor.Foldable
divIfMultiple :: Integral a => a -> [a] -> Maybe [a]
divIfMultiple x = cata go
where
go Nil = Just []
go (Cons a r)
| mod a x == 0 = fmap (div a x:) r
| otherwise = Nothing
@YoEight
YoEight / process.hs
Created February 9, 2014 18:47
Possible Process Haskell impl.
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE GADTs #-}
module Data.Process where
import Prelude hiding (zipWith)
import Control.Applicative
import Control.Monad
import Data.Foldable
@YoEight
YoEight / gist:8773962
Created February 2, 2014 20:02
I'm doing something wrong
Sun Feb 2 20:44 2014 Time and Allocation Profiling Report (Final)
spellcheck +RTS -p -sstderr -RTS
total time = 0.00 secs (0 ticks @ 1000 us, 1 processor)
total alloc = 90,424 bytes (excludes profiling overheads)
COST CENTRE MODULE %time %alloc
CAF GHC.IO.Handle.FD 0.0 38.4
From d8730a5b72f263754e82c5e18ee53abf0c002b46 Mon Sep 17 00:00:00 2001
From: YoEight <yo.eight@gmail.com>
Date: Sat, 11 Jan 2014 13:30:23 +0100
Subject: [PATCH] Apply changes relative to TH.Pred becoming a TH.Type's
synonym (issue #7021)
---
compiler/deSugar/DsMeta.hs | 53 +++++++++++++++++++----------------------
compiler/hsSyn/Convert.lhs | 16 +++++--------
compiler/typecheck/TcSplice.lhs | 41 +++++++++++++++++++++++--------