Skip to content

Clean up NonEmpty #457

@treeowl

Description

@treeowl

Currently, the Arbitrary instance for NonEmpty generates a nonempty list and then converts it to a NonEmpty:

  liftArbitrary arb = NonEmpty.fromList <$> listOf1 arb

This isn't very nice, since it uses a partial function unnecessarily. We could do this instead:

  liftArbitrary = nonEmptyListOf
 
nonEmptyListOf :: Gen a -> Gen (NonEmpty a)
nonEmptyListOf gen = sized $ \n -> do
  k <- chooseInt (0, 0 `max` (n - 1))
  liftA2 (:|) gen (vectorOf k gen)

-- To avoid duplication, we could redefine listOf1:
listOf1 :: Gen a -> Gen [a]
listOf1 = fmap toList . nonEmptyListOf

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions