lens

    A lens library for functional programming in MoonScript.

    lens
    functional programming
    fp
    Download zip
    Author
    Version
    0.1.1
    License
    Apache-2.0
    Last updated
    7 months ago
    Downloads
    16

    #sennenki/lens

    A simple yet powerful optics library for MoonBit, providing Lens, Prism, Iso, and Epi.

    #Installation

    Add this package to your moon.pkg.json:

    { "deps": { "sennenki/lens": "0.1.0" } }

    #Usage

    #Lens

    A Lens[A, B] focuses on a part B of a structure A. It allows getting and setting the focus.

    let lens = Lens::fst()
    let pair = (1, 2)

    // Get
    lens.get(pair) // 1

    // Set
    lens.set(pair, 3) // (3, 2)

    Compose lenses using compose:

    let l1 = Lens::fst()
    let l2 = Lens::snd()
    // Focus on the second element of the first element of a nested pair
    let composed = l1.compose(l2) // Lens[((Int, Int), Int), Int]

    composed.get(((1, 2), 3)) // 2

    #Prism

    A Prism[A, B] focuses on a part B of a structure A that may or may not exist (like a sum type variant or an index in a list).

    let prism = Prism::some()
    prism.get(Some(42)) // Some(42)
    prism.get(None) // None

    prism.set(Some(1), 2) // Some(2)

    Useful built-in prisms:

    • Prism::ok(): Focuses on the Ok variant of a Result.
    • Prism::err(): Focuses on the Err variant of a Result.
    • Prism::array_nth(index): Focuses on an index in an Array.
    • Prism::list_nth(index): Focuses on an index in a List.
    • Prism::map(key): Focuses on a key in a Map.

    #Iso

    An Iso[A, B] represents an isomorphism between A and B (conversions in both directions).

    let iso = Iso::array_list()
    iso.to([1, 2, 3]) // List::[1, 2, 3]
    iso.from(@list.from_array([1, 2, 3])) // [1, 2, 3]

    #Epi

    An Epi[A, B] (Epimorphism) has a to: (A) -> B? which might fail, and a from: (B) -> A which is total.

    #API Reference

    #Lens

    • Lens::new(get, set)
    • Lens::id()
    • Lens::fst()
    • Lens::snd()
    • Lens::from_iso(iso)
    • Lens::compose(other)
    • Lens::to_prism()

    #Prism

    • Prism::new(get, set)
    • Prism::some()
    • Prism::ok()
    • Prism::err()
    • Prism::array_nth(index)
    • Prism::immut_array_nth(index)
    • Prism::list_head()
    • Prism::list_tail()
    • Prism::list_nth(index)
    • Prism::map(key)
    • Prism::immut_hashmap(key)
    • Prism::immut_sorted_map(key)
    • Prism::from_epi(epi)
    • Prism::compose(other)

    #Iso

    • Iso::new(to, from)
    • Iso::invert()
    • Iso::compose(other)
    • Iso::to_epi()
    • Iso::array_list()

    #Epi

    • Epi::new(to, from)
    • Epi::compose(other)

    Epi

    type Epi[A, B]

    Epimorphism between A and B

    Epi::compose

    fn[A, B, C] Epi::compose(self : Epi[A, B], other : Epi[B, C]) -> Epi[A, C]

    Compose two Epis

    Epi::from

    fn[A, B] Epi::from(self : Epi[A, B], b : B) -> A

    Get the value converted by the Epi

    Epi::new

    fn[A, B] Epi::new(to : (A) -> B?, from : (B) -> A) -> Epi[A, B]

    Create a new Epi from to and from functions

    Epi::to

    fn[A, B] Epi::to(self : Epi[A, B], a : A) -> B?

    Get the value converted by the Epi

    Iso

    type Iso[A, B]

    Isomorphism between A and B

    Iso::array_list

    fn[A] Iso::array_list() -> Iso[Array[A],
    List
    [A]]

    Iso between Array and List

    Iso::compose

    fn[A, B, C] Iso::compose(self : Iso[A, B], other : Iso[B, C]) -> Iso[A, C]

    Compose two Isos

    Iso::from

    fn[A, B] Iso::from(self : Iso[A, B], b : B) -> A

    Get the value converted by the Iso

    Iso::invert

    fn[A, B] Iso::invert(self : Iso[A, B]) -> Iso[B, A]

    Invert an Iso

    Iso::new

    fn[A, B] Iso::new(to : (A) -> B, from : (B) -> A) -> Iso[A, B]

    Create a new Iso from to and from functions

    Iso::to

    fn[A, B] Iso::to(self : Iso[A, B], a : A) -> B

    Get the value converted by the Iso

    Iso::to_epi

    fn[A, B] Iso::to_epi(self : Iso[A, B]) -> Epi[A, B]

    Convert an Iso to an Epi

    Lens

    type Lens[A, B]

    Lens between A and B

    Lens::compose

    fn[A, B, C] Lens::compose(self : Lens[A, B], other : Lens[B, C]) -> Lens[A, C]

    Compose two Lenses

    Lens::from_iso

    fn[A, B] Lens::from_iso(iso : Iso[A, B]) -> Lens[A, B]

    Create a Lens from an Iso

    Lens::fst

    fn[A, B] Lens::fst() -> Lens[(A, B), A]

    Focus on the first element of a tuple

    Lens::get

    fn[A, B] Lens::get(self : Lens[A, B], a : A) -> B

    Get the value focused by the Lens

    Lens::id

    fn[A] Lens::id() -> Lens[A, A]

    Identity Lens

    Lens::new

    fn[A, B] Lens::new(get : (A) -> B, set : (A, B) -> A) -> Lens[A, B]

    Create a new Lens from get and set functions

    Lens::set

    fn[A, B] Lens::set(self : Lens[A, B], a : A, b : B) -> A

    Set the value focused by the Lens

    Lens::snd

    fn[A, B] Lens::snd() -> Lens[(A, B), B]

    Focus on the second element of a tuple

    Lens::to_prism

    fn[A, B] Lens::to_prism(self : Lens[A, B]) -> Prism[A, B]

    Convert a Lens to a Prism

    Prism

    type Prism[A, B]

    Prism between A and B

    Prism::array_nth

    fn[A] Prism::array_nth(index : Int) -> Prism[Array[A], A]

    Focus on the element at the given index in an Array

    Prism::compose

    fn[A, B, C] Prism::compose(self : Prism[A, B], other : Prism[B, C]) -> Prism[A, C]

    Compose two Prisms

    Prism::err

    fn[E, A] Prism::err() -> Prism[Result[A, E], E]

    Focus on the Err variant of a Result

    Prism::from_epi

    fn[A, B] Prism::from_epi(epi : Epi[A, B]) -> Prism[A, B]

    Create a Prism from an Epi

    Prism::get

    fn[A, B] Prism::get(self : Prism[A, B], a : A) -> B?

    Get the value focused by the Prism

    Prism::immut_array_nth

    fn[A] Prism::immut_array_nth(index : Int) -> Prism[
    T
    [A], A]

    Focus on the element at the given index in an immutable array

    Prism::immut_hashmap

    fn[K : Eq + Hash, V] Prism::immut_hashmap(key : K) -> Prism[
    HashMap
    [K, V], V]

    Focus on the value at the given key in an Immutable HashMap.

    Prism::immut_sorted_map

    fn[K : Compare + Eq, V] Prism::immut_sorted_map(key : K) -> Prism[
    SortedMap
    [K, V], V]

    Focus on the value at the given key in an Immutable SortedMap.

    Prism::list_head

    fn[A] Prism::list_head() -> Prism[
    List
    [A], A]

    Focus on the head of a List

    Prism::list_nth

    fn[A] Prism::list_nth(index : Int) -> Prism[
    List
    [A], A]

    Focus on the element at the given index in a List

    Prism::list_tail

    Focus on the tail of a List

    Prism::map

    fn[K : Eq + Hash, V] Prism::map(key : K) -> Prism[Map[K, V], V]

    Focus on the value at the given key in a Map. Returns None if the key is missing.

    Prism::new

    fn[A, B] Prism::new(get : (A) -> B?, set : (A, B) -> A) -> Prism[A, B]

    Create a new Prism from get and set functions

    Prism::ok

    fn[E, A] Prism::ok() -> Prism[Result[A, E], A]

    Focus on the Ok variant of a Result

    Prism::set

    fn[A, B] Prism::set(self : Prism[A, B], a : A, b : B) -> A

    Set the value focused by the Prism

    Prism::some

    fn[A] Prism::some() -> Prism[A?, A]

    Focus on the value of a Some variant

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io