Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
model history and push to it on any changes
- Loading branch information
1 parent
dd27284
commit e2a5b7f
Showing
2 changed files
with
79 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module History exposing (History, current, goBack, init, mapPush, push) | ||
|
||
{-| -} | ||
|
||
|
||
{-| A linear undo history. Basically works like a list zipper but drops | ||
items older than the retention limit. | ||
-} | ||
type History a | ||
= History | ||
{ retention : Int | ||
, past : List a | ||
, current : a | ||
, future : List a | ||
} | ||
|
||
|
||
init : Int -> a -> History a | ||
init retention initial = | ||
History | ||
{ retention = retention | ||
, past = [] | ||
, current = initial | ||
, future = [] | ||
} | ||
|
||
|
||
current : History a -> a | ||
current (History guts) = | ||
guts.current | ||
|
||
|
||
push : a -> History a -> History a | ||
push a (History guts) = | ||
History | ||
{ guts | ||
| past = List.take guts.retention (guts.current :: guts.past) | ||
, current = a | ||
, future = [] | ||
} | ||
|
||
|
||
mapPush : (a -> a) -> History a -> History a | ||
mapPush fn history = | ||
push (fn (current history)) history | ||
|
||
|
||
goBack : History a -> Maybe (History a) | ||
goBack (History guts) = | ||
case guts.past of | ||
mostRecent :: rest -> | ||
(Just << History) | ||
{ guts | ||
| past = rest | ||
, current = mostRecent | ||
, future = guts.current :: guts.future | ||
} | ||
|
||
[] -> | ||
Nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters