Macaulay2 » Documentation
Packages » Macaulay2Doc :: pack
next | previous | forward | backward | up | index | toc

pack -- pack elements of a list or string into several shorter lists or strings

Synopsis

Description

The commands pack(A, n) and pack(n, A) produce identical results.

i1 : pack(a..l, 3)

o1 = {{a, b, c}, {d, e, f}, {g, h, i}, {j, k, l}}

o1 : List
i2 : pack(3, a..l)

o2 = {{a, b, c}, {d, e, f}, {g, h, i}, {j, k, l}}

o2 : List
i3 : pack("Hello world!", 4)

o3 = {Hell, o wo, rld!}

o3 : List
i4 : pack(4, "Hello world!")

o4 = {Hell, o wo, rld!}

o4 : List

If n doesn't divide the length of A, the last list will have fewer than n elements.

i5 : pack(a..m, 3)

o5 = {{a, b, c}, {d, e, f}, {g, h, i}, {j, k, l}, {m}}

o5 : List

pack and mingle can be used together to take a transpose of lists

i6 : pack(2, mingle(a..m, 0..12))

o6 = {{a, 0}, {b, 1}, {c, 2}, {d, 3}, {e, 4}, {f, 5}, {g, 6}, {h, 7}, {i, 8},
     ------------------------------------------------------------------------
     {j, 9}, {k, 10}, {l, 11}, {m, 12}}

o6 : List

See also

Ways to use pack:

For the programmer

The object pack is a method function.