package: pfds

module: ralist

The ralist module implements a random-access list that can be used as an immutable array. This module exports a single function make for creating a random-access list.

ralist.make


ralist.make(init_seq = [])
      

Return a new random-access list initialized with the values from the sequence init_seq, which can be any object that implements the first and rest generic functions.

The new random-access list is represented as a closure that can respond to the following messages:


at(i)
      

Return the element at index i. Raise an error if the index is invalid.

    
set(i, x)
      

Return a new list with the value at index i updated to x. Raise an error if the index is invalid.


head()
      

Return the first element in the list. Raise an error if the list is empty.

    
tail()
      

Return a new list with the first element removed. Raise an error if the list is empty.


first
      

Return the first element in the list. Raise an error if the list is empty.

    
rest
      

Return a new list with the first element removed. Return false if the list is empty.


cons(x)
      

Return a new list queue with the element x added.


count
      

Return the number of elements in the list.


is_empty()
      

Return true if the list is empty, return false otherwise.

Examples:


let xs = ralist.make([100, 200, 300])
xs[0]
// 100
let ys = xs.set(1, 250)
xs[1]
// 200
ys[1]
// 250
      

Index | Packages Index