aiken_flat/encoder
Types
Opaque type for encoding given values and collecting them in a buffer.
Function type shared by all encoders.
Alias
EncoderFn<a> = fn(Encoder, a) -> Encoder
Constants
Identity encoder selector against field indices (helpful for defining empty record type encoders).
Identity encoder selector factory against tag indices (helpful for defining “empty sum type” encoders, i.e. sum types that none of their constructors carry values).
Functions
Basics
Runners
Essential Encoders
Custom Type Encoders
Helper for making encoders for record types with 2 fields. More performant
than using make_rec
.
make_rec_3(
f0_encoder: EncoderFn<Data>,
f1_encoder: EncoderFn<Data>,
f2_encoder: EncoderFn<Data>,
) -> EncoderFn<List<Data>>
Helper for making encoders for record types with 3 fields. More performant
than using make_rec
.
make_rec_4(
f0_encoder: EncoderFn<Data>,
f1_encoder: EncoderFn<Data>,
f2_encoder: EncoderFn<Data>,
f3_encoder: EncoderFn<Data>,
) -> EncoderFn<List<Data>>
Helper for making encoders for record types with 4 fields. More performant
than using make_rec
.
Helper for creating record type encoders. The resulting encoder traverses a
list of Data
values and uses their indices to find their corresponding
encoders from the provided selector function.
make_sum_2(
c0_encoders: fn(Int) -> EncoderFn<Data>,
c1_encoders: fn(Int) -> EncoderFn<Data>,
) -> EncoderFn<Data>
Helper for making encoders for sum types with 2 data constructors, and
arbitrary fields in each. More performant than using
make_sum
. The Data
in return type is assumed to be a
Constr
.
make_sum_3(
c0_encoders: fn(Int) -> EncoderFn<Data>,
c1_encoders: fn(Int) -> EncoderFn<Data>,
c2_encoders: fn(Int) -> EncoderFn<Data>,
) -> EncoderFn<Data>
Helper for making encoders for sum types with 3 data constructors, and
arbitrary fields in each. More performant than using
make_sum
. The Data
in return type is assumed to be a
Constr
.
make_sum_4(
c0_encoders: fn(Int) -> EncoderFn<Data>,
c1_encoders: fn(Int) -> EncoderFn<Data>,
c2_encoders: fn(Int) -> EncoderFn<Data>,
c3_encoders: fn(Int) -> EncoderFn<Data>,
) -> EncoderFn<Data>
Helper for making encoders for sum types with 4 data constructors, and
arbitrary fields in each. More performant than using
make_sum
. The Data
in return type is assumed to be a
Constr
.
make_sum(
constr_count: Int,
encoder_fn_selector_factory: fn(Int) -> fn(Int) -> EncoderFn<Data>,
) -> EncoderFn<Data>
Helper function for generating encoders for sum types. The factory is
expected to be a function that takes an index, which is the index of the
constructor in the subject sum type, and returns another function that given
the index of a field from the record, returns an encoder function.
An example can be:
pub type Color {
Abyss
BlackAndWhite {
white: Int,
}
Colored {
red: Int,
green: Int,
blue: Int,
}
}
The factory function for this datatype should be:
let color_encoder_factory =
fn(tag: Int) {
if tag == 0 {
identity_encoder_fn_selector
} else if tag == 1 {
fn(f_index: Int) {
if f_index == 0 {
integer |> contramap(builtin.un_i_data)
} else {
fail
}
}
} else if tag == 2 {
fn(f_index: Int) {
if f_index <= 2 {
integer |> contramap(builtin.un_i_data)
} else {
fail
}
}
} else {
fail
}
}
In this case, it can be much simpler if we forgo the strictness:
let color_encoder_factory =
fn(_) {
fn(_) {
integer |> contramap(builtin.un_i_data)
}
}
And the encoder itself will be:
let color_encoder = make_sum(3, color_encoder_factory)
Essential Encoders
Custom Type Encoders
Helper for making encoders for record types with 2 fields. More performant
than using make_rec
.
make_rec_3(
f0_encoder: EncoderFn<Data>,
f1_encoder: EncoderFn<Data>,
f2_encoder: EncoderFn<Data>,
) -> EncoderFn<List<Data>>
Helper for making encoders for record types with 3 fields. More performant
than using make_rec
.
make_rec_4(
f0_encoder: EncoderFn<Data>,
f1_encoder: EncoderFn<Data>,
f2_encoder: EncoderFn<Data>,
f3_encoder: EncoderFn<Data>,
) -> EncoderFn<List<Data>>
Helper for making encoders for record types with 4 fields. More performant
than using make_rec
.
Helper for creating record type encoders. The resulting encoder traverses a
list of Data
values and uses their indices to find their corresponding
encoders from the provided selector function.
make_sum_2(
c0_encoders: fn(Int) -> EncoderFn<Data>,
c1_encoders: fn(Int) -> EncoderFn<Data>,
) -> EncoderFn<Data>
Helper for making encoders for sum types with 2 data constructors, and
arbitrary fields in each. More performant than using
make_sum
. The Data
in return type is assumed to be a
Constr
.
make_sum_3(
c0_encoders: fn(Int) -> EncoderFn<Data>,
c1_encoders: fn(Int) -> EncoderFn<Data>,
c2_encoders: fn(Int) -> EncoderFn<Data>,
) -> EncoderFn<Data>
Helper for making encoders for sum types with 3 data constructors, and
arbitrary fields in each. More performant than using
make_sum
. The Data
in return type is assumed to be a
Constr
.
make_sum_4(
c0_encoders: fn(Int) -> EncoderFn<Data>,
c1_encoders: fn(Int) -> EncoderFn<Data>,
c2_encoders: fn(Int) -> EncoderFn<Data>,
c3_encoders: fn(Int) -> EncoderFn<Data>,
) -> EncoderFn<Data>
Helper for making encoders for sum types with 4 data constructors, and
arbitrary fields in each. More performant than using
make_sum
. The Data
in return type is assumed to be a
Constr
.
make_sum(
constr_count: Int,
encoder_fn_selector_factory: fn(Int) -> fn(Int) -> EncoderFn<Data>,
) -> EncoderFn<Data>
Helper function for generating encoders for sum types. The factory is
expected to be a function that takes an index, which is the index of the
constructor in the subject sum type, and returns another function that given
the index of a field from the record, returns an encoder function.
An example can be:
pub type Color {
Abyss
BlackAndWhite {
white: Int,
}
Colored {
red: Int,
green: Int,
blue: Int,
}
}
The factory function for this datatype should be:
let color_encoder_factory =
fn(tag: Int) {
if tag == 0 {
identity_encoder_fn_selector
} else if tag == 1 {
fn(f_index: Int) {
if f_index == 0 {
integer |> contramap(builtin.un_i_data)
} else {
fail
}
}
} else if tag == 2 {
fn(f_index: Int) {
if f_index <= 2 {
integer |> contramap(builtin.un_i_data)
} else {
fail
}
}
} else {
fail
}
}
In this case, it can be much simpler if we forgo the strictness:
let color_encoder_factory =
fn(_) {
fn(_) {
integer |> contramap(builtin.un_i_data)
}
}
And the encoder itself will be:
let color_encoder = make_sum(3, color_encoder_factory)
Helper for making encoders for record types with 2 fields. More performant
than using make_rec
.
make_rec_3(
f0_encoder: EncoderFn<Data>,
f1_encoder: EncoderFn<Data>,
f2_encoder: EncoderFn<Data>,
) -> EncoderFn<List<Data>>
Helper for making encoders for record types with 3 fields. More performant
than using make_rec
.
make_rec_4(
f0_encoder: EncoderFn<Data>,
f1_encoder: EncoderFn<Data>,
f2_encoder: EncoderFn<Data>,
f3_encoder: EncoderFn<Data>,
) -> EncoderFn<List<Data>>
Helper for making encoders for record types with 4 fields. More performant
than using make_rec
.
Helper for creating record type encoders. The resulting encoder traverses a
list of Data
values and uses their indices to find their corresponding
encoders from the provided selector function.
make_sum_2(
c0_encoders: fn(Int) -> EncoderFn<Data>,
c1_encoders: fn(Int) -> EncoderFn<Data>,
) -> EncoderFn<Data>
Helper for making encoders for sum types with 2 data constructors, and
arbitrary fields in each. More performant than using
make_sum
. The Data
in return type is assumed to be a
Constr
.
make_sum_3(
c0_encoders: fn(Int) -> EncoderFn<Data>,
c1_encoders: fn(Int) -> EncoderFn<Data>,
c2_encoders: fn(Int) -> EncoderFn<Data>,
) -> EncoderFn<Data>
Helper for making encoders for sum types with 3 data constructors, and
arbitrary fields in each. More performant than using
make_sum
. The Data
in return type is assumed to be a
Constr
.
make_sum_4(
c0_encoders: fn(Int) -> EncoderFn<Data>,
c1_encoders: fn(Int) -> EncoderFn<Data>,
c2_encoders: fn(Int) -> EncoderFn<Data>,
c3_encoders: fn(Int) -> EncoderFn<Data>,
) -> EncoderFn<Data>
Helper for making encoders for sum types with 4 data constructors, and
arbitrary fields in each. More performant than using
make_sum
. The Data
in return type is assumed to be a
Constr
.
make_sum(
constr_count: Int,
encoder_fn_selector_factory: fn(Int) -> fn(Int) -> EncoderFn<Data>,
) -> EncoderFn<Data>
Helper function for generating encoders for sum types. The factory is expected to be a function that takes an index, which is the index of the constructor in the subject sum type, and returns another function that given the index of a field from the record, returns an encoder function.
An example can be:
pub type Color {
Abyss
BlackAndWhite {
white: Int,
}
Colored {
red: Int,
green: Int,
blue: Int,
}
}
The factory function for this datatype should be:
let color_encoder_factory =
fn(tag: Int) {
if tag == 0 {
identity_encoder_fn_selector
} else if tag == 1 {
fn(f_index: Int) {
if f_index == 0 {
integer |> contramap(builtin.un_i_data)
} else {
fail
}
}
} else if tag == 2 {
fn(f_index: Int) {
if f_index <= 2 {
integer |> contramap(builtin.un_i_data)
} else {
fail
}
}
} else {
fail
}
}
In this case, it can be much simpler if we forgo the strictness:
let color_encoder_factory =
fn(_) {
fn(_) {
integer |> contramap(builtin.un_i_data)
}
}
And the encoder itself will be:
let color_encoder = make_sum(3, color_encoder_factory)