Skip to content

collections Module

The collections module provides data structures for storing and manipulating collections of values.

Stack

A Last-In-First-Out (LIFO) data structure.

new_stack()

Creates and returns a new empty stack.

Returns:

  • Stack: A new empty stack.

Example:

Create Stack
1
2
3
4
5
package main
import fmt
import collections

var s = collections.new_stack()

push(item)

Adds an item to the top of the stack.

Parameters:

  • item: The item to add to the stack.

Example:

Push to Stack
s.push(42)
s.push("hello")

pop()

Removes and returns the top item from the stack.

Returns:

  • any: The top item, or None if the stack is empty.

Example:

Pop from Stack
1
2
3
package main
var item = s.pop()
fmt.Println("Popped:", item)

peek()

Returns the top item without removing it.

Returns:

  • any: The top item, or None if the stack is empty.

Example:

Peek Stack
1
2
3
package main
var top = s.peek()
fmt.Println("Top item:", top)

is_empty()

Checks if the stack is empty.

Returns:

  • bool: true if the stack is empty, false otherwise.

Example:

Check Empty Stack
1
2
3
4
package main
if s.is_empty() {
    fmt.Println("Stack is empty")
}

size()

Returns the number of items in the stack.

Returns:

  • int: The number of items in the stack.

Example:

Stack Size
package main
fmt.Println("Stack size:", s.size())

inspect()

Returns a string representation of the stack.

Returns:

  • string: A string representation of the stack.

Example:

Inspect Stack
package main
fmt.Println("Stack:", s.inspect())

Queue

A First-In-First-Out (FIFO) data structure.

new_queue()

Creates and returns a new empty queue.

Returns:

  • Queue: A new empty queue.

Example:

Create Queue
var q = collections.new_queue()

enqueue(item)

Adds an item to the end of the queue.

Parameters:

  • item: The item to add to the queue.

Example:

Enqueue Items
q.enqueue("first")
q.enqueue("second")

dequeue()

Removes and returns the first item from the queue.

Returns:

  • any: The first item, or None if the queue is empty.

Example:

Dequeue Item
1
2
3
package main
var item = q.dequeue()
fmt.Println("Dequeued:", item)

peek()

Returns the first item without removing it.

Returns:

  • any: The first item, or None if the queue is empty.

Example:

Peek Queue
1
2
3
package main
var first = q.peek()
fmt.Println("First item:", first)

is_empty()

Checks if the queue is empty.

Returns:

  • bool: true if the queue is empty, false otherwise.

Example:

Check Empty Queue
1
2
3
4
package main
if q.is_empty() {
    fmt.Println("Queue is empty")
}

size()

Returns the number of items in the queue.

Returns:

  • int: The number of items in the queue.

Example:

Queue Size
package main
fmt.Println("Queue size:", q.size())

inspect()

Returns a string representation of the queue.

Returns:

  • string: A string representation of the queue.

Example:

Inspect Queue
package main
fmt.Println("Queue:", q.inspect())

Complete Example

Complete Collections Example
package main
import fmt
import collections

// Stack example
var stack = collections.new_stack()
stack.push(1)
stack.push(2)
stack.push(3)

fmt.Println("Stack:", stack.inspect())  // [1, 2, 3]
fmt.Println("Size:", stack.size())      // 3

var top = stack.pop()
fmt.Println("Popped:", top)            // 3
fmt.Println("Stack:", stack.inspect())  // [1, 2]

// Queue example
var queue = collections.new_queue()
queue.enqueue("first")
queue.enqueue("second")
queue.enqueue("third")

fmt.Println("Queue:", queue.inspect())  // ["first", "second", "third"]
fmt.Println("Size:", queue.size())      // 3

var first = queue.dequeue()
fmt.Println("Dequeued:", first)        // "first"
fmt.Println("Queue:", queue.inspect())  // ["second", "third"]

Draining a queue (while-style loop using for)

Harneet does not have a separate while keyword. Use a condition-only for loop to achieve the same effect (just like Go):

While Loop Pattern
package main
import fmt
import collections

var q = collections.new_queue()
q.enqueue("a")
q.enqueue("b")
q.enqueue("c")

fmt.Println("Draining queue:")
for !q.is_empty() {
    fmt.Println("  dequeued:", q.dequeue())
}

fmt.Println("Empty now?", q.is_empty())