W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
在這里你可以找到一些lambda表達(dá)式的變種。
如果它是一個(gè)單一的非類型化參數(shù),可以省略括號(hào)。否則,parenthesis是必需的。
<?hh
namespace Hack\UserDocumentation\Lambdas\Examples\Examples\SyntaxExamples;
function addLastname(): Vector<string> {
$people = Vector {
"Carlton",
"Will",
"Phil"
};
// You need parentheses when adding a type annotation or default value
$annotatedArgument = (string $name) ==> $name . " Banks";
$annotatedReturnType = ($name): string ==> $name . " Banks";
$defaultValue = (string $name = "Ashley") ==> $name . " Banks";
// You could use any of the above closures.
return $people->map($annotatedReturnType);
}
function calculateYears(): int {
// You need parentheses when using more than one argument
$difference = ($start, $end) ==> $end - $start;
return $difference(1990, 1996);
}
function familySize(): int {
$people = Vector {
"Carlton",
"Will",
"Phil"
};
// You can use curly braces to create a compound statement
$calculateSize = $family ==> {
$counter = 0;
foreach ($family as $member) {
$counter++;
}
return $counter;
};
return $calculateSize($people);
}
function run(): void {
var_dump(familySize());
var_dump(calculateYears());
var_dump(addLastname());
}
run();
Output
int(3)
int(6)
object(HH\Vector)#4 (3) {
[0]=>
string(13) "Carlton Banks"
[1]=>
string(10) "Will Banks"
[2]=>
string(10) "Phil Banks"
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: