Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
Nicer semantics for viewAt
Browse files Browse the repository at this point in the history
  • Loading branch information
FintanH committed Nov 15, 2019
1 parent e18e3c2 commit 04e9edb
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions docs/denotational-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,12 @@ getDirectory :: Browser a Directory
switchHistory :: (History a -> History a) -> Browser a b
μ switchHistory f = modify f

type ViewResult
μ ViewResult = Fail | Success

-- View the history up to a given point
-- Returns Success if @a@ existed in the history and we
-- could successfully set a new history.
--
-- Returns Failure if @a@ was not in the history and
-- we did not modify the history.
viewAt :: Eq a => a -> Browser a ViewResult
μ viewAt a = do
hist <- get
case nonEmpty $ Nel.dropWhile (/= a) hist of
Nothing -> pure Failure
Just h -> setHistory h *> pure Success
-- | Find the suffix of a History.
findSuffix :: Eq a => a -> History a -> Maybe (History a)
μ findSuffix a = nonEmpty . Nel.dropWhile (/= a)

-- View the history up to a given point by supplying a function to modify
-- the state. If this operation fails, then the default value is used.
viewAt :: (History a -> Maybe (History a)) -> History a -> Browser a b
μ viewAt f def = switchHistory (fromMaybe def . f)
```

0 comments on commit 04e9edb

Please sign in to comment.