Skip to content

Commit

Permalink
Dict\flatten
Browse files Browse the repository at this point in the history
Summary: Adding for parity with `{Keyset, Vec}\flatten`.

Reviewed By: fredemmott

Differential Revision: D6011560

fbshipit-source-id: 3527e9e5358a6795242f56dc5dd6ffe34e4a9cad
  • Loading branch information
kmeht authored and facebook-github-bot committed Oct 9, 2017
1 parent 9691f07 commit c5a3f28
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 7 deletions.
8 changes: 1 addition & 7 deletions src/dict/combine.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,5 @@ function associate<Tk as arraykey, Tv>(
function merge<Tk, Tv>(
KeyedTraversable<Tk, Tv> ...$traversables
): dict<Tk, Tv> {
$result = dict[];
foreach ($traversables as $traversable) {
foreach ($traversable as $key => $value) {
$result[$key] = $value;
}
}
return $result;
return namespace\flatten($traversables);
}
19 changes: 19 additions & 0 deletions src/dict/transform.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ function count_values<Tv as arraykey>(
return $result;
}

/**
* Returns a new dict formed by merging the KeyedTraversable elements of the
* given Traversable. In the case of duplicate keys, later values will overwrite
* the previous ones.
*
* For a fixed number of KeyedTraversables, see `Dict\merge`.
*/
function flatten<Tk, Tv>(
Traversable<KeyedTraversable<Tk, Tv>> $traversables,
): dict<Tk, Tv> {
$result = dict[];
foreach ($traversables as $traversable) {
foreach ($traversable as $key => $value) {
$result[$key] = $value;
}
}
return $result;
}

/**
* Returns a new dict where all the given keys map to the given value.
*/
Expand Down
77 changes: 77 additions & 0 deletions tests/dict/DictTransformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,83 @@ public function testFillKeys<Tk as arraykey, Tv>(
expect(Dict\fill_keys($keys, $value))->toBeSame($expected);
}

public static function provideTestFlatten(): array<mixed> {
return array(
tuple(
array(),
dict[],
),
tuple(
array(
array(
'one' => 'one',
'two' => 'two',
),
array(
'three' => 'three',
'one' => 3,
),
Map {
'four' => null,
},
),
dict[
'one' => 3,
'two' => 'two',
'three' => 'three',
'four' => null,
],
),
tuple(
array(
HackLibTestTraversables::getKeyedIterator(array(
'foo' => 'foo',
'bar' => 'bar',
'baz' => array(1, 2, 3),
)),
dict[
'bar' => 'barbar',
],
Vector {'I should feel bad for doing this', 'But yolo'},
array(
'1' => 'gross array behavior',
),
Set {'bloop'},
),
dict[
'foo' => 'foo',
'bar' => 'barbar',
'baz' => array(1, 2, 3),
0 => 'I should feel bad for doing this',
1 => 'gross array behavior',
'bloop' => 'bloop',
],
),
tuple(
HackLibTestTraversables::getIterator(array(
vec['zero'],
array(1 => 'one'),
dict[2 => 'two'],
Map {3 => 'three'},
)),
dict[
0 => 'zero',
1 => 'one',
2 => 'two',
3 => 'three',
],
),
);
}

/** @dataProvider provideTestFlatten */
public function testFlatten<Tk, Tv>(
Traversable<KeyedTraversable<Tk, Tv>> $traversables,
dict<Tk, Tv> $expected,
): void {
expect(Dict\flatten($traversables))->toBeSame($expected);
}

public static function provideTestFlip(): array<mixed> {
return array(
tuple(
Expand Down

0 comments on commit c5a3f28

Please sign in to comment.