Cassette Standard Library

This is the reference of the standard modules for the language Cassette.

Contents

Value

typeof(x)

Returns the type of a value as a symbol.

Value.typeof(3)                   ; => :integer
Value.typeof(true)                ; => :integer
Value.typeof(false)               ; => :integer
Value.typeof(:foo)                ; => :integer
Value.typeof("x")                 ; => :binary
Value.typeof({1, 2})              ; => :tuple
Value.typeof([1, 2, 3])           ; => :pair
Value.typeof(1 : 2)               ; => :pair
Value.typeof(nil)                 ; => :pair

byte(n)

Returns a binary of length 1 containing the byte represented by the number n. n should be between 0–255 (inclusive).

Value.byte(0x41)                  ; => "A"

symbol_name(sym)

Returns the binary representation of a symbol, if it exists. Returns nil otherwise.

Value.symbol_name(:foo)           ; => "foo"

error?(result)

Returns whether a result is an error tuple ({:error, ...}).

Value.error?({:error, "whoops"})  ; => true
Value.error?(:error)              ; => true

format(iodata)

Recursively builds a binary with the contents of iodata. iodata is either a binary, an integer from 0–255 (representing a byte), or a list of iodata items.

Value.format(["Error: ", [65, 66]]) ; => "Error: AB"

inspect_depth(x, depth)

Returns a binary representation of the value x. Lists and tuples are inspected recursively, up to the given maximum depth.

Value.inspect(34)                 ; => "34"
Value.inspect("hi")               ; => "hi"
Value.inspect([1, 2])             ; => "[1, 2]"

inspect(x)

Same as inspect(x, 10).

List

reduce(list, acc, fn)

Reduces a list, given an initial accumulator, with a given function (left-to-right). The reducer function takes two arguments, the current item and the current accumulator, and should return an updated accumulator.

List.reduce([1, 2, 3], 0, \num, sum -> num + sum) ; => 6

reverse(list)

Reverses a list.

List.reverse([1, 2, 3])           ; => [3, 2, 1]

map(list, fn)

Transforms each element of a list with a given function. The mapping function takes an item as an argument and should return its replacement.

List.map([1, 2, 3], \n -> n * 2)  ; => [2, 4, 6]

filter(list, fn)

Returns a list containing elements that pass a given function. The filtering function takes an item as an argument and should return true if the item should appear in the result.

List.filter([1, 2, 3], \n -> n > 1) ; => [2, 3]

range(min, max)

Returns a list of integers from min (inclusive) to max (exclusive).

List.range(2, 6)    ; => [2, 3, 4, 5]

zip(list1, list2)

Pairs each element of two lists into a new list. The result is the length of the shorter list.

List.zip([1, 2, 3], ["a", "b", "c", "d"]) ; => [1 : "a", 2 : "b", 3 : "c"]

with_index(list)

Returns a list of index : item pairs.

List.with_index(["a", "b", "c"])  ; => [0 : "a", 1 : "b", 2 : "c"]

contains?(list, item)

Returns whether a list contains an item.

List.contains?([1, 2, 3], 2)      ; => true

intersperse(list, sep)

Returns a list with sep between each item.

List.intersperse([1, 2, 3], "x")  ; => [1, "x", 2, "x", 3]

all?(list)

Returns true if all elements of a list are true.

List.all?([0, 1, 2])              ; => false
List.all?([])                     ; => true

any?(list)

Returns true if any element of a list is true.

List.any?([nil, :ok, nil])        ; => true
List.any?([])                     ; => false

IO

open(path, modes)

Opens a file with the given modes. Returns a file reference or {:error, reason}. modes can contain:

IO.open("data.txt", :read)

open_serial(port, speed, opts)

Opens a serial port. opts specifies the data bits, parity type, and stop bits as either a tuple or string. Parity is specified as :even/"E", :odd/"O", or :none/"N". Returns a file reference or {:error, reason}.

IO.open_serial("COM1", 9600, "8N1")
IO.open_serial("/dev/tty1", 12800, {7, :even, 2})

close(file_ref)

Closes a file previously opened with open. Returns :ok or {:error, reason}.

IO.close(f)

read(file_ref, size)

Reads size bytes from a file. Returns a binary or {:error, reason}.

IO.read(f, 1024)

write(file_ref, data)

Writes the binary data to a file. Returns the number of bytes written or {:error, reason}.

IO.write(f, "some data")

seek(file_ref, offset, whence)

Seeks to a given offset in a file, returning the position from the beginning of the file. whence determines how offset is used:

IO.seek(f, 0, :end)

read_file(path)

Reads the entire contents of a file. Returns a binary or {:error, reason}.

IO.read_file("input.txt")

write_file(path, data)

Writes the binary data to a file. Returns the number of bytes written or {:error, reason}.

IO.write_file("output.txt", "Some data")

print(iodata)

Prints iodata to standard output.

IO.print("Hello!")

inspect(x)

Prints a binary representation of the value x to standard output.

IO.inspect({:error, "Some error"})

Net

listen(port)

Opens a socket listening on a port. Returns a socket reference or {:error, reason}.

Net.listen("7000")

accept(socket)

Waits for a new connection on a socket opened with listen. Returns a new socket for that connection or {:error, reason}.

Net.accept(s)

connect(host, port)

Opens a socket connecting to a host. Returns a socket reference or {:error, reason}.

Net.connect("example.com", "80")

send(socket, data)

Sends data through an open socket. Returns the number of bytes sent or {:error, reason}.

Net.send(s, "message")

recv(socket, max_length)

Receives data from an open socket. Returns a binary of the received data or {:error, reason}.

Net.recv(s, 1024)