Arrays(2) Arrays(2) NAME arrays: find, append, prepend, tail, pair, filter, map- array operations SYNOPSIS include "arrays.m" arrays := load Arrays Arrays->PATH; find: fn[T](a: array of T, x: T): array of T for { T => Equals: fn(a, b: T): int; }; append: fn[T](a₀: array of T, x: T): array of T; prepend: fn[T](a₀: array of T, x: T): array of T; tail: fn[T](a: array of T): array of T; pair: fn[T₁, T₂](a₁: array of T₁, a₂: array of T₂): array of (T₁, T₂); stringify: fn[T](a: array of T): string for { T => String: fn(a: self T): string; }; filter: fn[T](a: array of T, f: ref fn(x: T): int): array of T; map: fn[T](a₀: array of T, f: ref fn(x: T): T): array of T; DESCRIPTION The module arrays provides operations on arrays of type T, which may be any reference type, or string. Reference types are array, chan, array, module, and ref adt. None of the operations change their parameter arrays: they always return a new array. First, there is a group of common array operations. Find finds the first instance of x in array a and returns the slice a[x:]. Find returns nil if there is no instance of x in a. The type T must implement a method Equals which returns a number greater than zero if the elements a and b are equal and zero if a and b are not equal. Append returns a new array with the elements of a₀ followed by the element x. Prepend returns a new array with the element x followed by the elements of a₀. Tail returns a new array which is the slice a[1:]. Nil is returned if the length of a is less than 2 elements. Page 1 Plan 9 (printed 12/3/24) Arrays(2) Arrays(2) Pair takes two arrays a₁ and a₂, and returns a new array of tuples (v1, v2) in which each element of a₁ has been paired with the corresponding element of a₂. Nil is returned if the arrays are not equal in length. Stringify returns a visually pleasing string representation of a. The type T must implement a method String which returns a string representation of an element in the array of type T. A second group of operations applies a function f or a Boolean predicate p to each element of a array, returning a transformed array or a Boolean value. A predicate p must return non-zero if its parameter value satisfies its condi- tion, and must return zero if it does not. Filter returns a new array containing only the elements of a that satisfy p. Map returns a new array in which each element of a₀ has been transformed by f (ie, if a₀ is e0,e1,⋯ then the result is f(e0),f(e1),⋯). SOURCE /appl/lib/arrays.b BUGS The current implementation of polymorphism is limited to reference types and strings, which in turn limits use of this module. SEE ALSO lists(2) Page 2 Plan 9 (printed 12/3/24)