miso
Copyright(C) 2016-2026 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <code@dmj.io>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellSafe-Inferred
LanguageHaskell2010

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 (model ^. mAlert) $ \msg ->
  div_ [ class_ "alert" ] [ text msg ]
conditionalViews isLoggedIn
  [ button_ [ onClick Logout ] [ 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 to asum)
  • sepBy / sepBy1 — parse a list interleaved with a separator
  • enclosed — parse something between two delimiters (l *> x <* r)
  • between — parse two things separated by a third, returning a pair
  • optionalDefault — parse with a fallback default on failure
  • exists — test whether a combinator succeeds, returning Bool

Miscellaneous

  • (=:) — infix tuple constructor for key-value pairs: "key" =: value
  • compose — forward function composition generalised to any Category: f `compose` g = g . f

See also

Synopsis

Documentation

withFoldable Source #

Arguments

:: Foldable t 
=> t a

Container to map over (e.g. Maybe, [])

-> (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) ]

conditionalViews Source #

Arguments

:: Bool

When True the views are included; when False an empty list is returned

-> [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.

enclosed Source #

Arguments

:: Applicative f 
=> f a

Opening delimiter (e.g. char '(')

-> f b

Closing delimiter (e.g. char ')')

-> 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 ')')

optionalDefault Source #

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")

sepBy1 Source #

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

sepBy Source #

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

between Source #

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

(=:) :: k -> v -> (k, v) Source #

Tuple constructor, useful for constructing key-value pairs.

compose :: Category cat => cat a b -> cat b c -> cat a c Source #

Function composition generalized to Category

test :: Int -> Int
test = (+1) `compose` (+1)