purescript-datetime icon indicating copy to clipboard operation
purescript-datetime copied to clipboard

Add a saturating `adjust` variant?

Open potocpav opened this issue 3 years ago • 0 comments

While writing an app with lots of DateTime manipulations, I noticed that adjust is very annoying to use, because of the Maybe return type. I don't think this is a very practical design, similarly to how (+) :: Int -> Int -> Maybe Int wouldn't be. There is often not much you can do on failure, other than just skip the operation. Needless to say, that doesn't help with correctness.

I think that saturating semantics would make more sense. Something like this:

-- | Like `adjust`, but saturates on out-of-bounds results.
shift :: forall d. Duration d => d -> DateTime -> DateTime
shift d dt = case adjust d dt of
    Nothing -> if fromDuration d > mempty then top else bottom
    Just d' -> d'

Does this sound like a good idea? I can make it into a PR, but wanted to get some feedback first.

potocpav avatar Mar 03 '21 14:03 potocpav