untag
The type Or
is an type union with no wrapping.
Or2(Int, String, Float)
is the same as Int | String | Float
,
not {Left, Int} | {Right, {Left, String} | {Right, Float}}
You can either treat it as a dynamic type that gives you some options on what it might be, or as a group of types that can be whittled down with decoders.
There are functions for (example)
* Encoding: tail_3rd t3 -> Or2(t1,t2,t3)
* Decoding: dehead Or2(t1,t2,t3) -> t1?
* Moving: flip Or(a,b) -> Or(b,a)
The repeated functions with numbers are used to move around
the types, so you dont have to make an external casting function.
For example, rev4_with_tail
just returns the same value with
a different, equivalent type. It asserts that
Or(k, Or(l, Or(m, n)))
is equivalent to Or(n, Or(m, Or(l, k)))
.
head_4th puts a type in the 4th position in a union.
V------------------------->V
fn(k) -> Or(l, Or(m, Or(n, Or(k, o))))
Using tail_4th puts the type in the 4th position at the very end of the union, terminating the list of types.
V-----------------------V
fn(k) -> Or(l, Or(m, Or(n, k)))
Types
Values
pub fn decode(
this or: Or(a, b),
with decoder: decode.Decoder(c),
) -> Result(c, List(decode.DecodeError))
pub fn dehead_unsafe(
this union: Or(head, tail),
with decoder: decode.Decoder(head),
) -> Result(head, #(tail, List(decode.DecodeError)))
Tries to decode the head. if it fails, it will return the value as the tail type.
This function is unsound when the generic type of the decoder does not directly match with the data it wants to decode. For example,
"string"
// String -> Or(String, b)
|> head
|> dehead_unsafe(dec.int |> dec.map(int.to_string))
The value of the Or(String, b) is “string”, not any integer.
pub fn detail_unsafe(
this union: Or(head, tail),
with decoder: decode.Decoder(tail),
) -> Result(tail, #(head, List(decode.DecodeError)))
Unsafe. See [dehead_unsafe]
pub fn give5(
or: Or(Or(a, Or(b, Or(c, Or(d, Or(e, f))))), g),
) -> Or(f, Or(e, Or(d, Or(c, Or(b, Or(a, g))))))
pub fn give6(
or: Or(Or(a, Or(b, Or(c, Or(d, Or(e, Or(f, g)))))), h),
) -> Or(g, Or(f, Or(e, Or(d, Or(c, Or(b, Or(a, h)))))))
pub fn give7(
or: Or(Or(a, Or(b, Or(c, Or(d, Or(e, Or(f, Or(g, h))))))), i),
) -> Or(h, Or(g, Or(f, Or(e, Or(d, Or(c, Or(b, Or(a, i))))))))
pub fn give8(
or: Or(
Or(a, Or(b, Or(c, Or(d, Or(e, Or(f, Or(g, Or(h, i)))))))),
j,
),
) -> Or(
i,
Or(h, Or(g, Or(f, Or(e, Or(d, Or(c, Or(b, Or(a, j)))))))),
)
pub fn head_10th(
x: a,
) -> Or(
b,
Or(c, Or(d, Or(e, Or(f, Or(g, Or(h, Or(i, Or(j, Or(a, k))))))))),
)
pub fn rev10_with_tail(
or: Or(
a,
Or(b, Or(c, Or(d, Or(e, Or(f, Or(g, Or(h, Or(i, j)))))))),
),
) -> Or(
j,
Or(i, Or(h, Or(g, Or(f, Or(e, Or(d, Or(c, Or(b, a)))))))),
)
pub fn rev6_with_tail(
or: Or(a, Or(b, Or(c, Or(d, Or(e, f))))),
) -> Or(f, Or(e, Or(d, Or(c, Or(b, a)))))
pub fn rev7_with_tail(
or: Or(a, Or(b, Or(c, Or(d, Or(e, Or(f, g)))))),
) -> Or(g, Or(f, Or(e, Or(d, Or(c, Or(b, a))))))
pub fn rev8_with_tail(
or: Or(a, Or(b, Or(c, Or(d, Or(e, Or(f, Or(g, h))))))),
) -> Or(h, Or(g, Or(f, Or(e, Or(d, Or(c, Or(b, a)))))))
pub fn rev9_with_tail(
or: Or(a, Or(b, Or(c, Or(d, Or(e, Or(f, Or(g, Or(h, i)))))))),
) -> Or(i, Or(h, Or(g, Or(f, Or(e, Or(d, Or(c, Or(b, a))))))))
pub fn take5(
or: Or(a, Or(b, Or(c, Or(d, Or(e, Or(f, g)))))),
) -> Or(Or(f, Or(e, Or(d, Or(c, Or(b, a))))), g)
pub fn take6(
or: Or(a, Or(b, Or(c, Or(d, Or(e, Or(f, Or(g, h))))))),
) -> Or(Or(g, Or(f, Or(e, Or(d, Or(c, Or(b, a)))))), h)
pub fn take7(
or: Or(a, Or(b, Or(c, Or(d, Or(e, Or(f, Or(g, Or(h, i)))))))),
) -> Or(Or(h, Or(g, Or(f, Or(e, Or(d, Or(c, Or(b, a))))))), i)
pub fn take8(
or: Or(
a,
Or(b, Or(c, Or(d, Or(e, Or(f, Or(g, Or(h, Or(i, j)))))))),
),
) -> Or(
Or(i, Or(h, Or(g, Or(f, Or(e, Or(d, Or(c, Or(b, a)))))))),
j,
)
pub fn to_dynamic(or: Or(a, b)) -> dynamic.Dynamic