{sequence}

Generates a range, a list or one of 80 mathematical sequences, and iterates its values.

Sequences Either form

Syntax

{sequence '1..10'} ... {/sequence}
{sequence range='1..100', increment=5} ... {/sequence}
{sequence list='3;1;2'} ... {/sequence}
{sequence prime, rows=10, name='p'} ... {/sequence}
{sequence fibonacci, from=1, to=20, stop=100, skip=2} ... {/sequence}
{sequence random, minimal=1, maximal=100, rows=5, unique, randomly} ... {/sequence}
{sequence '1..20', keep, prime, sum} ... {/sequence}
{sequence '1..20', keep='prime', action='first|3'} ... {/sequence}
{sequence prime, rows=10, push='primes', toData='primeList'}
{sequence pull='primes', reverse} ... {/sequence}

Parameters

what
A range '1..10', a number 10 (the same as '1..10'), the name of a sequence type - prime, fibonacci, triangular, ... - or the name of a stored sequence.

Options

range
The range as an option: range='20..30'.
list
An explicit list of values, separated by ;: list='3;1;2'.
from
Where the generating loop starts (default 1). A value for a type that tests candidates (prime), a position for one that computes the nth term (fibonacci, square).
to
Where the loop ends, counting the same way as from.
increment
The step of the loop, 1 or more (default 1).
rows
How many terms to produce - for a sequence this counts produced terms whatever the type, so it is the way to ask for "the first ten".
stop
End at the first term that reaches this value - that term included.
skip
Skip the first N candidates the loop offers (not the first N kept).
minimal
Leave out terms below this value.
maximal
Leave out terms above this value.
unique
Bare option: leave out a term that is already in the result.
randomly
Bare option: take the candidates in random order instead of from low to high.
push
Store the result under this name, for {pull}, {resume} and {action}.
pull
Start from a stored sequence instead of generating one.
toData
Also keep the values as a data set of this name, iterated as {name}.
name
The field the current value is written to, instead of $sequence; the result is also stored under it, like push.
negative
Bare option: invert the selection of an action - first=3, negative is all but the first three.
keep
A play: keep, prime (bare, followed by a type) or keep='prime' keeps only the values that belong to that type. Also remove, flag and make; see those tags.
action
Names an action explicitly, its value after a |: action='first|3'. Usually the action is written as an option of its own, applied to the result in the order written: sum, average, median, count, reverse, sort, shuffle, dedup, first=n, last=n, append='store', ...

Description

{sequence} is the front door of PAD's sequence subsystem. It builds a list of numbers - a range, an explicit list, or the terms of one of 80 mathematical sequence types - runs the plays and actions written with it over that list, and gives the level the values to iterate. Inside the pair the current value is {$sequence}; for a type it is also there under the type's name ({$prime}, {$range}), and name='n' puts it in {$n} instead - and also stores the result under that name, as push does. Like any data tag it has the loop properties: first@sequence, last@sequence, count@sequence.

What to generate

  • A range: {sequence '1..10'}, {sequence range='20..30'}, or a bare number - {sequence 10} is 1 to 10. increment=5 steps.
  • A list: {sequence list='3;1;2'}, values separated by ;.
  • A sequence type, named bare: prime, fibonacci, triangular, square, lucas, catalan, palindrome, happy, recaman, ... and the arithmetic ones that take a value - add=4, multiply=3, modulo=7. The sequence application has a gallery of every type beside its OEIS entry.
  • A stored sequence: the name given to push= earlier, or pull='name'.

How much

rows=10 asks for ten terms whatever the type, and is usually what you want. from and to bound the loop the terms are generated by, and what that loop counts depends on the type: a type that tests candidates (prime, palindrome) counts values, so {sequence prime, to=20} gives the primes up to 20; a type that computes the nth term (fibonacci, square) counts positions, so {sequence square, to=20} gives twenty squares. stop=100 always names a value: the run ends at the first term that reaches it, that term included. minimal and maximal leave out terms outside a band, skip skips candidates, unique drops repeats.

Plays and actions

A play holds every value against a second type while the list is built: keep, prime keeps the primes, remove, prime drops them, flag, prime turns each value into 1 or 0, and make, prime replaces each value with what that type makes of it. The tags {keep}, {remove}, {flag} and {make} are the same runs with the play as the tag name.

An action works on the finished list, in the order written: aggregates that leave one value (sum, average, median, product, count, minimum, maximum), selections (first=3, last=3, slice, element), orderings (sort, reverse, shuffle), dedup, and actions that bring in a stored sequence (append, prepend, merge, intersection, difference). negative inverts an action's selection: first=3, negative is all but the first three.

Storing

push='name' keeps the result as a store. The single form {sequence prime, rows=10, push='primes'} stores without printing anything; {pull} iterates the store later, {resume} transforms the last pushed one in place and {action} runs an action over one. A store can also be iterated by its own name as a tag: {primes}. name='x' stores as well. A store name may not be the name of a sequence type, an action, a sequence option or a PAD option - name='step' is an error, since step is a type. toData='name' keeps the values as an ordinary data set instead.

In an expression a single term is sequence:fibonacci(8) - the 8th term, 13. Under the strict check an option that nothing reads (rowz=3) and a name that is no type, store or action are errors.

Examples

Ranges and lists

A range, a range with a step and an explicit list, each with its own variable name.

Templateexamples/sequence/1-ranges.pad
<p>{sequence '1..10'}{$sequence} {/sequence}</p>
<p>{sequence range='0..50', increment=10, name='tens'}{$tens} {/sequence}</p>
<p>{sequence list='red;green;blue', name='colour'}{$colour}{if not last@sequence}, {/if}{/sequence}</p>
Resultopen alone

1 2 3 4 5 6 7 8 9 10

0 10 20 30 40 50

red, green, blue

Sequence types

The first terms of three famous sequences, and one ended by stop - the term that reaches 100 included.

Templateexamples/sequence/2-types.pad
<p>Primes: {sequence prime, rows=10}{$prime} {/sequence}</p>
<p>Fibonacci: {sequence fibonacci, rows=10}{$fibonacci} {/sequence}</p>
<p>Triangular: {sequence triangular, rows=8}{$sequence} {/sequence}</p>
<p>Fibonacci, stop=100: {sequence fibonacci, stop=100}{$sequence} {/sequence}</p>
Resultopen alone

Primes: 2 3 5 7 11 13 17 19 23 29

Fibonacci: 0 1 1 2 3 5 8 13 21 34

Triangular: 1 3 6 10 15 21 28 36

Fibonacci, stop=100: 0 1 1 2 3 5 8 13 21 34 55 89 144

Plays and actions

keep filters while building; sum, reverse and first=3 with negative work on the result.

Templateexamples/sequence/3-plays-actions.pad
<p>Primes up to 20: {sequence '1..20', keep, prime}{$sequence} {/sequence}</p>
<p>Their sum: {sequence '1..20', keep, prime, sum}{$sequence}{/sequence}</p>
<p>Odd numbers, reversed: {sequence '1..12', keep, odd, reverse}{$sequence} {/sequence}</p>
<p>All but the first three: {sequence '1..10', first=3, negative}{$sequence} {/sequence}</p>
Resultopen alone

Primes up to 20: 2 3 5 7 11 13 17 19

Their sum: 77

Odd numbers, reversed: 11 9 7 5 3 1

All but the first three: 4 5 6 7 8 9 10

Store and reuse

push stores the primes without printing; pull and the store's own name read them back.

Templateexamples/sequence/4-push-pull.pad
{sequence prime, rows=6, push='primes'}
<p>Stored: {pull 'primes'}{$sequence} {/pull}</p>
<p>Reversed: {pull 'primes', reverse}{$sequence} {/pull}</p>
<p>As a tag: {primes}[{$primes}]{/primes}</p>
<p>Term 8 of fibonacci, in an expression: {echo sequence:fibonacci(8)}</p>
Resultopen alone

Stored: 2 3 5 7 11 13

Reversed: 13 11 7 5 3 2

As a tag: [2][3][5][7][11][13]

Term 8 of fibonacci, in an expression: 13

See also

  • {pull} Iterates a stored sequence, optionally through plays and actions.
  • {keep} Keeps only the values of a sequence that belong to a sequence type.
  • {remove} Removes the values of a sequence that belong to a sequence type - the inverse of keep.
  • {flag} Replaces every value of a sequence with 1 or 0: whether it belongs to a sequence type.
  • {make} Replaces every value of a sequence with what a sequence type makes of it.
  • {resume} Transforms the sequence pushed last, in place, through an action or a play.
  • {action} Runs one sequence action - reverse, sum, first and the rest - over a store, a range or a type, and iterates the result.
  • {sparkline} Draws a word-sized line chart as inline SVG - no axes, the last point marked - to stand in a sentence or a table cell.
  • {chart} Draws a chart as inline SVG on the server - 46 kinds, from bars and lines to sankeys, maps of a hierarchy and org charts.