Skip to content

Instantly share code, notes, and snippets.

@beckyconning
Created August 28, 2015 16:40
Show Gist options
  • Save beckyconning/228a44cd87d50bb82df7 to your computer and use it in GitHub Desktop.
Save beckyconning/228a44cd87d50bb82df7 to your computer and use it in GitHub Desktop.
newtype AtLeastTwoAndUnique a = AtLeastTwoAndUnique (Array a)
toArray :: forall a. AtLeastTwoAndUnique a -> Array a
toArray (AtLeastTwoAndUnique xs) = xs
first :: forall a. AtLeastTwoAndUnique a -> a
first = Unsafe.head
instance arbAtLeastTwoAndUnique :: (Arbitrary a) => Arbitrary (AtLeastTwoAndUnique a) where
arbitrary = AtLeastTwoAndUnique <$> arbitrary `suchThat` (\xs -> unique xs && atLeastTwo xs)
where
unique xs = nub xs == xs
atLeastTwo = (>= 2) <<< length
newtype AtLeastTwoAndUnique a = AtLeastTwoAndUnique (Tuple (Tuple a a) (Array a))
toArray :: forall a. AtLeastTwoAndUnique a -> Array a
toArray (AtLeastTwoAndUnique (Tuple (Tuple x1 x2) xs)) = x1 : x2 : xs
first :: forall a. AtLeastTwoAndUnique a -> a
first (AtLeastTwoAndUnique (Tuple tuple _)) = fst tuple
instance arbAtLeastTwoAndUnique :: (Arbitrary a, Ord a, Show a) => Arbitrary (AtLeastTwoAndUnique a) where
arbitrary = AtLeastTwoAndUnique <$> arbitrary `suchThat` unique
where
unique (Tuple (Tuple x1 x2) xs) = nub (x1 : x2 :xs) == (x1 : x2 : xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment