Macaulay2 » Documentation
Packages » Macaulay2Doc » The Macaulay2 language » hash tables » pairs
next | previous | forward | backward | up | index | toc

pairs -- list the pairs in a hash table, dictionary, or basic list

Description

Synopsis

Synopsis

  • Usage:
    pairs x
  • Inputs:
    • x, a thing, an instance of a class with an iterator method installed;
  • Outputs:

If x is a hash table or dictionary, the pairs consist of each key along with its associated value.

i1 : x = new HashTable from {a => 1, b => 2, c => 3}

o1 = HashTable{a => 1}
               b => 2
               c => 3

o1 : HashTable
i2 : pairs x

o2 = {(c, 3), (a, 1), (b, 2)}

o2 : List

A dictionary is a special hash table whose keys are strings, and whose values are the corresponding symbols.

i3 : d = new Dictionary

o3 = d

o3 : GlobalDictionary
i4 : getGlobalSymbol (d, "foo")

o4 = foo

o4 : Symbol
i5 : getGlobalSymbol (d, "bar")

o5 = bar

o5 : Symbol
i6 : pairs d

o6 = {(bar, bar), (foo, foo)}

o6 : List
i7 : first oo

o7 = (bar, bar)

o7 : Sequence
i8 : class \ oo

o8 = (String, Symbol)

o8 : Sequence

If x is a basic list, the pairs consist of each index along with the element at that index in the list.

i9 : L = {3, 5, 7};
i10 : pairs L

o10 = {(0, 3), (1, 5), (2, 7)}

o10 : List
i11 : pairs {apple, banana, carrot}

o11 = {(0, apple), (1, banana), (2, carrot)}

o11 : List

If x belongs to any other class, then iterator is called on it, and if successful, an Iterator object is returned.

i12 : pairs "foo"

o12 = iterator "foo"

o12 : Iterator
i13 : toList oo

o13 = {(0, f), (1, o), (2, o)}

o13 : List
i14 : i = pairs Iterator(() -> 5)

o14 = i

o14 : Iterator
i15 : next i

o15 = (0, 5)

o15 : Sequence
i16 : next i

o16 = (1, 5)

o16 : Sequence
i17 : next i

o17 = (2, 5)

o17 : Sequence

Caveat

As the first example illustrates, pairs are not necessarily listed in any particular order.

See also

Ways to use pairs:

  • pairs(BasicList)
  • pairs(Dictionary)
  • pairs(HashTable)
  • pairs(Thing)

For the programmer

The object pairs is a compiled function.


The source of this document is in Macaulay2Doc/ov_hashtables.m2:482:0.