Basic modules and lambdas
Let's take a look at basic modules and lambdas in Aquarius.
"hello"
100
3.1415926
[1 2 3]
{"name": "bob" "age": 19}
(puts: 1 2 3)
(assert: (1 == 1) "it's true")
Common methods
All modules and lambdas have common methods. In addition to modules and lambdas, other entities in Aquarius also have these methods, such as variables.
{1} and {2} are placeholders for entities such as modules, lambdas and variables.
method | |
---|---|
({1} == {2}) -> bool | Determine whether {1} is equal to {2} |
({1} != {2}) -> bool | Determine whether {1} is not equal to {2} |
({1} to_s) -> string | Converts {1} to string |
({1} to_b) -> bool | Converts {1} to boolean |
({1} type) -> string | Extracts type of {1} |
Nil
Non-value in Aquarius. We need to be aware of the difference between it and nil
.
(let x = (Nil to_b))
(let y = (nil to_b))
(puts: x y)
zhoudefa@DefaBook aquarius % ./bin/aqr run ./ext/test/test.aqr
true false
Booleans
True
and False
are designed as boolean modules. true
and false
are the corresponding values. Conditions need an boolean expression as condition expression, and ==
, !=
methods are all return boolean values.
Numerics
Int
and Float
are two numeric modules.
Int
is the set of all signed 64-bit integers(range: -9223372036854775808 through 9223372036854775807).
Float
is the set of all IEEE-754 64-bit floating-point numbers in Aquarius.
Strings
A String
value holds an arbitrary sequence of bytes, typically representing characters. And the String
module implements subset of Ruby 2.7.0 string methods.
There is a convention here that calls to method which name ends with !
change the value itself. The corresponding method without an !
suffix returns a new String
.
(let x = "abcd")
(let y = (x reverse))
(puts: x y (x == y))
zhoudefa@DefaBook aquarius % ./bin/aqr run ./ext/test/test.aqr
abcd dcba false
Now let's look at the result for reverse!
.
(let x = "abcd")
(let y = (x reverse!))
(puts: x y (x == y))
zhoudefa@DefaBook aquarius % ./bin/aqr run ./ext/test/test.aqr
dcba dcba true
Lists
List
is a container module implements subset of Ruby 2.7.0 Array
. Some methods have two formats, one names with suffix !
that evaluated in place and the other is not.
(let x = []) # list literal #
(let y = (List: 1 2 3))
(puts: x y)
zhoudefa@DefaBook aquarius % ./bin/aqr run ./ext/test/test.aqr
[] [1 2 3]
Dicts
Like List
, Dict
is a container module that implements parts of Ruby 2.7.0 Hash
.
(let x = {}) # dict literal #
(let y = (Dict:))
(puts: x y)
zhoudefa@DefaBook aquarius % ./bin/aqr run ./ext/test/test.aqr
{} {}
dict initialization does not accpet any parameters.
RegExp
A RegExp
holds a regular expression, used to match a pattern against strings. There are two ways to create a RegExp
value: One is the literal form of the regular expression, which wraps the expression in ``; Another way is to call :
of RegExp
module directly.
Regular expressions (regexps) are patterns which describe the contents of a string. They're used for testing whether a string contains a given pattern, or extracting the portions that match.
(let x = `\s`)
(let y = "w hat")
(let z = "what")
(puts: (x match? y) (x match? z))
zhoudefa@DefaBook aquarius % ./bin/aqr run ./ext/test/test.aqr
true false
Range
A Range
is a contiguous interval of integers. There's a beginning and an end. The end is not included. We can create a new Range
value by calling :
of Range
module. Alternatively, we can call the integer's ..
method to create it.
(let x = (1 .. 2))
(let y = (Range: 1 2))
(puts: x y)
(puts: (x count) (x begin) (x end))
(x >> (|i|(puts: "->" i)))
(x << (|i|(puts: "->" i)))
zhoudefa@DefaBook aquarius % ./bin/aqr run ./ext/test/test.aqr
(1..2) (1..2)
1 1 2
-> 1
-> 1
Error
Error
s are special in Aquraius. When you initalize an error value, the program will interrupt execution immiediately. If there is a try
expression around it, the program jumps to the catch
block immediately. The error will be eaten
.
In the
catch
block, the predefined variableerror
will now be the error you just caught. You can get the information from error value.
puts
puts
lambda outputs the parameters as string to the stdout. show
is alias of puts
.
(puts: String 1 nil)
termial shows
<module String> 1 nil
assert
assert
lamabda is used to perform an assertion on an expression.
(assert: (true == false) "Failed to assert boolean")
zhoudefa@DefaBook aquarius % ./bin/aqr run ./ext/test/test.aqr
AssertError: Failed to assert boolean
(assert : ...) /Users/zhoudefa/Documents/Projects/others/aquarius/ext/test/test.aqrc:25:1
(main : ...) /Users/zhoudefa/Documents/Projects/others/aquarius/ext/test/test.aqrc:25:26
zhoudefa@DefaBook aquarius %
noop
noop
is an empty lambda which does nothing when it is called. It acts as a placeholder.