Skip to main content

Mojo struct

Variadic

struct Variadic

A namespace for variadic utilities.

Implemented traits

AnyType, ImplicitlyDestructible

comptime members

concat_types

comptime concat_types[T: AnyTrait[AnyType], //, *Ts: Variadic[T]] = #kgen.variadic.concat(Ts)

Represents the concatenation of multiple variadic sequences of types.

Parameters

  • T (AnyTrait): The trait that types in the variadic sequences must conform to.
  • *Ts (Variadic): The variadic sequences to concatenate.

concat_values

comptime concat_values[T: AnyType, //, *Ts: Variadic[T]] = #kgen.variadic.concat(Ts)

Represents the concatenation of multiple variadic sequences of values.

Parameters

  • T (AnyType): The types of the values in the variadic sequences.
  • *Ts (Variadic): The variadic sequences to concatenate.

contains

comptime contains[Trait: AnyTrait[AnyType], //, type: Trait, element_types: Variadic[Trait]] = #kgen.variadic.reduce(element_types, base=False, reducer=[PrevV: Variadic[Bool], VA: Variadic[Trait], idx: __mlir_type.index] _type_is_eq_parse_time[VA[idx], type]() if _type_is_eq_parse_time[VA[idx], type]() else PrevV[0])[0]

Check if a type is contained in a variadic sequence.

Parameters

  • Trait (AnyTrait): The trait that the types conform to.
  • type (Trait): The type to check for.
  • element_types (Variadic): The variadic sequence of types to search.

contains_value

comptime contains_value[T: Equatable, //, value: T, element_values: Variadic[T]] = #kgen.variadic.reduce(element_values, base=False, reducer=[PrevV: Variadic[Bool], VA: Variadic[T], idx: __mlir_type.index] T.__eq__($0,$0)(VA[idx], value) or PrevV[0])[0]

Check if a value is contained in a variadic sequence of values.

Parameters

  • T (Equatable): The type of the values. Must be Equatable.
  • value (T): The value to search for.
  • element_values (Variadic): The variadic sequence of values to search.

empty_of_trait

comptime empty_of_trait[T: AnyTrait[AnyType]]

Empty comptime variadic of type values.

Parameters

  • T (AnyTrait): The trait that types in the variadic sequence must conform to.

empty_of_type

comptime empty_of_type[T: AnyType]

Empty comptime variadic of values.

Parameters

  • T (AnyType): The type of values in the variadic sequence.

filter_types

comptime filter_types[T: AnyTrait[AnyType], //, *element_types: T, *, predicate: [Type: T] Bool] = #kgen.variadic.reduce(element_types, base=, reducer=[PrevV: Variadic[T], VA: Variadic[T], idx: __mlir_type.index] #kgen.variadic.concat(PrevV, VA[idx]) if predicate[VA[idx]] else PrevV)

Filter types from a variadic sequence based on a predicate function.

Returns a new variadic containing only the types for which the predicate returns True.

Examples:

from std.builtin.variadics import Variadic
from std.utils import Variant
from std.sys.intrinsics import _type_is_eq

comptime FullVariant = Variant[Int, String, Float64, Bool]

# Exclude a single type
comptime IsNotInt[Type: AnyType] = not _type_is_eq[Type, Int]()
comptime WithoutInt = Variadic.filter_types[*FullVariant.Ts, predicate=IsNotInt]
comptime FilteredVariant = Variant[*WithoutInt]
# FilteredVariant is Variant[String, Float64, Bool]

# Keep only specific types
comptime IsNumeric[Type: AnyType] = (
    _type_is_eq[Type, Int]() or _type_is_eq[Type, Float64]()
)
comptime OnlyNumeric = Variadic.filter_types[*FullVariant.Ts, predicate=IsNumeric]
# OnlyNumeric is Variadic.types[T=AnyType, Int, Float64]

# Exclude multiple types using a variadic check
comptime ExcludeList = Variadic.types[T=AnyType, Int, Bool]
comptime NotInList[Type: AnyType] = not Variadic.contains[
    type=Type, element_types=ExcludeList
]
comptime Filtered = Variadic.filter_types[*FullVariant.Ts, predicate=NotInList]
# Filtered is Variadic.types[T=AnyType, String, Float64]

Filter operations can be chained for complex transformations:

comptime IsNotBool[Type: AnyType] = not _type_is_eq[Type, Bool]()
comptime Step1 = Variadic.filter_types[*FullVariant.Ts, predicate=IsNotBool]
comptime Step2 = Variadic.filter_types[*Step1, predicate=IsNotInt]
comptime ChainedVariant = Variant[*Step2]

Parameters

  • T (AnyTrait): The trait that the types conform to.
  • *element_types (T): The input variadic sequence.
  • predicate (````): A generator function that takes a type and returns Bool.

map_types_to_types

comptime map_types_to_types[From: AnyTrait[AnyType], To: AnyTrait[AnyType], //, element_types: Variadic[From], Mapper: [From: From] To] = #kgen.variadic.reduce(element_types, base=, reducer=[PrevV: Variadic[To], VA: Variadic[From], idx: __mlir_type.index] #kgen.variadic.concat(PrevV, Mapper[VA[idx]]))

Map a variadic of types to a new variadic of types using a mapper.

Returns a new variadic of types resulting from applying Mapper[T] to each type in the input variadic.

Examples:

from std.builtin.variadics import Variadic
from std.testing import *

trait MyError:
    comptime ErrorType: AnyType

struct Foo(MyError):
    comptime ErrorType = Int

struct Baz(MyError):
    comptime ErrorType = String

# Given a variadic of types [Foo, Baz]
comptime input_types = Variadic.types[T=MyError, Foo, Baz]

# And a mapper that maps the type to it's MyError `ErrorType` type
comptime mapper[T: MyError] = T.ErrorType

# The resulting variadic of types is [Int, String]
comptime output = Variadic.map_types_to_types[input_types, mapper]

assert_equal(Variadic.size(output), 2)
assert_true(_type_is_eq[output[0], Int]())
assert_true(_type_is_eq[output[1], String]())

Parameters

  • From (AnyTrait): The trait that the input types conform to.
  • To (AnyTrait): The trait that the output types conform to.
  • element_types (Variadic): The input variadic of types to map.
  • Mapper (````): A generator that maps a type to another type. The generator type is [T: From] -> To.

reverse

comptime reverse[T: AnyTrait[AnyType], //, *element_types: T] = #kgen.variadic.reduce(element_types, base=, reducer=[PrevV: Variadic[T], VA: Variadic[T], idx: __mlir_type.index] #kgen.variadic.concat(PrevV, VA[(add (mul idx, -1), len(VA), -1)]))

A wrapper to reverse a variadic sequence of types.

Parameters

  • T (AnyTrait): The trait that the types conform to.
  • *element_types (T): The variadic sequence of types to reverse.

slice_types

comptime slice_types[T: AnyTrait[AnyType], //, element_types: Variadic[T], start: Int where (start >= 0) = 0, end: Int where (end <= Variadic.size[T](element_types)) if (start <= end) else (start <= end) = Variadic.size[T](element_types)] = #kgen.variadic.reduce(element_types, base=, reducer=[PrevV: Variadic[T], VA: Variadic[T], idx: __mlir_type.index] #kgen.variadic.concat(PrevV, VA[idx]) if (idx < end) if (idx >= start) else (idx >= start) else PrevV)

Extract a contiguous subsequence from a variadic sequence.

Returns a new variadic containing elements from index start (inclusive) to index end (exclusive). Similar to Python's slice notation [start:end].

Constraints: - 0 <= start <= end <= Variadic.size(element_types)

Examples:

from std.builtin.variadics import Variadic
# Given a variadic of types [Int, String, Float64, Bool]
comptime MyTypes = Tuple[Int, String, Float64, Bool].element_types
# Extract middle elements: [String, Float64]
comptime Sliced = Variadic.slice_types[start=1, end=3, element_types=MyTypes]
# Extract first two: [Int, String]
comptime First2 = Variadic.slice_types[start=0, end=2, element_types=MyTypes]
# Extract last element: [Bool]
comptime Last = Variadic.slice_types[start=3, end=4, element_types=MyTypes]

Parameters

  • T (AnyTrait): The trait that the types conform to.
  • element_types (Variadic): The variadic sequence to slice.
  • start (Int): The starting index (inclusive).
  • end (Int): The ending index (exclusive).

splat_type

comptime splat_type[Trait: AnyTrait[AnyType], //, count: Int, type: Trait] = #kgen.variadic.tabulate(count, [idx: __mlir_type.index] type)

Splat a type into a variadic sequence.

Parameters

  • Trait (AnyTrait): The trait that the types conform to.
  • count (Int): The number of times to splat the type.
  • type (Trait): The type to splat.

splat_value

comptime splat_value[T: AnyType, //, count: Int, value: T] = #kgen.variadic.tabulate(count, [idx: __mlir_type.index] value)

Splat a value into a variadic sequence.

Parameters

  • T (AnyType): The type of the value to splat.
  • count (Int): The number of times to splat the value.
  • value (T): The value to splat.

tabulate

comptime tabulate[ToT: AnyType, //, count: Int, Mapper: [Idx: Int] ToT] = #kgen.variadic.tabulate(count, [idx: __mlir_type.index] Mapper[idx])

Apply an "index -> value" generator, N times to build a variadic.

Parameters

  • ToT (AnyType): The type of the values in the variadic sequence.
  • count (Int): The number of times to apply the generator.
  • Mapper (````): The generator to apply, mapping from Int to ToT.

tabulate_type

comptime tabulate_type[Trait: AnyTrait[AnyType], ToT: Trait, //, count: Int, Mapper: [Idx: Int] Trait] = #kgen.variadic.tabulate(count, [idx: __mlir_type.index] Mapper[idx])

Apply an "index -> value" generator, N times to build a variadic.

Parameters

  • Trait (AnyTrait): The trait that the types conform to.
  • ToT (Trait): The type of the values in the variadic sequence.
  • count (Int): The number of times to apply the generator.
  • Mapper (````): The generator to apply, mapping from Int to ToT.

types

comptime types[T: AnyTrait[AnyType], //, *Ts: T] = Ts

Turn discrete type values (bound by T) into a single variadic.

Parameters

  • T (AnyTrait): The trait that the types must conform to.
  • *Ts (T): The types to collect into a variadic sequence.

TypesOfTrait

comptime TypesOfTrait[T: AnyTrait[AnyType]] = Variadic[T]

Represents a raw variadic sequence of types that satisfy the specified trait.

Parameters

  • T (AnyTrait): The trait that types in the variadic sequence must conform to.

values

comptime values[T: AnyType, //, *values_: T] = values_

Turn discrete values (bound by T) into a single variadic.

Parameters

  • T (AnyType): The type of the values.
  • *values_ (T): The values to collect into a variadic sequence.

ValuesOfType

comptime ValuesOfType[type: AnyType] = Variadic[type]

Represents a raw variadic sequence of values of the specified type.

Parameters

  • type (AnyType): The type of values in the variadic sequence.

zip_types

comptime zip_types[Trait: AnyTrait[AnyType], //, *types: Variadic[Trait]] = #kgen.variadic.zip(types)

Zips a group of variadics of types together.

Parameters

  • Trait (AnyTrait): The trait that the types conform to.
  • *types (Variadic): The type to check for.

zip_values

comptime zip_values[type: AnyType, //, *values: Variadic[type]] = #kgen.variadic.zip(values)

Zips a group of variadics of values together.

Parameters

  • type (AnyType): The type that the values conform to.
  • *values (Variadic): The values to zip.

Methods

size

static size[T: AnyType](seq: Variadic[T]) -> Int

Returns the length of a variadic sequence.

Parameters:

  • T (AnyType): The type of values in the sequence.

Args:

  • seq (Variadic): The variadic sequence to measure.

Returns:

Int: The length of the variadic sequence.

static size[T: AnyTrait[AnyType]](seq: Variadic[T]) -> Int

Returns the length of a variadic sequence.

Parameters:

  • T (AnyTrait): The trait that types in the sequence must conform to.

Args:

  • seq (Variadic): The variadic sequence of types to measure.

Returns:

Int: The length of the variadic sequence.

Was this page helpful?