# sequence

Definition: | sequence identifier -- or -- bool b = sequence(object x) -- or -- seq identifier -- or -- bool res = seq(object x)
Description: | This serves to define the sequence type. seq is just a simple alias of sequence. You can also call it like an ordinary function to determine if an object is a sequence.
pwa/p2js: | Supported, however JavaScript is a typeless language, with "`sequence q`" being mapped to "`let /*sequence*/ q;`" and there are no run-time typechecks, or at least they are very rare (see implementation notes below). An explicit hll/Phix "if sequence(x)" and similar expressions all work under pwa/p2js exactly as expected.
Comments: | When invoked as a function, returns true (1) if x is a sequence otherwise false (0). Note that `sequence(x)` and `not atom(x)` are effectively identical. A sequence can hold all string values, but the reverse is not true.
Example 1: |
    sequence s = {1,2,3}
Example 2: |
    if sequence(x) then
        total = 0
        for i=1 to length(x) do
            total += x[i]
        end for
    else
        -- x must be an atom
        total = x
    end if
Implementation: | There are generic versions of the sequence function implemented as :%opSq in builtins\VM\pType.e and :%opSq0 in builtins\VM\pJcc.e that the compiler only invokes as a last resort, preferring instead to inline that functionality if the argument is known to be assigned or the result variable, if any, is known to be an integer, ie does not require decref/dealloc. Note that opSeq is an equality test, and must not be confused with opSq, which is the sequence test. The compiler may also completely omit the test and/or any related code, if type inference/propagation determines that the result will always be true or always false. The file pwa/p2js.js defines sequence() with an optional name parameter, which if not "" triggers unassigned or type check errors, but that (parameter) is not intended for or available from hll/Phix code, and only meant for use in manually hand-crafted replacements elsewhere in p2js.js, and in practice it ended up being invoked (that way) rather more sparingly than first predicted.
See Also: | atom, integer, string, object, Sequences, Core Language
Technicalia | The sq_seq() routine can be used to apply a sequence() test to all top-level elements of a sequence, eg `sq_seq({1,1.5,"string",{1,1.5,"string",{}}) ==> {false,false,true,true}`. That routine returns a single true/false if passed an atom or string, otherwise a "flat" sequence of true/false of the same length as the input argument. There are no known valid uses of the sq_seq() routine, though, as yet.