Etc keyword

From Swym Wiki
Jump to: navigation, search

The etc keyword indicates to the compiler, "continue this sequence in the obvious way". For example, here's the definition of the "total" function, which adds up all the elements of an array:

 Array.'total' = .1st + .2nd + etc

Although etc is a very intuitive concept that makes code extremely readable, it can seem rather "magical". There are some details that need explaining.

The etc keyword must immediately follow an infix operator (above, that would be the + operator, but other useful operators include && || * and ,). It collects all the expressions that are separated by this operator (in this case, the expressions .1st and .2nd), and looks for differences between those expressions. In this case the difference it finds is a number: 1 changes to 2.

The etc keyword can deduce three kinds of sequence:

1) A difference between number literals becomes a numeric sequence.

[1,20,300,4000,etc**10]
RunSandbox

2) A difference between string literals becomes a unicode sequence.

"(a)" + "(b)" + etc**26
RunSandbox

3) An expanding sequence continues to expand.

[1],[1,1],etc**10
RunSandbox

[edit] Resolving numeric sequences

The etc keyword understands the following types of numeric sequence:

  • Arithmetic sequences, such as 1,2,3,4 (adding 1 each time) or 1, 0.75, 0.5, 0.25 (subtracting 0.25 each time). Element n = a + b*n.
  • Geometric sequences, such as 3,6,12,24 (multiplying by 2 each time) or 1,0.1,0.01,0.001 (dividing by 10 each time). Element n = a * b^n.
  • Quadratic sequences, such as 1,4,9,16 (square numbers) or 1,3,6,10 (triangle numbers). Element n = a + b*n + c*n*n
  • Cumulative geometric sequences, such as 1, 11, 111, 1111. Element n = total[a * b^(0..<n)]
  • Arithmetic times geometric sequences, such as 1, 20, 300, 4000. Element n = (a + b*n) * c^n

If two examples are given, it's assumed to be an arithmetic sequence.

If only three examples are given, it's assumed to be an arithmetic or geometric sequence. Most of the other sequences can be made to fit just about any three-element sequence, so they can only be deduced from four or more terms. The Swym compiler will ask for additional clarification if you don't give enough examples.

1,20,300,etc
RunSandbox

No more complex sequences (the most common request being Fibonacci) are supported: they're generally not obvious to a human reader. (And really, that's who this whole feature is supposed to be for.)

[edit] Resolving an expanding sequence

When comparing code examples such as 1 and 1+2, it's clear to a human reader that the 1 has not changed, but a +2 expression has been added; so the next term should be 1+2+3, and so on. The etc compiler understands this concept too.

Currently, only a few types of expanding sequence are supported.

Appending one term each time:

[1], [1,2], etc**10
RunSandbox

Prepending one term each time:

[1], [2,1], etc**10
RunSandbox

Or a normal sequence, split into expanding slices:

[1], [2,3], etc**10
RunSandbox

[edit] Ending conditions

It's easy to overlook the question of when an etc expression terminates, but it's actually a rather important, and subtle, topic.

Any time an etc expression would access a value outside the bounds of an array, it halts instead. This is very convenient, and means you don't have to explicitly provide a halt condition on many simple expressions, such as the "total" function above.

To provide a more explicit halt condition, the writer can use variants of the etc expression. Currently, the following are supported:

1, 2, etc..10
RunSandbox

The etc.. keyword halts after it generates a term equal to the halt value.

1, 2, 4, etc..<100
RunSandbox

The etc..< keyword halts if it would generate a term equal to or greater than the halt value.

1, 2, 4, etc**10
RunSandbox

The etc** keyword halts after generating the requested number of terms.

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox