Esc
Start typing to search...

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

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:
import ValueLabel

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

ValueLabel.value Gender::Male
Try it

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:
import ValueLabel

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

ValueLabel.label Gender::Female
Try it

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:
import ValueLabel

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

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:
import ValueLabel

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

ValueLabel.toPair Gender::Male
Try it

See also: ValueLabel.value, ValueLabel.label

ValueLabel.toString

ValueLabel -> String

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

Example:
import ValueLabel

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

ValueLabel.toString Gender::Male
Try it

See also: ValueLabel.value, ValueLabel.label

Transform

ValueLabel.mapValue

(Int -> Int) -> ValueLabel -> ValueLabel

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

Example:
import ValueLabel

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

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

See also: ValueLabel.mapLabel

ValueLabel.mapLabel

(String -> String) -> ValueLabel -> ValueLabel

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

Example:
import ValueLabel
import String

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

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

See also: ValueLabel.mapValue

Query

ValueLabel.matchesValue

Int -> ValueLabel -> Bool

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

Example:
import ValueLabel

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

ValueLabel.matchesValue 1 Gender::Male
Try it

See also: ValueLabel.matchesLabel

ValueLabel.matchesLabel

String -> ValueLabel -> Bool

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

Example:
import ValueLabel

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

ValueLabel.matchesLabel "Male" Gender::Male
Try it

See also: ValueLabel.matchesValue