iyi

Standard library, 0.18.0

std/tuple

Size, equality, each and printing for the prelude's Tuple and NamedTuple.

import std/tuple to use it. Source: src/std/tuple.iyi at v0.18.0.

Heterogeneous tuples cannot implement Indexable (one Elem). This module adds what the compiler does not: size, empty?, first/last, ==, each, to_s.

NamedTuple(T)struct, reopened

std/tupleiyi doc src/std/tuple.iyi
struct ::NamedTuple(T)
  # Compares the values by name.
  #
  # Any named tuple: the keys are sorted and compared, then the values in
  # that order as tuples compare (src/iyi/object.iyi), member by member
  # where one type holds the other. `self` took `{a: 1, b: 2}` for `{a: 1,
  # b: y}` (y : Int32?) and not back, so the pair answered true one way and
  # false the other, and `case` over them did not match.
  def ==(other : NamedTuple) : Bool
  # Yields each key symbol and corresponding value.
  def each : Nil
  # Yields each value in order.
  def each_value : Nil
  # Returns true when the named tuple contains zero entries.
  def empty? : Bool
  # Returns true if the named tuple contains the given key symbol.
  #
  # The keys here and below are written with `symbolize`, never as
  # `:{{key.id}}`: a quoted key such as `"content-type"` or `"x y"` pasted
  # that way is `:content-type`, a symbol minus a call, or `:x y`, which
  # does not parse, so every one of these methods failed to compile for
  # such a key. `std/named_tuple` already wrote them this way.
  def has_key?(key : Symbol) : Bool
  # Hashing follows the `==` below, mixed as `Tuple#hash` mixes the members
  # (src/iyi/object.iyi). `Object#hash` answered the type's id, so
  # `{a: 1}` and `{a: 2}` hashed alike and 20,000 keys took 1.4 s to insert
  # where the same pairs as tuples took 1 ms. The values go in by sorted
  # key, because `==` matches by name: `{x: 1, y: 2} == {y: 2, x: 1}`, so
  # the two have to hash alike although their types list the keys in
  # another order.
  def hash : Int32
  def initialize
  def inspect : String
  # Returns a Tuple containing the key symbols in order. Built with
  # `Tuple.new`, as `Tuple#map` is: for the named tuple with no keys,
  # braces are an empty hash literal.
  def keys
  # Value identity.
  def same?(other : self) : Bool
  # Number of key-value pairs in the named tuple.
  def size : Int32
  # The keys, and the values, in sorted key order: the order a type lists
  # its keys in is not part of the value.
  protected def sorted_keys
  protected def sorted_values
  # String representation: `{name: "Alice", age: 30}`. A key that is not a
  # name is quoted, as it is written: `{"content-type": "text/plain"}`.
  def to_s : String
  # Returns a Tuple containing the values in order.
  def values
end

Tuple(*T)struct, reopened

std/tupleiyi doc src/std/tuple.iyi
struct ::Tuple(*T)
  # Concatenates two tuples.
  def +(other : Tuple(*U)) forall U
  # Yields each element sequentially.
  def each : Nil
  # Yields each element alongside its index.
  def each_with_index : Nil
  # Returns true when the tuple contains zero elements.
  def empty? : Bool
  # Returns the first element. Raises if empty.
  def first
  # Returns the first element, or nil if empty.
  def first?
  # Returns the last element. Raises if empty.
  def last
  # Returns the last element, or nil if empty.
  def last?
  # Returns a new Tuple with each element mapped through the block.
  #
  # This, `+`, `reverse`, and `NamedTuple#keys` and `#values` build with
  # `Tuple.new(...)`, not braces: with no members `{ }` is an empty hash
  # literal, so on the empty tuple each failed to compile with "for empty
  # hashes use '{} of KeyType => ValueType'".
  def map
  # Creates a new Tuple containing the given arguments.
  def self.new(*args : *T)
  # Reverses elements in tuple.
  def reverse
  # Value identity.
  def same?(other : self) : Bool
  # Converts the tuple into an Array typed as the union of all element types.
  # The empty tuple's is `[] of NoReturn`, the union of no types: the
  # `typeof` below had no argument there, and `Tuple.new.to_a` did not
  # compile.
  def to_a
end