在JavaScript中寻找切换语句的干净函数实现
我正在教授一门课程,其中包括解释功能性JavaScript,我想拥有一个非常好的功能编程的例子,该功能编程希望比非功能更清洁。我想将以下开关语句转换为功能。我自己以这种转换为例,但是希望有一个简单的解决方案。
这是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.";
}
I am teaching a course that includes explaining functional JavaScript and I want to have a really good example of functional programming that is hopefully cleaner then non-functional. I want to convert the following switch statement to functional. I've made an example of that conversion myself, but hoping there is a simpler solution.
Here is the switch statement version:
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;
}
And here is what I came up with as functional that I'm not that happy about
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.";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用对象将动物类型映射到功能。
You can use an object to map animal types to functions.
一种选择是由狗或猫索引的对象,其值是动物类型的阵列。这对于其他动物类型很容易扩展。
One option is an object indexed by dog or cat, whose values are arrays of animal types. This is easily extensible to additional animal types.
您可以为此创建一个非常简单的3线功能
You can create a really simple 3-line function for this
一个变体,有点像Barmar的变体,但对我来说仍然是个人的
a variant, a bit like Barmar's, but which remains personal to me
从功能编程的角度来看,
switch
没有错。问题是,它将console.log
称为副作用,而不是返回值。但是,易于修复:下一个改进可能避免重复:
There's nothing wrong with
switch
from the perspective of functional programming. The problem rather is that it callsconsole.log
as a side effect, instead of returning a value. Easy to fix, though:Next improvement might be avoiding some duplication:
在这种情况下,使用地图将是我首选的选择。问题是如何。我喜欢咖喱功能,所以我会使用这些功能:
这里发生了什么?
我们有一个函数获取地图,然后返回一个函数,该功能最终接收动物并返回消息。关键是要意识到我们还使用IIFE:
函数
AnimalType
是“硬编码”来与我们提供的type
一起使用的。现在,它只是等待Animal
参数进来。但是,这似乎是我们可以抽象和重复使用的东西:
lookup
功能取得成功函数Pass 和失败函数
并将失败
和初始地图
。然后,它返回一个<代码>键Pass
应用于键>键
和相应值(如果密钥存在于地图中的)的函数。否则,它将
Fail
应用于密钥。因此,我们可以以这种方式构建
AnimalType
:Working with maps would be my preferred option in this case. The question is how. I like curried functions so I'll use those:
What's going on here?
We have a function that takes a map then returns a function that finally takes an animal and returns a message. The key is to realise that we also use an IIFE:
The function
animalType
is "hardcoded" to work with thetype
we gave it. Now it just waits for theanimal
parameter to come in.However this seems like something we could abstract and reuse:
The
lookup
function takes a success functionpass
and a failure functionfail
and an initialmap
. Then it returns a function that takes akey
and appliespass
tokey
and the corresponding value if the key exists in the map. Otherwise it appliesfail
to the key.With that we can build
animalType
this way:通过编译打字稿枚举代码示例,我得到了与此类似的东西:
I get something similar to this by compiling a TypeScript enum code example: