Nanyx
PlaygroundPackages

mstack

Mutable stacks, last in, first out

Import it as nanyx/mstack. Reach for an MStack to keep a stack inside a memory block: push and pop change it in place, each in constant time. Like an MRef, it belongs to the block it was made in, and can’t leave it.

import nanyx/mstack

def top = memory {
  def stack = MStack.empty()
  stack \push(5.0)
  stack \push(6.0)
  def popped = stack \pop -- #some(6.0)
  stack \peek -- #some(5.0)
}

Reference

Mutable stacks: last in, first out, changed in place.

An MStack(a) is a stack that MStack.push and MStack.pop change in place, each in constant time. It holds its elements in an MRef, so it belongs to the memory block it was made in: every operation requires that block’s Memory, and the stack can’t leave the block.

Each function is attached to MStack, so with import nanyx/mstack it is called as MStack.push(stack, 5), or as stack \push(5). For an immutable stack, which needs no memory block, see Stack.

MStack

type MStack(a) = [Memory] private MRef(list(a))

MStack.empty

def MStack.empty: [Memory] () -> MStack(a)

A new stack with nothing on it.

MStack.fromList

def MStack.fromList: [Memory] list(a) -> MStack(a)

A new stack holding xs, the first of them on top.

MStack.fromArray

def MStack.fromArray: [Memory] array(a) -> MStack(a)

A new stack holding xs, the first of them on top. What a literal builds a stack with, MStack[1, 2, 3], or [1, 2, 3] where a stack is expected.

MStack.push

def MStack.push: [Memory] MStack(a), a -> ()

Puts value on top of stack.

MStack.pop

def MStack.pop: [Memory] MStack(a) -> #some(a) | #none

Takes the value on top of stack off it, or #none when it is empty.

MStack.peek

def MStack.peek: [Memory] MStack(a) -> #some(a) | #none

The value on top of stack, left where it is, or #none when it is empty.

MStack.isEmpty

def MStack.isEmpty: [Memory] MStack(a) -> bool

Whether stack has nothing on it.

MStack.length

def MStack.length: [Memory] MStack(a) -> int

How many values are on stack.

MStack.toSeq

def MStack.toSeq: [Memory] MStack(a) -> seq(a)

The values on stack now, the top one first, as a sequence: pushing or popping later doesn’t change it. A mutable stack is Iterable by it, so a for loop walks one.

MStack.toList

def MStack.toList: [Memory] MStack(a) -> list(a)

The values on stack, the top one first.