简体   繁体   中英

“mayBeMempty” function for a Semigroup

The following function -

mayBeMempty :: (Eq a, Semigroup a) => a -> a -> Bool
mayBeMempty candidate ref = candidate <> ref == ref

Is a (less efficient) generalization of Data.Set.isSubSetOf . It checks if the first argument is "contained" in the second one and always returns True when the first argument is mempty (when it returns False it is known to not be mempty ).

Does anyone know if this function or concept already exist under some name or even an existing type-class (in which case it would not be less efficient than isSubSetOf )?

I have searched with Hoogle and think there is no pre-implemented version of this.

I don't really see how it could be made more efficient, TBH.

PartialOrd 's leq is what I was looking for.

IIUC, a difference is that it is not designed to fit the rules as I described for Semigroup , but instead it's designed to work in this manner with Lattice which is defined in the same package, which is similar to a semigroup in some ways but better fits things like sets where merging something with itself results in itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM