sanctuary-js / sanctuary

:see_no_evil: Refuge from unsafe JavaScript
https://sanctuary.js.org
MIT License
3.04k stars 94 forks source link

Check for empty structures? #721

Open JuanGalilea opened 2 years ago

JuanGalilea commented 2 years ago

Can't seem to find any functions that check if a structure is empty.

Though it can be implemented by composing isNothing with head but this is O(n) on any structure other than array.

While i can't figure out a O(1) version of isNothing, or head for that matter, an alternative empty checker could be implemented this other way:

// isEmpty :: (Monoid a, Setoid a) => a -> Boolean
const isEmpty = (a) => equals (a) (empty(a))

This is different from the standard implementation, as it doesn't even require it to be a foldable, but provides arguably the same functionality (at least on most structures)

Either implementation would be nice to have IMO.

davidchambers commented 2 years ago

I agree that either of these functions would be a worthwhile addition. The monoidal function seems more natural to me.