ordo-conversio
Translation status: हिन्दी reader-locale proof. Term names and code fences follow the hi pack; supporting prose may still be English.
An क्रम converts to its number and name, and a number converts back to a member.
Syntax: <ordo-value> ↦ int | <ordo-value> ↦ string | <int-expression> ↦ <Ordo> [⊥ <default>]
Category#
conversion
Related#
Examples#
radix/corpus/ordo/ordo-conversio.fab (canonical · conversio)#
An क्रम converts to its number and name, and a number converts back to a member.
# =============================================================================
# ordo-conversio — An क्रम converts to its number and name, and back.
# =============================================================================
#
# What this teaches:
# • `member ↦ int` gives the member's number. A member without `= n`
# takes the previous member's number plus one; the first defaults to 0.
# • `member ↦ string` gives the member's name. It cannot fail.
# • `n ↦ Ordo` finds the first member with that number. It can fail, so
# write a `⊥` default for values that match no member.
#
# Common mistakes:
# • Writing methods on the क्रम to convert it — an क्रम is data only;
# these three conversions are built in.
# • An क्रम with a string-valued member has no number, so it converts
# only to string.
#
# See also: क्रम, ↦, ⊥
# =============================================================================
enum Color {
red,
green,
blue
}
enum Status {
pending = 10,
active,
done = -3,
archived
}
main {
# क्रम ↦ int: declared or implicit numbers
print Color.green ↦ int
print Status.active ↦ int
print Status.archived ↦ int
# क्रम ↦ string: the member name
print Color.blue ↦ string
print Status.done ↦ string
# int ↦ क्रम: the matching member, or the ⊥ default
const Color found ← 2 ↦ Color ⊥ Color.red
print found ↦ string
const Color missing ← 9 ↦ Color ⊥ Color.red
print missing ↦ string
const Status negative ← -2 ↦ Status ⊥ Status.pending
print negative ↦ string
}Expected output:
1
11
-2
blue
done
blue
red
archived