filemanip icon indicating copy to clipboard operation
filemanip copied to clipboard

Monoid instance for `FindClause Bool`

Open dcastro opened this issue 8 years ago • 0 comments

Proposal

I propose we add a monoid instance for FindClause Bool, in a fashion similar to Monoid All and Monoid Any. Here's what I came up with:

newtype AllClause = AllClause { getAllClause :: FindClause Bool }

instance Monoid AllClause where
  mempty = AllClause always
  AllClause x `mappend` AllClause y = AllClause (x &&? y)
newtype AnyClause = AnyClause { getAnyClause :: FindClause Bool }

instance Monoid AnyClause where
  mempty = AnyClause never
  AnyClause x `mappend` AnyClause y = AnyClause (x ||? y)

never :: FindClause Bool
never = return False

Additionally, we could make AllClause and AnyClause derive Generic. In doing so, clients using newtype-generics could easily derive an instance of Newtype for themselves, making packing/unpacking easier.

I'll gladly submit a Pull Request for this, I just wanted to ask the maintainers what they think and get their approval first.

Feedback regarding implementation, naming, standards, etc. is very welcome.

Use case

I thought of this while working on a very small application, where I have a list of filename patterns :: [String] that should be excluded from search results.

With a monoid instance for FindClause Bool, i can do this:

excludePaths :: [String] -> FilterPredicate
excludePaths patterns = getAllClause $ foldMap AllClause $ (filePath /~?) <$> patterns

Using this monoid instance and newtype-generics, I could do this:

instance Newtype AllClause

excludePaths patterns = ala AllClause foldMap $ (filePath /~?) <$> patterns

dcastro avatar Jan 13 '17 13:01 dcastro