Standard Library: Attributes & Control flow

copy-attrs


[%copy-attrs]

Pushes a new attribute frame with identical contents to the previous top frame.

count-attrs


[%count-attrs]

int

Prints the current size of the attribute frame stack.

else


[%else]

Marks the next block as conditional and causes it to resolve iff the last block was conditional and its condition evaluated to false.

else-if


[%else-if: condition]

Marks the next block as conditional and causes it to resolve iff the following are true:

  • condition is @true
  • The last block was conditional and its condition evaluated to @false

Parameters

conditionbool
The condition to check.

if


[%if: condition]

Marks the next block as conditional and causes it to resolve iff condition is @true.

mksel


[%mksel: selector-mode]

selector

Creates and returns a selector with the specified mode.

Parameters

selector-modestring
The mode to assign to the created selector.

Options for selector-mode

ModeDescription
randomSelect a random element each time. (default)
oneSelect the same, random element each time.
forwardSelect in a wrapping sequence from first to last.
forward-clampSelect from first to last, then repeat the last element forever.
forward-mirrorSelect from first to last, then last to first, then start over.
reverseSelect in a wrapping reverse sequence from last to first.
reverse-clampSelect from last to first, then repeat the first element forever.
reverse-mirrorSelect in a from last to first, then first to last, then start over.
deckSelect each element once in a random sequence, then reshuffles.
deck-loopSelect each element once in a wrapping random sequence, without reshuffling.
deck-clampSelect each element once in a random sequence, repeating the final element.
deck-mirrorSelect each element once in a random sequence, then repeats the sequence backwards before reshuffling.
pingSelect from first to last, switching directions when a boundary element is reached.
pongSelect from last to first, switching directions when a boundary element is reached.
no-doubleSelect a random element each time, ensuring the same element never occurs twice in a row.

mut


[%mut: mutator-func]

Sets the mutator function for the next block.

The function passed to mutator-func must accept a single parameter (the element callback) in order to run properly.

Parameters

mutator-funcfunction | nothing
The mutator function to assign.

push-attrs


[%push-attrs]

Pushes a new attribute frame onto the attribute frame stack, overriding the previous one.

pop-attrs


[%pop-attrs]

Removes the topmost attribute frame from the attribute frame stack, handing control back to the previous one.

Errors

Attempting to pop the last attribute frame will raise a runtime error.

rep


[%rep: reps]

Sets the repetition count for the next block to reps. The value of reps must either be a non-negative int or one of the special modes listed below.

Parameters

repsint | string
The repetition count or mode to set. If a string, it must match one of the below mode names.

Mode options for reps

Mode nameDescription
onceRun the block only once. (default behavior)
foreverRepeat the next block until explicitly stopped.
allRepeat as many times as there are elements in the next block.

Examples

# Print the fragment 3 times
[rep:3]{ha}
# hahaha
# Print the fragment between 3 and 8 times
[rep:[num:3;8]]{ha}
# hahahaha
# hahahahahahaha
# hahaha
# hahahahaha
# ...
# Print a random card from each suit
[rep: all] 
[sel: deck]
[pipe: [?: e] ({A|2|3|4|5|6|7|8|9|J|Q|K}[e])]
{ ♥ | ♣ | ♠ | ♦ }
# ~> (8♣; 2♦; 6♥; Q♠)

sel


[%sel: selector]

Sets the selector for the next block.

The selector parameter can accept either a selector state object created via [mksel], or a selector mode (in which case it will create a single-use selector of that mode).

Parameters

selectorspecial | string
The selector or selector mode to use. If it's a string, it has to be one of the selector modes accepted by [mksel].

Examples

# Pass in an existing selector object so its state persists between uses
<%fwd = [mksel:forward]>
[sel:<fwd>]
[rep:all]
[sep:\s]
{
  A | B | C | D
}
# Create a temporary selector using [mksel]
[sel:[mksel:forward]]
[rep:all]
[sep:\s]
{
  A | B | C | D
}
# Automatically create a temporary selector from a mode name
[sel:forward]
[rep:all]
[sep:\s]
{
  A | B | C | D
}

sep


[%sep: separator]

Sets the separator value for the next block to separator. The value of separator may be of any type.

Parameters

separatorany
The separator value. If it is a function, it will be called separately for each usage and its return value will be printed.

Examples

# Print comma-separated list of numbers 1-10
[rep:10][sep:,\s]{[step]}

##
  Output:
  1, 2, 3, 4, 5, 6, 7, 8, 9, 10
##
# Print list of numbers 1-10 separated by random sequence of spaces and newlines
[rep:10][sep:[?]{{\n|\s}}]{[step]}

##
  Output:
  1 2 3
  4 5 6 7 8
  9
  10
##