| Copyright | (C) 2016-2026 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Miso.Util
Description
Overview
Miso.Util provides general-purpose combinators shared across miso's internal modules and available to application code. It is re-exported by Miso.
View helpers
withFoldable—mapover anyFoldableto produce a list of views; particularly handy forMaybe:
withFoldable(model ^. mAlert) $ \msg ->div_[class_"alert" ] [textmsg ]
conditionalViews— include a list of views only when a condition isTrue; returns[]otherwise:
conditionalViewsisLoggedIn [button_[onClickLogout ] [text"Log out" ] ]
Parser / lexer combinators
These Alternative-polymorphic combinators work
with both Lexer and Parser:
oneOf— try alternatives in order, succeeding on the first match (analogous toasum)sepBy/sepBy1— parse a list interleaved with a separatorenclosed— parse something between two delimiters (l *> x <* r)between— parse two things separated by a third, returning a pairoptionalDefault— parse with a fallback default on failureexists— test whether a combinator succeeds, returningBool
Miscellaneous
(=:)— infix tuple constructor for key-value pairs:"key"=:valuecompose— forward function composition generalised to anyCategory:f `compose` g = g . f
See also
- Miso.Util.Lexer — the
Lexercombinator library - Miso.Util.Parser — the
Parsercombinator library
Synopsis
- withFoldable :: Foldable t => t a -> (a -> b) -> [b]
- conditionalViews :: Bool -> [view] -> [view]
- oneOf :: Alternative f => [f a] -> f a
- enclosed :: Applicative f => f a -> f b -> f c -> f c
- optionalDefault :: Alternative f => b -> f b -> f b
- exists :: Alternative f => f a -> f Bool
- sepBy1 :: Alternative m => m sep -> m a -> m [a]
- sepBy :: Alternative m => m sep -> m a -> m [a]
- between :: Applicative f => f a -> f b -> f c -> f (b, c)
- (=:) :: k -> v -> (k, v)
- compose :: Category cat => cat a b -> cat b c -> cat a c
Documentation
Arguments
| :: Foldable t | |
| => t a | Container to map over (e.g. |
| -> (a -> b) | Function to apply to each element |
| -> [b] |
Generic map function, useful for creating Views from the elements of
some Foldable. Particularly handy for Maybe, as shown in the example
below.
view model =
div_ [] $
withFoldable (model ^. mSomeMaybeVal) $ \someVal ->
p_ [] [ text $ "Hey, look at this value: " <> ms (show someVal) ]
Arguments
| :: Bool | When |
| -> [view] | Views to include conditionally |
| -> [view] |
Conditionally includes views.
Hides the Views if the condition is False. Shows them when the condition
is True.
oneOf :: Alternative f => [f a] -> f a Source #
Selects the first Alternative, analogous to asum.
Arguments
| :: Applicative f | |
| => f a | Opening delimiter (e.g. |
| -> f b | Closing delimiter (e.g. |
| -> f c | Inner parser/lexer whose result is returned |
| -> f c |
Convenience function for constructing parser / lexer combinators.
test :: Parser a -> Parser a
test = enclosed (char '(') (char ')')
Arguments
| :: Alternative f | |
| => b | Default value to use when the parser/lexer fails |
| -> f b | Parser/lexer to attempt |
| -> f b |
Allow the specification of default values during parsing / lexing in the case of parser / lexer failure.
test :: Parser MisoString test = optionalDefault "foo" (string "bar")
exists :: Alternative f => f a -> f Bool Source #
Combinator for testing parsing / lexing failure on any input.
test :: Parser Bool test = exists (string "foo")
Arguments
| :: Alternative m | |
| => m sep | Separator parser/lexer (result discarded) |
| -> m a | Element parser/lexer |
| -> m [a] |
Interleaves one parser combinator with another, must have at least one successful parse.
test :: Parser [Int] test = sepBy1 (char ',') number
Arguments
| :: Alternative m | |
| => m sep | Separator parser/lexer (result discarded) |
| -> m a | Element parser/lexer |
| -> m [a] |
Interleaves one parser combinator with another, may not have any successful parses.
test :: Parser [Int] test = sepBy (char ',') number
Arguments
| :: Applicative f | |
| => f a | Separator between the two elements (result discarded) |
| -> f b | Left element parser/lexer |
| -> f c | Right element parser/lexer |
| -> f (b, c) |
Successfully parses the arguments between another combinator
test :: Parser (Int, Int)
test = between (char *) number number
-- 5*5