1/4: add real HSL to HHVM repo :)
[hiphop-php.git] / hphp / hsl / src / vec / combine.php
blob7e6d40aa95a1b7ae09d7763c0fa8b8d0f52f5661
1 <?hh
2 /*
3 * Copyright (c) 2004-present, Facebook, Inc.
4 * All rights reserved.
6 * This source code is licensed under the MIT license found in the
7 * LICENSE file in the root directory of this source tree.
9 */
11 namespace HH\Lib\Vec;
13 use namespace HH\Lib\{C, Math};
15 /**
16 * Returns a new vec formed by concatenating the given Traversables together.
18 * For a variable number of Traversables, see `Vec\flatten()`.
20 * Time complexity: O(n + m), where n is the size of `$first` and m is the
21 * combined size of all the `...$rest`
22 * Space complexity: O(n + m), where n is the size of `$first` and m is the
23 * combined size of all the `...$rest`
25 function concat<Tv>(
26 Traversable<Tv> $first,
27 Container<Tv> ...$rest
28 )[]: vec<Tv> {
29 $result = cast_clear_legacy_array_mark($first);
30 foreach ($rest as $traversable) {
31 foreach ($traversable as $value) {
32 $result[] = $value;
35 return $result;
38 /**
39 * Returns a vec where each element is a tuple (pair) that combines, pairwise,
40 * the elements of the two given Traversables.
42 * If the Traversables are not of equal length, the result will have
43 * the same number of elements as the shortest Traversable.
44 * Elements of the longer Traversable after the length of the shorter one
45 * will be ignored.
47 * Time complexity: O(min(m, n)), where m is the size of `$first` and n is the
48 * size of `$second`
49 * Space complexity: O(min(m, n)), where m is the size of `$first` and n is the
50 * size of `$second`
52 <<__ProvenanceSkipFrame>>
53 function zip<Tv, Tu>(
54 Traversable<Tv> $first,
55 Traversable<Tu> $second,
56 )[]: vec<(Tv, Tu)> {
57 $one = cast_clear_legacy_array_mark($first);
58 $two = cast_clear_legacy_array_mark($second);
59 $result = vec[];
60 $lesser_count = Math\minva(C\count($one), C\count($two));
61 for ($i = 0; $i < $lesser_count; ++$i) {
62 $result[] = tuple($one[$i], $two[$i]);
64 return $result;