jsmw-0.1: Javascript Monadic Writer base package.





Encoding of Javascript conditionals.



The following functions help encode the Javascript switch and case statements. The Switchable class seen in the type signatures, and its necessary instances are defined internally by this module. The switch statement can be encoded for numeric, boolean, and string scrutinees.

Unlike Javascript switch, a value can be returned from JSMW switch (see the second example). All expressions matching case labels must return values of the same type.

Below are examples of switch statements encoded in JSMW.

  let p = number 5 - (number 1 - number 4)
  switch p $ do
    5 --> alert (string "This is Five")
    8 --> alert (string "This is Eight")
    none $ toString p >>= alert  
  n2 <- switch ctrl $ do
    True --> return (n - number 1)
    False --> return (n + number 1)

switch :: (Switchable s r, CElement c) => Expression r -> Switch s r c e a -> JSMW c (Expression e)Source

Encode a switch statement.

(-->) :: (Switchable s r, CElement c) => s -> JSMW c (Expression e) -> Switch s r c e ()Source

Encode a case label. The first (left) argument is a literal describing the value of the label. Note that the left argument must be a Haskell literal, not a Javascript expression. In other words, for boolean labels, use True rather than true. The second (right) argument is a JSMW monadic expression matching the label. Break statements are inserted automatically (that is, fall-through case labels are not permitted).

none :: (Switchable s r, CElement c) => JSMW c (Expression e) -> Switch s r c e ()Source

Encode a default: case label, that is, what action should be taken if none of the case labels matches the scrutinee.

In both none and -->, JSMW monadic expression should be of the same type. Also note that if no case label matches the scrutinee value, and no default label has been defined, an exception will be thrown showing the scrutinee name that did not match.