This page documents Twaddle’s boolean logic functions, if/else, and while loop construct.
Boolean functions evaluate conditions and return either "1" (true) or "0"
(false). They are designed to work seamlessly with while loops
and Twaddle’s if/else functionality, but can also be used standalone within regular Twaddle sentences.
Many of the boolean functions accept arguments and convert them to boolean values using the following rules:
"5", "-3",
"2.5"), it is considered true if and only if the numeric value is greater
than zero. Zero and negative numbers are false.Examples:
"5" → true (positive number)"0" → false (zero)"-3" → false (negative number)"hello" → true (non-empty string)"" → false (empty string)Evaluates the truthiness of a single argument using the bool conversion rules described above.
Syntax:
[bool:<value>]
Arguments:
value — any string or numeric valueReturns: "1" if true, "0" if false
Examples:
[bool:5] → "1"
[bool:0] → "0"
[bool:-3] → "0"
[bool:hello] → "1"
[bool:] → "0"
These functions perform numeric comparisons. Both arguments must be parseable
as numbers, otherwise a TwaddleFunctionException is raised.
Syntax:
[less_than:<x>;<y>]
[lt:<x>;<y>]
Arguments:
x — numeric valuey — numeric valueReturns: "1" if x < y, otherwise "0"
Examples:
[less_than:2;5] → "1"
[less_than:5;2] → "0"
[less_than:5;5] → "0"
[less_than:-10;-5] → "1"
Syntax:
[greater_than:<x>;<y>]
[gt:<x>;<y>]
Arguments:
x — numeric valuey — numeric valueReturns: "1" if x > y, otherwise "0"
Examples:
[greater_than:5;2] → "1"
[greater_than:2;5] → "0"
[greater_than:5;5] → "0"
[greater_than:-5;-10] → "1"
Performs comparison based on whether both arguments are numeric:
Syntax:
[equal_to:<x>;<y>]
[eq:<x>;<y>]
Arguments:
x — any valuey — any valueReturns: "1" if equal, otherwise "0"
Examples:
[equal_to:5;5] → "1"
[equal_to:5;3] → "0"
[equal_to:hello;hello] → "1"
[equal_to:hello;world] → "0"
[equal_to:5;hello] → "0"
[equal_to:5;5.000] → "1"
These functions perform logical operations on their arguments, converting them to boolean values using the bool conversion rules.
Syntax:
[and:<x>;<y>]
Arguments:
x — any valuey — any valueReturns: "1" if both are true, otherwise "0"
Examples:
[and:1;1] → "1"
[and:1;0] → "0"
[and:5;hello] → "1"
[and:0;5] → "0"
Syntax:
[or:<x>;<y>]
Arguments:
x — any valuey — any valueReturns: "1" if at least one is true, otherwise "0"
Examples:
[or:1;1] → "1"
[or:1;0] → "1"
[or:0;0] → "0"
[or:0;5] → "1"
Syntax:
[xor:<x>;<y>]
Arguments:
x — any valuey — any valueReturns: "1" if exactly one is true, otherwise "0"
Examples:
[xor:1;1] → "0"
[xor:1;0] → "1"
[xor:0;0] → "0"
[xor:5;hello] → "0"
[xor:0;hello] → "1"
Syntax:
[not:<x>]
Arguments:
x — any valueReturns: "1" if false, "0" if true (inverts the bool conversion)
Examples:
[not:1] → "0"
[not:0] → "1"
[not:5] → "0"
[not:] → "1"
[not:hello] → "0"
While loops repeatedly execute a block as long as a given predicate evaluates to true
or until a maximum number of iterations is reached. Twaddle is a text-templating tool
which happens to include some basic looping and boolean logic functionality, it is not
designed for extensive calculations and running while loops with complex predicates
may result in poor performance. As such, the default maximum iterations is set relatively
low: 100.
Syntax:
[while:<predicate>;<max_iterations>]{<block contents>}
Arguments:
predicate — a boolean expression (typically using one of the boolean functions)max_iterations - optional the maximum number of iterations to run. must be
parseable as an int. Defaults to 100 if not set.block contents — the block to repeatBehaviour:
max_iterations times.max_iterations is 0 the block is never executedExamples:
Count from 1 to 5:
[copy:counter]{1}[while:[less_than:[paste:counter];5]]{[copy:counter]{ [add:[paste:counter];1]}}
Output: 1 2 3 4 5
Repeat until a condition is met:
[while:[not:[eq:<country-south_america::my_country>;Brazil]]]{<country-south_america::^=my_country> }
Output: repeatedly prints random South American countries until the first time Brazil is chosen.
Twaddle’s if function accepts any of the boolean predicates above, or implicitly
converts a standard Twaddle pattern according to the same rules as stated above for
boolean conversion. It accepts two mandatory and one optional
argument:
Syntax:
[if:<predicate>;<if-branch>;<else-branch>]
Arguments:
predicate - a boolean expression, implicitly converted if necessaryif-branch - a Twaddle pattern to run if the predicate is trueelse-branch (optional) - a Twaddle pattern to run if the predicate is falseExamples:
[if:[less_than:3;5];3 is smaller than 5;3 is bigger than 5]
Output: 3 is smaller than 5
[if:[eq:3;5];3 is equal to 5;3 is not equal to 5]
Output: 3 is not equal to 5
[hide]{[copy:i]{0}}[while:[less_than:[paste:i];5]]{[if:[eq:[paste:i];2];2;x][hide]{[copy:i]{[add:[paste:i];1]}}}
Output: xx2xx
Boolean functions can be nested and combined to create complex predicates:
...[while:[and:[less_than:[paste:x];10];[greater_than:[copy:y];0]]]{
...
}
This loop continues while x < 10 AND y > 0 (note that x and y must be copied
already with some numeric value so they can be loaded and used in the less_than and
greater_than functions on the first iteration). The same principle can also be applied
to if predicates.