MoonBit Standard Library

repository·main·Indexed 22 days ago

https://github.com/moonbitlang/core

The standard library for the MoonBit language, providing essential types, collections, algorithms, and tooling. It features a modular hierarchy consisting of the implicitly available `builtin` layer, the automatically opened `prelude`, and explicit standard packages. Key components include the `Array` package with adaptive QuickSort and functional utilities, the `argparse` library for declarative CLI argument parsing, and core types such as `BigInt`, `Map`, and `String`.

Tokens
72.9K
Snippets
324
Records
361
Agent score
78%

What's inside moonbitlang/core

  1. Overview of the `uint64` package

    main
    The uint64 package in moonbitlang/core provides functionality for working with 64-bit unsigned integers (UInt64). It includes support for constants, arithmetic, bitwise operations, comparisons, byte conversions, and type conversions.
  2. Overview of the `json` package

    main

    The json package provides tools for parsing, stringifying, and type-safe conversion between JSON and MoonBit data types. The core abstraction is the Json enum, which represents all possible JSON values:

    • Null
    • True
    • False
    • Number
    • String
    • Array
    • Object

    You can use JSON literals directly in your MoonBit source code.

  3. Overview of the List package

    main
    The @list.List package provides an immutable, singly linked list data structure. It is designed for functional programming, supporting efficient traversal, transformation, and recursive algorithms. Because it is immutable, it is ideal for scenarios where data integrity and functional patterns are required.
  4. Use `uint` for 32-bit unsigned integer operations

    main

    The uint package provides utilities for handling 32-bit unsigned integers (UInt) in MoonBit. It includes constants for the value range and methods for converting between different number formats.

    import @uint
  5. Use the MoonBit Prelude for common types and functions

    main

    The prelude package provides a set of commonly used types, traits, and functions that are automatically available in every MoonBit program without requiring explicit imports. This includes core data structures like Array, Map, and Set, as well as essential utilities for error handling, debugging, and I/O.

    // Example of using prelude types like Set directly
    test "Set constructor from prelude" {
      let set = Set([1, 2, 3, 4])
      inspect(set.length(), content="4")
      inspect(set.contains(3), content="true")
    }
  6. Use the `v128` package for SIMD operations

    main

    The moonbitlang/core/v128 package provides SIMD-style (Single Instruction, Multiple Data) constructors and lane extractors for the built-in V128 type. You can use it to create 128-bit vectors from constants and extract individual lanes (elements) from those vectors.

    test "make and extract lanes" {
      let value = @v128.i64x2_const(0x0123456789abcdefUL, 0xfedcba9876543210UL)
      inspect(@v128.i64x2_extract_lane(value, 0), content="81985529216486895")
      inspect(@v128.i64x2_extract_lane(value, 1), content="18364758544493064720")
    }
  7. Overview of SortedMap

    main

    A SortedMap is a mutable map implementation backed by an AVL tree that maintains its keys in sorted order. It is ideal when you need ordered traversal, efficient range queries, or keys that are always sorted.

    Performance

    • add/set: O(log n)
    • remove: O(log n)
    • get/contains: O(log n)
    • iterate: O(n)
    • range: O(log n + k) where k is the number of elements in the range
    • space complexity: O(n)
  8. What is ImmutableSet and how to create one

    main

    An ImmutableSet is a persistent, immutable implementation of a set structure using a balanced tree. Because it is based on comparison, the type used must implement the Compare trait.

    Every operation on an ImmutableSet returns a new set instance rather than modifying the existing one.

    // Create an empty set
    let _set1 : @sorted_set.SortedSet[Int] = @sorted_set.new()
    
    // Create a singleton set
    let _set2 = @sorted_set.singleton(1)
    
    // Create a set from an Array
    let _set4 = @sorted_set.SortedSet([1])
  9. What is the Option type?

    main

    The Option type is a built-in MoonBit type used to represent an optional value. It is an enum with two variants:

    • Some(T): Represents a value of type T.
    • None: Represents the absence of a value.

    You can use the type annotation Option[A] or the shorthand syntax A? (e.g., Int?).

  10. What is the Result type?

    main

    Result[T, E] is an enum used for explicit and declarative error handling, similar to Rust's Result<T, E>. It has two variants:

    • Ok(T): Represents success and contains a value of type T.
    • Err(E): Represents an error and contains an error value of type E.