Skip to content

Commit

Permalink
Fix types for key-based dict/keyset sorting functions (#103)
Browse files Browse the repository at this point in the history
Summary:
The builtins are no longer typed as returning a generic, as they can
convert a `vec` to a `dict` when keys have defined behavior in the
function.

Changed by D18937742
Pull Request resolved: #103

Test Plan: TravisCI

Reviewed By: rodmk

Differential Revision: D19027560

Pulled By: fredemmott

fbshipit-source-id: d3d63ab8427046bae73ba8748170ee4a6e31f018
  • Loading branch information
fredemmott authored and facebook-github-bot committed Dec 13, 2019
1 parent 94c81cd commit e6abfde
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
19 changes: 7 additions & 12 deletions src/dict/order.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ function shuffle<Tk as arraykey, Tv>(
function sort<Tk as arraykey, Tv>(
<<__MaybeMutable, __OnlyRxIfImpl(\HH\Rx\KeyedTraversable::class)>>
KeyedTraversable<Tk, Tv> $traversable,
<<__AtMostRxAsFunc>>
?(function(Tv, Tv): int) $value_comparator = null,
<<__AtMostRxAsFunc>> ?(function(Tv, Tv): int) $value_comparator = null,
): dict<Tk, Tv> {
$result = dict($traversable);
if ($value_comparator) {
Expand All @@ -79,7 +78,7 @@ function sort<Tk as arraykey, Tv>(
/* HH_IGNORE_ERROR[4107] __PHPStdLib */
\asort(inout $result);
}
return $result;
return dict($result);
}

/**
Expand All @@ -100,14 +99,11 @@ function sort<Tk as arraykey, Tv>(
function sort_by<Tk as arraykey, Tv, Ts>(
<<__MaybeMutable, __OnlyRxIfImpl(\HH\Rx\KeyedTraversable::class)>>
KeyedTraversable<Tk, Tv> $traversable,
<<__AtMostRxAsFunc>>
(function(Tv): Ts) $scalar_func,
<<__AtMostRxAsFunc>>
?(function(Ts, Ts): int) $scalar_comparator = null,
<<__AtMostRxAsFunc>> (function(Tv): Ts) $scalar_func,
<<__AtMostRxAsFunc>> ?(function(Ts, Ts): int) $scalar_comparator = null,
): dict<Tk, Tv> {
$tuple_comparator = $scalar_comparator
? ((Ts, Tv) $a, (Ts, Tv) $b) ==>
$scalar_comparator($a[0], $b[0])
? ((Ts, Tv) $a, (Ts, Tv) $b) ==> $scalar_comparator($a[0], $b[0])
/* HH_FIXME[4240] need Scalar type */
: ((Ts, Tv) $a, (Ts, Tv) $b) ==> $a[0] <=> $b[0];
return $traversable
Expand All @@ -132,8 +128,7 @@ function sort_by<Tk as arraykey, Tv, Ts>(
function sort_by_key<Tk as arraykey, Tv>(
<<__MaybeMutable, __OnlyRxIfImpl(\HH\Rx\KeyedTraversable::class)>>
KeyedTraversable<Tk, Tv> $traversable,
<<__AtMostRxAsFunc>>
?(function(Tk, Tk): int) $key_comparator = null,
<<__AtMostRxAsFunc>> ?(function(Tk, Tk): int) $key_comparator = null,
): dict<Tk, Tv> {
$result = dict($traversable);
if ($key_comparator) {
Expand All @@ -147,5 +142,5 @@ function sort_by_key<Tk as arraykey, Tv>(
/* HH_IGNORE_ERROR[4107] __PHPStdLib */
\ksort(inout $result);
}
return $result;
return dict($result);
}
5 changes: 2 additions & 3 deletions src/keyset/order.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
function sort<Tv as arraykey>(
<<__MaybeMutable, __OnlyRxIfImpl(\HH\Rx\Traversable::class)>>
Traversable<Tv> $traversable,
<<__AtMostRxAsFunc>>
?(function(Tv, Tv): int) $comparator = null,
<<__AtMostRxAsFunc>> ?(function(Tv, Tv): int) $comparator = null,
): keyset<Tv> {
$keyset = keyset($traversable);
if ($comparator) {
Expand All @@ -38,5 +37,5 @@ function sort<Tv as arraykey>(
/* HH_IGNORE_ERROR[4107] __PHPStdLib */
\ksort(inout $keyset);
}
return $keyset;
return keyset($keyset);
}

0 comments on commit e6abfde

Please sign in to comment.