Simplified implementation of `fold` and `foldable`