Skip to content

Commit 871dffe

Browse files
committed
add eitherRelAbs
also express maybeRel maybeAbs using eitherRelAbs
1 parent 8712dde commit 871dffe

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/Data/Path/Pathy.purs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -318,21 +318,21 @@ maybeFile (ParentIn _) = Nothing
318318
maybeFile (FileIn p f) = (</>) <$> maybeDir p <*> Just (file' f)
319319
maybeFile (DirIn _ _) = Nothing
320320

321+
-- | Determines if the path is relatively or absolutely specified.
322+
eitherRelAbs :: forall a b s. Path a b s -> Either (Path Rel b s) (Path Abs b s)
323+
eitherRelAbs Current = Left Current
324+
eitherRelAbs Root = Right Root
325+
eitherRelAbs (ParentIn p) = bimap (ParentIn) (ParentIn) (eitherRelAbs p)
326+
eitherRelAbs (FileIn p f) = bimap (flip FileIn f) (flip FileIn f) (eitherRelAbs p)
327+
eitherRelAbs (DirIn p d) = bimap (flip DirIn d) (flip DirIn d) (eitherRelAbs p)
328+
321329
-- | Determines if the path is relatively specified.
322330
maybeRel :: forall a b s. Path a b s -> Maybe (Path Rel b s)
323-
maybeRel Current = Just Current
324-
maybeRel Root = Nothing
325-
maybeRel (ParentIn p) = ParentIn <$> maybeRel p
326-
maybeRel (FileIn p f) = flip FileIn f <$> maybeRel p
327-
maybeRel (DirIn p d) = flip DirIn d <$> maybeRel p
331+
maybeRel = eitherRelAbs >>> either Just (const Nothing)
328332

329333
-- | Determines if the path is absolutely specified.
330-
maybeAbs :: forall a b s. Path a b s -> Maybe (Path Rel b s)
331-
maybeAbs Current = Nothing
332-
maybeAbs Root = Just Root
333-
maybeAbs (ParentIn p) = ParentIn <$> maybeAbs p
334-
maybeAbs (FileIn p f) = flip FileIn f <$> maybeAbs p
335-
maybeAbs (DirIn p d) = flip DirIn d <$> maybeAbs p
334+
maybeAbs :: forall a b s. Path a b s -> Maybe (Path Abs b s)
335+
maybeAbs = eitherRelAbs >>> either (const Nothing) Just
336336

337337
-- | Returns the depth of the path. This may be negative in some cases, e.g.
338338
-- | `./../../../` has depth `-3`.

0 commit comments

Comments
 (0)