I/O
print("Hello") # print to stdout
print("a", "b", "c") # multiple args
let name = input("Name? ") # read from stdin
Type Conversion
str(42) # "42"
int("42") # 42
float("3.14") # 3.14
bool(1) # true
type(42) # "int"
Type checking functions: is_nil, is_int, is_float, is_number, is_string, is_list, is_dict, is_bool, is_fn
String Functions
len("hello") # 5
split("a,b,c", ",") # ["a", "b", "c"]
join(["a", "b"], "-") # "a-b"
upper("hello") # "HELLO"
lower("HELLO") # "hello"
trim(" hi ") # "hi"
replace("hello", "l", "r") # "herro"
contains("hello", "ell") # true
starts_with("hello", "he") # true
ends_with("hello", "lo") # true
index_of("hello", "ll") # 2
List Functions
let items = [3, 1, 4, 1, 5]
len(items) # 5
push(items, 9) # mutates: [3,1,4,1,5,9]
append(items, 2) # same as push
pop(items) # removes and returns last
slice(items, 1, 3) # [1, 4]
reverse(items) # reversed copy
sort(items) # sorted copy
sort(items, fn(a,b) => b - a) # custom comparator
# Functional operations
map([1,2,3], fn(x) => x * 2) # [2, 4, 6]
filter([1,2,3,4], fn(x) => x > 2) # [3, 4]
reduce([1,2,3], fn(a,b) => a + b, 0) # 6
find([1,2,3], fn(x) => x > 1) # 2
# Utilities
zip([1,2], ["a","b"]) # [[1,"a"], [2,"b"]]
enumerate(["a","b","c"]) # [[0,"a"], [1,"b"], [2,"c"]]
flatten([[1,2], [3,4]]) # [1, 2, 3, 4]
unique([1,1,2,2,3]) # [1, 2, 3]
take([1,2,3,4,5], 3) # [1, 2, 3]
skip([1,2,3,4,5], 2) # [3, 4, 5]
count([1,2,3], fn(x) => x > 1) # 2
any([false, true, false]) # true
all([true, true, false]) # false
contains([1,2,3], 2) # true
Dict Functions
let d = {name: "Alice", age: 25}
keys(d) # ["name", "age"]
values(d) # ["Alice", 25]
entries(d) # [["name","Alice"], ["age",25]]
has(d, "name") # true
remove_key(d, "age") # removes key, returns value
Math Functions
abs(-5) # 5
max(3, 7) # 7
min(3, 7) # 3
round(3.7) # 4
round(3.14159, 2) # 3.14
floor(3.9) # 3
ceil(3.1) # 4
sqrt(16) # 4
pow(2, 10) # 1024
log(100) # natural log
log10(100) # 2
sin(3.14159) # ~0
cos(0) # 1
random() # 0.0 to 1.0
random(1, 100) # random int in range
sum([1,2,3,4,5]) # 15
average([1,2,3,4,5]) # 3
Utility Functions
range(5) # [0, 1, 2, 3, 4]
range(1, 5) # [1, 2, 3, 4]
range(0, 10, 2) # [0, 2, 4, 6, 8]
repeat("x", 3) # ["x", "x", "x"]
assert(x > 0, "must be positive")
debug(value) # print type and value info
now() # unix timestamp (float)
timestamp() # unix timestamp (int)
Testing
NSL has built-in test blocks:
test "addition works"
expect 1 + 1 == 2
expect 2 + 2 == 4, "basic math"
test "string operations"
let s = "hello"
expect len(s) == 5
expect upper(s) == "HELLO"
Run tests with your NSL file -- test blocks execute automatically and report results.