dataprep/validated
Types
Applicative functor for error accumulation.
pub type Validated(a, e) {
Valid(a)
Invalid(non_empty_list.NonEmptyList(e))
}
Constructors
-
Valid(a) -
Invalid(non_empty_list.NonEmptyList(e))
Values
pub fn and_then(
v: Validated(a, e),
f: fn(a) -> Validated(b, e),
) -> Validated(b, e)
Monadic bind. Sequential, short-circuits on error. Use for dependent operations (e.g. parse then validate). Does NOT accumulate errors – that is intentional.
pub fn fail(error: e) -> Validated(a, e)
Construct an Invalid result from a single error.
Convenience for the common pattern of failing with one error without
manually importing non_empty_list.
pub fn from_result_map(
r: Result(a, err),
f: fn(err) -> e,
) -> Validated(a, e)
Convert from Result with a custom error mapper. Useful for parsing where the original error type differs from the validation error type.
Example: int.parse(raw) |> validated.from_result_map(fn(_) { NotAnInteger(raw) })
pub fn map(v: Validated(a, e), f: fn(a) -> b) -> Validated(b, e)
Transform the success value (functor map).
pub fn map2(
f: fn(a, b) -> c,
va: Validated(a, e),
vb: Validated(b, e),
) -> Validated(c, e)
Combine two Validated values, accumulating all errors.
pub fn map3(
f: fn(a, b, c) -> d,
va: Validated(a, e),
vb: Validated(b, e),
vc: Validated(c, e),
) -> Validated(d, e)
Combine three Validated values, accumulating all errors.
pub fn map4(
f: fn(a, b, c, d) -> out,
va: Validated(a, e),
vb: Validated(b, e),
vc: Validated(c, e),
vd: Validated(d, e),
) -> Validated(out, e)
Combine four Validated values, accumulating all errors.
pub fn map5(
f: fn(a, b, c, d, e_) -> out,
va: Validated(a, e),
vb: Validated(b, e),
vc: Validated(c, e),
vd: Validated(d, e),
ve: Validated(e_, e),
) -> Validated(out, e)
Combine five Validated values, accumulating all errors.
pub fn map_error(
v: Validated(a, e1),
f: fn(e1) -> e2,
) -> Validated(a, e2)
Transform every error value.
pub fn sequence(
vs: List(Validated(a, e)),
) -> Validated(List(a), e)
Combine a list of Validated values into a Validated list. All errors from all elements are accumulated. Returns Valid([]) for an empty input list.
pub fn to_result(v: Validated(a, e)) -> Result(a, List(e))
Convert to Result (collapses errors into a list).
pub fn traverse(
xs: List(a),
f: fn(a) -> Validated(b, e),
) -> Validated(List(b), e)
Apply a function that returns Validated to each element of a list, then combine the results. All errors are accumulated.
Equivalent to list.map(xs, f) |> validated.sequence but avoids
building an intermediate list.