我正在教授一門課程,其中包括解釋函式式 JavaScript,我希望有一個非常好的函式式編程示例,希望它比非函式式更簡潔。我想將以下 switch 陳述句轉換為函式式。我自己做了一個轉換的例子,但希望有一個更簡單的解決方案。
這是 switch 陳述句版本:
let animalType = "Poodle";
switch (animalType) {
case "Poodle":
case "Beagle":
case "Bulldog":
console.log(animalType " is a dog.");
break;
case "Bengal":
case "Siamese":
console.log(animalType " is a cat.");
break;
default:
console.log(animalType " is not a dog or cat.");
break;
}
這就是我想出的功能,但我對此并不滿意
const result = getAnimalType("Poodle");
console.log("result:" result)
function getAnimalType(animal) {
function isDog(animal) {
const dogs = ["Poodle", "Beagle", "Bulldog"];
return dogs.includes(animal)
}
function isCat(animal) {
const cats = ["Bengal", "Siamese"];
return cats.includes(animal)
}
return isDog(animal)
? animal " is a dog."
: isCat(animal)
? animal " is a cat."
: animal " is not a dog or cat.";
}
uj5u.com熱心網友回復:
一種選擇是由 dog 或 cat 索引的物件,其值是動物型別的陣列。這很容易擴展到其他動物型別。
const animalNamesByType = {
dog: ["Poodle", "Beagle", "Bulldog"],
cat: ["Bengal", "Siamese"]
};
function getAnimalType(animal) {
const entry = Object.entries(animalNamesByType).find(
entry => entry[1].includes(animal)
);
return entry
? `${animal} is a ${entry[0]}`
: `${animal} is not in animalNamesByType`;
}
console.log(getAnimalType("Poodle"));
uj5u.com熱心網友回復:
您可以使用物件將動物型別映射到函式。
function dog(animalType) {
return animalType " is a dog.";
}
function cat(animalType) {
return animalType " is a cat.";
}
function other(animalType) {
return animalType " is not a dog or cat.";
}
const typeMap = {
Poodle: dog,
Beagle: dog,
Bulldog: dog,
Bengal: cat,
Siamese: cat
};
function getAnimalType(animalType) {
let typeFun = typeMap[animalType] || other;
return typeFun(animalType);
}
console.log(getAnimalType("Poodle"));
uj5u.com熱心網友回復:
您可以為此創建一個非常簡單的 3 行函式
const dogs = ["Poodle", "Beagle", "Bulldog"];
const cats = ["Bengal", "Siamese"];
const getAnimalType = (animal) => {
if(dogs.includes(animal)) return `${animal} is a dog`
if(cats.includes(animal)) return `${animal} is a cat`
return `${animal} is not a dog or cat.`
}
const result = getAnimalType("Poodle");
console.log("result:" result)
uj5u.com熱心網友回復:
一個變種,有點像 Barmar 的,但對我來說仍然是個人的
const getAnimalType = (() =>
{
const
isX =
{ dog : 'is a dog'
, cat : 'is a cat'
, nDC : 'is neither a dog nor a cat'
}
, typeMap =
{ Poodle : 'dog'
, Beagle : 'dog'
, Bulldog : 'dog'
, Bengal : 'cat'
, Siamese : 'cat'
};
return (animal) => `${animal} ${isX[ typeMap[animal] ?? 'nDC']}`
})()
console.log(getAnimalType('Beagle'))
console.log(getAnimalType('Bengal'))
console.log(getAnimalType('schtroumpf'))
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}
uj5u.com熱心網友回復:
switch
從函式式編程的角度來看并沒有錯。問題在于它console.log
作為副作用呼叫,而不是回傳值。不過很容易修復:
function getAnimalType(animalType) {
switch (animalType) {
case "Poodle":
case "Beagle":
case "Bulldog":
return animalType " is a dog.";
case "Bengal":
case "Siamese":
return animalType " is a cat.";
default:
return animalType " is not a dog or cat.";
}
}
console.log(getAnimalType("Poodle"));
下一個改進可能是避免一些重復:
function getAnimalType(animalType) {
switch (animalType) {
case "Poodle":
case "Beagle":
case "Bulldog":
return "a dog";
case "Bengal":
case "Siamese":
return "a cat";
default:
return "not a dog or cat";
}
}
function getStatement(animalType) {
return animalType " is " getAnimalType(animalType) ".";
}
console.log(getStatement("Poodle"));
uj5u.com熱心網友回復:
通過編譯 TypeScript 列舉代碼示例,我得到了與此類似的東西:
var AnimalType;
(function (AnimalType) {
AnimalType[AnimalType["Poodle"] = 0] = "Poodle";
AnimalType[AnimalType["Beagle"] = 1] = "Beagle";
AnimalType[AnimalType["Bulldog"] = 2] = "Bulldog";
AnimalType[AnimalType["Bengal"] = 3] = "Bengal";
AnimalType[AnimalType["Siamese"] = 4] = "Siamese";
})(AnimalType || (AnimalType = {}));
function whichKind(animalType) {
let kind;
switch (animalType) {
case AnimalType.Poodle:
kind = `${AnimalType[animalType]} is a dog`;
break;
case AnimalType.Beagle:
kind = `${AnimalType[animalType]} is a dog`;
break;
case AnimalType.Bulldog:
kind= `${AnimalType[animalType]} is a dog`;
break;
case AnimalType.Bengal:
kind = `${AnimalType[animalType]} is a cat`;
break;
case AnimalType.Siamese:
kind = `${AnimalType[animalType]} is a cat`;
break;
}
kind = typeof kind === 'undefined' ? `${animalType} is not a dog or cat.` : kind;
return kind;
}
console.log(whichKind(AnimalType.Poodle)); // Poodle is a dog
// console.log(whichKind('Other')); // Other is not a dog or cat.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/492010.html
標籤:javascript 函数式编程