Neptune: Lists
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:
- list literals begin with an opening square bracket
[. - list literals end with a closing square bracket
]. - normal statement separators provide a boundary between items.
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:
list_map(over:using)accepts a List under the nameoverand a Function under the nameusing, then returns a new List after mapping.usingmust accept one argument under the name_
list_flatmap(over:using)accepts a List under the nameoverand a Function under the nameusing, then returns a new List after flat mapping.usingmust accept one argument under the name_usingmust return a List
list_reduce(over:from:using) accepts a List under the nameover, any value under the namefrom, and a Function under the nameusing`, then returns a reduction.usingmust accept two arguments:accumulatoranditem
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:
table_new()which creates a new, empty table. This is equivalent to using an empty table literal{ }table_get(key:from)which returns the value in the table under the namefromfor the key under the namekeytable_set(key:of:to)which returns a copy of a table (under the nameof), but with a key (under the namekey) set to a new value (the value passed under the nameto)
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 }