Overview
Values that combine in order, such as the results of the parts of a collection.
(import (std combine))
(combine [1 2] [3]) ; [1 2 3]
(combine-all ["ab" "c" "de"]) ; "abcde"
(combine (Tuple [1] "a") (Tuple [2] "b")) ; (Tuple [1 2] "ab")
(combine a b) is the value of a run over the elements of a
followed by those of b. in-parallel of (std parallel)
uses it to join the results of the parts, in the order of the parts.
Laws. combine is associative: (combine (combine a
b) c) equals (combine a (combine b c)). It is not commutative.
(combine-all xs) equals combine from the left over
xs.
No mutation. combine and combine-all change no
argument. The result may share structure with them.
One meaning for each type. A type is Combinable only
where it has one meaning. Numbers and maps have none: a number may be a
sum, a count or a maximum, and a key in both maps may keep either value
or both combined. Such a result is a type of its own, with its own
implementation.
Implementations. Unit; Vec, List,
IArray and string, one after the other; HashSet
and OrderedSet, the union; tuples of two to six elements, element
by element. Array has none, because it is mutable: to fill an
array in parts, make it first and let each part write its own range.
See also: Combinable, combine, combine-all
Reference
Types
Combinable
trait ↑ Types
Trait for values that combine in order.
%a- The value type.
An implementation defines combine. It defines
combine-all when combining all values at once costs less than
combining them two at a time, as it does when each combine copies.
See also: combine, combine-all
Functions
combine
function ↑ Functions
(: combine (-> %a %a %a))
Combines two values: the elements of a, then those of b.
a- The first value. It is not changed.
b- The second value. It is not changed.
- returns
- The combined value.
(combine [1 2] [3]) ; [1 2 3]
(combine "ab" "c") ; "abc"
See also: combine-all, Combinable
combine-all
function ↑ Functions
(: combine-all (-> (Vec %a) %a))
Combines the values of a vector, in order.
xs- The values. At least one.
- returns
- The values combined from the first to the last.
(combine-all [[1] [2 3] []]) ; [1 2 3]
The default combines from the left. A list’s combines from the right, so that each list but the last is copied once. A string’s and an immutable array’s copy each value once.
See also: combine, Combinable