Will's Digital Garden

Neptune: Lists

Download v0.5b25

previous post | Part two of a series on Neptune

Much of Neptune's existing functionality is unchanged from the first post, but I've added quite a bit since then. Here, I'll detail any changes and any additions.

Language Changes

In addition to the original three types (Strings, Numbers, and Functions), there are now two more types: Lists and Tables

Lists

Lists are the first complex data structure in Neptune. For now they are heterogenous. There is new syntax for list literals:

Previously, the only statement separators were newlines. Now this group of tokens includes semicolon ; and (in some contexts such as list literals) commas ,. Thus, these are all valid list literals:

list_with_newlines = [
  1
  2
  3
]

list_with_commas = [1, 2, 3]
list_with_semicolons = [1; 2; 3]

A few new standard library functions exist to help operate on lists:

Tables

Tables are the next complex data structure in Neptune. The syntax for table literals are similar to lists, but using curly braces instead of square brackets:

dimensons_table = {
  width: 10
  height: 20
  length: 30
}

There are of course a few standard library functions to operate on tables:

Functions

In addition to returning a simple singular value, functions can now return their full inner scope as a table.

function double_dimensions = (_ x) -> * { width: x * 2, height: x * 2 }

double_dimensions(5)
# returns a table with 3 keys: { x: 5, width: 10, height: 10 } because those keys were present in the inner scope

In the event something akin to “private” variables is wanted, the inner scope can be restricted by specifying which values should be returned:

function triple_dimensions = (_ x) -> (width, height) { width: x * 3, height: x * 3 }

triple_dimensions(7)
# returns a table with 2 keys: { width: 21, height: 21 }

Like function arguments, these can be renamed as well:

function four_times_dimensions = (_ x) -> (width w, height h) { w = x * 4 ; h = x * 4 }

four_times_dimensions(9)
# returns a table with 2 keys: { width: 36, height 36 }