Skip to content

Instantly share code, notes, and snippets.

@cheecheeo
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save cheecheeo/a37dd840f0356c30a795 to your computer and use it in GitHub Desktop.

Select an option

Save cheecheeo/a37dd840f0356c30a795 to your computer and use it in GitHub Desktop.

Revisions

  1. cheecheeo revised this gist Sep 24, 2014. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions FoundIt.hs
    Original file line number Diff line number Diff line change
    @@ -17,10 +17,10 @@ mkFoundItTriplet a x y z =

    -- | @FoundIt a@ is a semigroup
    --
    -- prop> \a b c -> let (x, y, z) = mkFoundItTriplet a a b c in x <> (y <> z) == (x <> y) <> z
    -- prop> \a b c -> let (x, y, z) = mkFoundItTriplet b a b c in x <> (y <> z) == (x <> y) <> z
    -- prop> \a b c -> let (x, y, z) = mkFoundItTriplet c a b c in x <> (y <> z) == (x <> y) <> z
    -- prop> \a b c d -> let (x, y, z) = mkFoundItTriplet d a b c in x <> (y <> z) == (x <> y) <> z
    -- prop> \a b c -> let (x, y, z) = mkFoundItTriplet a a b c in x <> (y <> z) == (x <> y) <> (z :: FoundIt Integer)
    -- prop> \a b c -> let (x, y, z) = mkFoundItTriplet b a b c in x <> (y <> z) == (x <> y) <> (z :: FoundIt Integer)
    -- prop> \a b c -> let (x, y, z) = mkFoundItTriplet c a b c in x <> (y <> z) == (x <> y) <> (z :: FoundIt Integer)
    -- prop> \a b c d -> let (x, y, z) = mkFoundItTriplet d a b c in x <> (y <> z) == (x <> y) <> (z :: FoundIt Integer)
    instance Semigroup (FoundIt a) where
    f1 <> f2 = if getFoundIt f1 then f1 else f2

  2. cheecheeo revised this gist Sep 24, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion FoundIt.hs
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ mkFoundItTriplet :: (Eq a) => a -> a -> a -> a -> (FoundIt a, FoundIt a, FoundIt
    mkFoundItTriplet a x y z =
    (FoundIt (== a) x, FoundIt (== a) y, FoundIt (== a) z)

    -- | FoundIt is a semigroup
    -- | @FoundIt a@ is a semigroup
    --
    -- prop> \a b c -> let (x, y, z) = mkFoundItTriplet a a b c in x <> (y <> z) == (x <> y) <> z
    -- prop> \a b c -> let (x, y, z) = mkFoundItTriplet b a b c in x <> (y <> z) == (x <> y) <> z
  3. cheecheeo created this gist Sep 24, 2014.
    45 changes: 45 additions & 0 deletions FoundIt.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    module FoundIt (FoundIt.elem) where

    import Data.Semigroup
    import Data.List.NonEmpty (NonEmpty(..))

    data FoundIt a = FoundIt (a -> Bool) a

    instance (Eq a) => Eq (FoundIt a) where
    f1 == f2 =
    case (f1, f2) of
    (FoundIt _ x1, FoundIt _ x2) ->
    x1 == x2 && getFoundIt f1 == getFoundIt f2

    mkFoundItTriplet :: (Eq a) => a -> a -> a -> a -> (FoundIt a, FoundIt a, FoundIt a)
    mkFoundItTriplet a x y z =
    (FoundIt (== a) x, FoundIt (== a) y, FoundIt (== a) z)

    -- | FoundIt is a semigroup
    --
    -- prop> \a b c -> let (x, y, z) = mkFoundItTriplet a a b c in x <> (y <> z) == (x <> y) <> z
    -- prop> \a b c -> let (x, y, z) = mkFoundItTriplet b a b c in x <> (y <> z) == (x <> y) <> z
    -- prop> \a b c -> let (x, y, z) = mkFoundItTriplet c a b c in x <> (y <> z) == (x <> y) <> z
    -- prop> \a b c d -> let (x, y, z) = mkFoundItTriplet d a b c in x <> (y <> z) == (x <> y) <> z
    instance Semigroup (FoundIt a) where
    f1 <> f2 = if getFoundIt f1 then f1 else f2

    getFoundIt :: FoundIt a -> Bool
    getFoundIt f =
    case f of
    FoundIt p x -> p x

    -- | Is @findMe@ an element of @xs'@?
    -- >>> FoundIt.elem 5 $ [1..10]
    -- True
    -- >>> FoundIt.elem 5 $ [1..4]
    -- False
    -- >>> FoundIt.elem 42 $ [1..4]
    -- False
    -- >>> FoundIt.elem 42 $ [42]
    -- True
    elem :: (Eq a) => a -> [a] -> Bool
    elem findMe xs' =
    case xs' of
    [] -> False
    (x:xs) -> getFoundIt . sconcat . fmap (FoundIt (== findMe)) $ x :| xs