Skip to content
Halyard
Project
Source code on Codeberg (opens in new tab)

Search

Menu

ValueLabel Module

Access and transform individual ValueLabel pairs.

A ValueLabel is an (Int, String) pair — an integer code and its human-readable label. ValueLabel data lives inside enum variants defined with the ValueLabel built-in type. This module provides functions to extract, create, transform, and query individual pairs.

All functions accept both standalone tuples and enum variants containing ValueLabel data.

Common patterns

halyard
import ValueLabel

type Gender
    = Male (ValueLabel 1 "Male")
    | Female (ValueLabel 2 "Female")

ValueLabel.value Gender::Male          -- 1
ValueLabel.label Gender::Female        -- "Female"
Gender::Male |> ValueLabel.toString    -- "1: Male"

Functions

Access

ValueLabel.value

ValueLabel -> Int

Extract the integer value from a ValueLabel.

Example:
halyard
import ValueLabel

enum Gender
    = Male (ValueLabel 1 "Male")
    | Female (ValueLabel 2 "Female")

ValueLabel.value Gender::Male

Notes: Accepts both standalone (Int, String) tuples and enum variants with ValueLabel data.

See also: ValueLabel.label, ValueLabel.toPair

ValueLabel.label

ValueLabel -> String

Extract the string label from a ValueLabel.

Example:
halyard
import ValueLabel

enum Gender
    = Male (ValueLabel 1 "Male")
    | Female (ValueLabel 2 "Female")

ValueLabel.label Gender::Female

Notes: Accepts both standalone (Int, String) tuples and enum variants with ValueLabel data.

See also: ValueLabel.value, ValueLabel.toPair

Create

ValueLabel.create

Int -> String -> ValueLabel

Create a standalone ValueLabel tuple from an integer value and a string label.

Example:
halyard
import ValueLabel

let vl = ValueLabel.create 1 "Yes"
ValueLabel.value vl

See also: ValueLabel.value, ValueLabel.label

Convert

ValueLabel.toPair

ValueLabel -> (Int, String)

Convert a ValueLabel to a plain (Int, String) tuple. For standalone tuples this is the identity; for enum variants it unwraps the ValueLabel data.

Example:
halyard
import ValueLabel

enum Gender
    = Male (ValueLabel 1 "Male")
    | Female (ValueLabel 2 "Female")

ValueLabel.toPair Gender::Male

See also: ValueLabel.value, ValueLabel.label

ValueLabel.toString

ValueLabel -> String

Format a ValueLabel as "value: label" (e.g., "1: Male").

Example:
halyard
import ValueLabel

enum Gender
    = Male (ValueLabel 1 "Male")
    | Female (ValueLabel 2 "Female")

ValueLabel.toString Gender::Male

See also: ValueLabel.value, ValueLabel.label

Transform

ValueLabel.mapValue

(Int ->{| e} Int) -> a ->{| e} (Int, String)

Transform the integer value of a ValueLabel, keeping the label unchanged.

Example:
halyard
import ValueLabel

enum Gender
    = Male (ValueLabel 1 "Male")
    | Female (ValueLabel 2 "Female")

Gender::Male |> ValueLabel.mapValue (|v| v * 10) |> ValueLabel.value

See also: ValueLabel.mapLabel

ValueLabel.mapLabel

(String ->{| e} String) -> a ->{| e} (Int, String)

Transform the string label of a ValueLabel, keeping the value unchanged.

Example:
halyard
import ValueLabel
import String

enum Gender
    = Male (ValueLabel 1 "Male")
    | Female (ValueLabel 2 "Female")

Gender::Male |> ValueLabel.mapLabel String.toUpper |> ValueLabel.label

See also: ValueLabel.mapValue

Query

ValueLabel.matchesValue

Int -> ValueLabel -> Bool

Check if a ValueLabel's integer value matches the given value.

Example:
halyard
import ValueLabel

enum Gender
    = Male (ValueLabel 1 "Male")
    | Female (ValueLabel 2 "Female")

ValueLabel.matchesValue 1 Gender::Male

See also: ValueLabel.matchesLabel

ValueLabel.matchesLabel

String -> ValueLabel -> Bool

Check if a ValueLabel's string label matches the given label.

Example:
halyard
import ValueLabel

enum Gender
    = Male (ValueLabel 1 "Male")
    | Female (ValueLabel 2 "Female")

ValueLabel.matchesLabel "Male" Gender::Male

See also: ValueLabel.matchesValue