zipper

The zipper data structure

zipper
data-structure
moon add CAIMEOX/zipper@0.1.0
Download zip
Author
Version
0.1.0
License
Apache-2.0
Last updated
11 months ago
Downloads
24
README

#CAIMEOX/zipper

A classic implementation of the Huet Zipper for singly-linked lists in MoonBit.

#Features

  • Immutable, functional list (MList)
  • Zipper structure for efficient navigation and modification
  • Move focus left/right, update, insert, and delete at focus
  • Convert between list and zipper
  • Move focus to start or end

#Data Structures

pub enum MList[T] {
Nil
Cons(T, MList[T])
}

pub struct Zipper[T] {
left : MList[T] // elements left of focus, reversed
focus : T // current element in focus
right : MList[T] // elements right of focus
}

#API

  • MList::from_array(arr: Array[T]) -> MList[T]
  • MList::reverse(self: MList[T]) -> MList[T]
  • Zipper::from_list(list: MList[T]) -> Zipper[T]?
  • to_list(self: Zipper[T]) -> MList[T]
  • current(self: Zipper[T]) -> T
  • move_left(self: Zipper[T]) -> Zipper[T]?
  • move_right(self: Zipper[T]) -> Zipper[T]?
  • update(self: Zipper[T], new_focus: T) -> Zipper[T]
  • insert_left(self: Zipper[T], value: T) -> Zipper[T]
  • insert_right(self: Zipper[T], value: T) -> Zipper[T]
  • delete(self: Zipper[T]) -> Zipper[T]?
  • move_to_start(self: Zipper[T]) -> Zipper[T]
  • move_to_end(self: Zipper[T]) -> Zipper[T]

#Example

let list = MList::from_array([1, 2, 3, 4])
let zipper = from_list(list).unwrap().move_right().unwrap() // focus on 2
let at_end = zipper.move_to_end() // focus on 4
let at_start = at_end.move_to_start() // focus on 1
let result = at_start.to_list() // [1, 2, 3, 4]

#License

Apache-2.0

#
MList

pub(all) enum MList[T] {
Nil
Cons(T, MList[T])
}

A generic functional list.
impl Eq for MList[T]
impl Show for MList[T]

#
MList::from_array

fn[T] MList::from_array(arr : Array[T]) -> MList[T]

#
Zipper

pub(all) struct Zipper[T] {
left : MList[T]
focus : T
right : MList[T]
}

A zipper for a functional list MList[T]. It represents a list with a "focus" on one of its elements. left contains elements to the left of the focus, in reverse order. right contains elements to the right of the focus.
impl Eq for Zipper[T]
impl Show for Zipper[T]

#
Zipper::current

fn[T] Zipper::current(self : Zipper[T]) -> T

Returns the element currently in focus.

#
Zipper::delete

fn[T] Zipper::delete(self : Zipper[T]) -> Zipper[T]?

Deletes the focused element. Focus moves to the right if possible, otherwise to the left. Returns None if the deletion results in an empty list.

#
Zipper::from_list

fn[T] Zipper::from_list(list : MList[T]) -> Zipper[T]?

Creates a zipper from a list, focusing on the first element. Returns None if the list is empty.

#
Zipper::insert_left

fn[T] Zipper::insert_left(self : Zipper[T], value : T) -> Zipper[T]

Inserts an element to the left of the focus. The new element remains on the left.

#
Zipper::insert_right

fn[T] Zipper::insert_right(self : Zipper[T], value : T) -> Zipper[T]

Inserts an element to the right of the focus. The new element remains on the right.

#
Zipper::move_left

fn[T] Zipper::move_left(self : Zipper[T]) -> Zipper[T]?

Moves the focus one step to the left. Returns None if already at the beginning of the list.

#
Zipper::move_right

fn[T] Zipper::move_right(self : Zipper[T]) -> Zipper[T]?

Moves the focus one step to the right. Returns None if already at the end of the list.

#
Zipper::move_to_end

fn[T] Zipper::move_to_end(self : Zipper[T]) -> Zipper[T]

Moves the focus to the last element (end of the list).

#
Zipper::move_to_start

fn[T] Zipper::move_to_start(self : Zipper[T]) -> Zipper[T]

Moves the focus to the first element (start of the list).

#
Zipper::to_list

fn[T] Zipper::to_list(self : Zipper[T]) -> MList[T]

Converts a zipper back into a list.

#
Zipper::update

fn[T] Zipper::update(self : Zipper[T], new_focus : T) -> Zipper[T]

Replaces the focused element with a new value.

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io