Data types

From Reia

Jump to: navigation, search

Contents

Numeric types

Two numeric types are available: Integers and Floats. Both consist of a sequence of digits. Floats are distinguished by the presence of a '.' character somewhere inside the digit sequence.

Integers and Floats are arbitrary precision and have no set range limitations.

Examples:

3      # integer
3.1415 # float

Booleans

The two boolean types are represented by the "true" and "false" keywords. These values are returned from any boolean comparison.

Examples:

true
false

Nil

The "nil" keyword is a placeholder for the absence of a value. In a boolean context it evaluates to false.

Example:

nil

Atoms

Atoms are like a freeform enumeration. Each atom has a unique value globally across the entire system. Atoms are declared by prepending the ~ character to an alphanumeric sequence, and may optionally contain underscores. The name of an atom must start with a letter. More complex atoms may be specified by quoting the sequence following the ~ character. Quoted atoms are not restricted and may start with any character you desire.

Examples:

~foo        # an atom
~foo_bar    # an atom with an underscore
~foobar_42  # an atom with an underscore and digits
~"Hi there" # a quoted atom
~" :-) "    # quoted atoms are unrestricted
~'this too' # single quotes are also acceptable

Strings

Strings are delimited by either single or double quotes. Quotes of one type are nestable inside the other. Multi-line strings are triple quoted.

>> ""          # empty string
>> 'foo'       # a single quoted string
>> "foo 'bar'" # a string nesting another
>> """a
.. multiline
.. string"""   # multiline string

Lists

Lists are ordered sequences of values, enclosed with square braces: []. Entries in lists are delineated with commas.

Examples:

[]     # empty list
[42]   # a list with one value
[0,42] # a list with two values

Tuples

Tuples are fixed-length, immutable value sets, enclosed with parens: (). Entries in tuples are delineated with commas.

However, parens can also be used to specify order-of-operation. Thus, (x) evaluates to x, rather than specifying a single-element tuple containing x. The special-case syntax (x,) specifies a single element tuple. () specifies an empty tuple.

Examples:

()     # empty tuple
(42,)  # tuple with one value
(0,42) # tuple with two values

Dicts

Dicts are simple key/value stores, enclosed with curly braces {}. A ':' character delineates the key from the value. Entries in dicts are delineated with commas.

Examples:

{}                  # empty dict
{'foo': 42}         # dict with one value
{~foo: 0, ~bar: 42} # dict with two values

Binaries

Binaries are a data structure for storing and operating on bitfields, enclosed in double angle brackets: <<>>. Binaries are immutable and store data in a space efficient manner.

Binaries are specified as lists of integers, in the range 0-255.

Examples:

<<>>     # empty binary
<<0>>    # binary with one value
<<0,42>> # binary with two values

Constants

Constants are declared by any alphanumeric sequence which begins with a capital letter, and may optionally contain an underscore.

Examples:

ANSWER_TO_EVERYTHING = 42
TheValueOfPi = 3.14159

Lambdas

Lambda expressions are declared by the "fun" keyword, which is followed by a parenthetical containing arguments, and curly braces or do/end keywords containing an expression. Variables bound in the parenthetical are available in the scope of the given expression, as are any variables in the surrounding lexical scope (i.e. outer variables are closed over by the lambda) The lambda returns result of last expression evaluated in its scope.

Lambda expressions are called by adding a parenthetical expression to variable bound to a Lambda.

Examples:

>> plus_two = fun(n) { n + 2 }
>> plus_two(2)
4

When using curly braces, the lambda expression is bound to a single line. For multi-line lambdas, the do syntax must be used instead:

>> plus_two_times_two = fun(n) do
..   n += 2
..   n * 2
..
#Fun
>> plus_two_times_two(2)
8

Regular expressions

Regular expressions are Perl compatible and enclosed by forward slashes: /

The standard Perl modifiers 'i', 'm', 's', and 'x' may follow the second forward slash terminating the expression.

Examples:

 //        # empty regexp
 /foo/     # a regexp that matches foo
 /[a-z]+/i # a regexp that matches strings of lowercase or uppercase letters
Personal tools