Lambdas設(shè)計(jì)

2018-11-22 14:16 更新

lambda表達(dá)式使用lambda箭頭表示==>。箭頭的左邊是匿名函數(shù)的參數(shù),右邊是大括號(hào)中的表達(dá)式或者語(yǔ)句列表{}。

<?hh

namespace Hack\UserDocumentation\Lambdas\Examples\Design\Introduction;

class User {
  public string $name;
  protected function __construct(string $name) { $this->name = $name; }
  static function get(int $id): User {
    // Load user from database, return a stub for the example
    return new User("User" . strval($id));
  }
}

function getUsersFromIds(Vector<int> $userids): Vector<User> {
  return $userids->map($id ==> User::get($id));
}

function run(): void {
  var_dump(getUsersFromIds(Vector { 1, 2, 3, 4 }));
}

run();

Output

object(HH\Vector)#3 (4) {
  [0]=>
  object(Hack\UserDocumentation\Lambdas\Examples\Design\Introduction\User)#4 (1) {
    ["name"]=>
    string(5) "User1"
  }
  [1]=>
  object(Hack\UserDocumentation\Lambdas\Examples\Design\Introduction\User)#5 (1) {
    ["name"]=>
    string(5) "User2"
  }
  [2]=>
  object(Hack\UserDocumentation\Lambdas\Examples\Design\Introduction\User)#6 (1) {
    ["name"]=>
    string(5) "User3"
  }
  [3]=>
  object(Hack\UserDocumentation\Lambdas\Examples\Design\Introduction\User)#7 (1) {
    ["name"]=>
    string(5) "User4"
  }
}

lambdas注釋

Lambdas相當(dāng)于Closures,但你不能鍵入他們注釋callable。但是,您可以:

  • 注釋傳遞或返回的lambda調(diào)用,(function(parametertypes): returntype)以提供類型信息。
  • 注釋lambda本身,參數(shù)和lambda返回類型。

你有更多的類型信息,你可以提前發(fā)現(xiàn)更多的錯(cuò)誤。

<?hh

namespace Hack\UserDocumentation\Lambdas\Examples\Design\Annotation;

function getLambda(): (function(?int): bool) {
  return $x ==> $x === null || $x === 0;
}

function annotateLambda(string $s1, string $s2): array<string> {
  $strs = array($s1, $s2);
  usort(
    $strs,
    (string $s1, string $s2): int ==> strlen($s1) - strlen($s2)
  );
  return $strs;
}
function run(): void {
  var_dump(getLambda());
  var_dump(annotateLambda('Joel', 'Tim'));
}

run();

Output

object(Closure$Hack\UserDocumentation\Lambdas\Examples\Design\Annotation\Hack\UserDocumentation\Lambdas\Examples\Design\Annotation\getLambda;1186593164)#1 (1) {
  ["parameter"]=>
  array(1) {
    ["$x"]=>
    string(10) "<required>"
  }
}
array(2) {
  [0]=>
  string(3) "Tim"
  [1]=>
  string(4) "Joel"
}

請(qǐng)注意,像一個(gè)函數(shù)的定義 Vector::filter 是:

 public function filter ( (function(Tv): bool) $callback ): Vector<Tv>

這樣你只能傳遞一個(gè)返回的lambda bool。黑客推斷類型,并會(huì)打印一個(gè)錯(cuò)誤,如果你沒(méi)有通過(guò)一個(gè)有效的封閉。因此,在許多情況下,您不必注釋lambda表達(dá)式。

括號(hào)附近的括號(hào)

大多數(shù)情況下,你需要圍繞參數(shù)括號(hào)。但是,如果只有一個(gè)沒(méi)有類型注釋的參數(shù),沒(méi)有默認(rèn)值,并且您的lambda沒(méi)有返回類型注釋,則可以省略括號(hào)。有關(guān)更多信息,請(qǐng)參閱示例。

優(yōu)先權(quán)

該==>運(yùn)營(yíng)商與其他運(yùn)營(yíng)商相比,具有較低的優(yōu)先級(jí)。這很方便,因?yàn)樗试Slambdas有一個(gè)復(fù)雜的身體,而不需要括號(hào)。此外,運(yùn)營(yíng)商是正確的聯(lián)想,可以鏈接。

<?hh

namespace Hack\UserDocumentation\Lambdas\Examples\Design\Chained;

function chainedLambdas(): void {
  $lambda = $x ==> $y ==> $x - $y;
  $f = $lambda(4); // You are providing $x the value 4
  echo $f(2); // You are providing $y the value 2; thus this prints 2
}

function run(): void {
  chainedLambdas();
}

run();

Output

2
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)