{keep}

Keeps only the values of a sequence that belong to a sequence type.

Sequences Tag pairs

Syntax

{keep '1..50', square} ... {/keep}
{keep palindrome, from=100, rows=5} ... {/keep}
{keep:prime from=10, rows=4} ... {/keep:prime}
{keep store, even} ... {/keep}

Parameters

values
Where the values come from: a range '1..50', a stored sequence named bare (primes, not quoted), or left out - the candidates then run from from upward.
type
The sequence type the values are tested against: prime, even, square, palindrome, ... A type with a value is written as an option: multiple=3.

Description

{keep '1..50', square} runs a sequence in keep mode: every value is held against the named type, and only the members stay - here 1 4 9 16 25 36 49. It is the same run as {sequence '1..50', keep, square}, with the play as the tag name; {remove} is its inverse, {flag} marks instead of filtering, and {make} transforms.

The values can come from a range, from a stored sequence ({keep primes, palindrome}), or from nowhere in particular: {keep palindrome, from=100, rows=5} offers the numbers from 100 upward and keeps the first five palindromes - 101 111 121 131 141. Because filtering happens while the list is built, rows counts the values kept, not the candidates offered. The prefix form {keep:prime from=10, rows=4} names the type in the tag; it takes no range parameter - bound it with from, to and rows. A store is named bare, not quoted.

The current value is {$sequence}. Actions written after the type work on the kept values: {keep '1..20', prime, sum} is 77.

Examples

Filter a range

The squares and the primes among the numbers 1 to 50.

Templateexamples/keep/1-range.pad
<p>Squares: {keep '1..50', square}{$sequence} {/keep}</p>
<p>Primes:  {keep '1..50', prime}{$sequence} {/keep}</p>
Resultopen alone

Squares: 1 4 9 16 25 36 49

Primes: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

The first five from 100

Without a range the candidates run from from= upward; rows counts what is kept.

Templateexamples/keep/2-first-five.pad
<p>{keep palindrome, from=100, rows=5}{$sequence} {/keep}</p>
<p>Their sum: {keep palindrome, from=100, rows=5, sum}{$sequence}{/keep}</p>
Resultopen alone

101 111 121 131 141

Their sum: 605

See also

  • {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.
  • {sequence} Generates a range, a list or one of 80 mathematical sequences, and iterates its values.
  • {pull} Iterates a stored sequence, optionally through plays and actions.