根据 "or" 和 "and" 对世界建模
Modelling world in terms of "or" and "and"
托马斯用户 :
I think modelling the world in terms of "or" (aka discriminated union) and "and" (aka records) is a very powerful functional trick. Sometimes, functions/interfaces are useful too, but if I tend to stick to "or"/"and" as much as I can :-)
假设我正在为 Animal
的世界建模(开放世界假设),我如何通过使用可区分的联合、记录以及有时 functions/interfaces 来做到这一点?
比如animal types,可以用discriminated union来分类吗?
type Type =
| Phyla of Phyla
| Chordata of Chordata
| Arthropods of Arthropods
| ...
and Chordata =
| Fish
| Reptiles
| Mammals
| ...
函数式编程和面向对象编程在用词重点上有所不同。 "Function" 表示关注动词,而 "object" 表示关注名词。
您可能会因为问题描述中没有动词而感到吃力。你在评论中提到了一个动物王国模拟器;那个世界会发生什么样的事情?我假设动物不会一动不动地坐着 :-)
托马斯用户
I think modelling the world in terms of "or" (aka discriminated union) and "and" (aka records) is a very powerful functional trick. Sometimes, functions/interfaces are useful too, but if I tend to stick to "or"/"and" as much as I can :-)
假设我正在为 Animal
的世界建模(开放世界假设),我如何通过使用可区分的联合、记录以及有时 functions/interfaces 来做到这一点?
比如animal types,可以用discriminated union来分类吗?
type Type =
| Phyla of Phyla
| Chordata of Chordata
| Arthropods of Arthropods
| ...
and Chordata =
| Fish
| Reptiles
| Mammals
| ...
函数式编程和面向对象编程在用词重点上有所不同。 "Function" 表示关注动词,而 "object" 表示关注名词。
您可能会因为问题描述中没有动词而感到吃力。你在评论中提到了一个动物王国模拟器;那个世界会发生什么样的事情?我假设动物不会一动不动地坐着 :-)