Skip to content

Instantly share code, notes, and snippets.

@TrevorBasinger
Last active August 29, 2015 14:10
Show Gist options
  • Save TrevorBasinger/0458b94873b7f3f0cb3b to your computer and use it in GitHub Desktop.
Save TrevorBasinger/0458b94873b7f3f0cb3b to your computer and use it in GitHub Desktop.
module Data.Hashable where
import Data.Maybe
import Data.Tuple
import Data.Either
import Data.String
import Data.Char
import Data.Function
import Data.Foldable
import Control.Plus
type HashCode = Number
class (Eq a) <= Hashable a where
hash :: a -> HashCode
newtype Complex = Complex { real :: Number, imaginary :: Number }
instance showComplex :: Show Complex where
show (Complex c) = "(Real: " ++ show c.real ++ ", Imaginary: " ++ show c.imaginary ++ ")"
instance eqComplex :: Eq Complex where
(==) (Complex a) (Complex b) = a.real == b.real && a.imaginary == b.imaginary
(/=) a b = not (a == b)
data NonEmpty a = NonEmpty a [a]
instance eqNonEmpty :: (Eq a) => Eq (NonEmpty a) where
(==) (NonEmpty l ls) (NonEmpty r rs) = l == r && ls == rs
(/=) l r = not (l == r)
data Extended a = Finite a | Infinite
instance eqExtended :: (Eq a) => Eq (Extended a) where
(==) (Finite a) (Finite b) = a == b
(/=) a b = not (a == b)
instance ordExteneded :: (Ord a) => Ord (Extended a) where
compare (Finite a) (Finite b) = compare a b
compare Infinite Infinite = EQ
compare _ Infinite = LT
compare Infinite _ = GT
instance showExtended :: (Show a) => Show (Extended a) where
show (Finite a) = "Finite " ++ show a
show Infinite = "Infinite"
data OneMore f a = OneMore a (f a)
instance plusOneMore :: (Semigroup a, Semigroup (f a)) => Semigroup (OneMore f a) where
(<>) (OneMore x xs) (OneMore y ys) = (OneMore (x <> y) (xs <> ys))
instance foldableOneMore :: (Foldable f) => Foldable (OneMore f) where
foldr f b (OneMore x xs) = f x (foldr f b xs)
foldl f b (OneMore x xs) = f (foldl f b xs) x
foldMap f (OneMore x xs) = f x <> foldMap f xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment