TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

From Object Oriented Programming to Functional Programming

65 点作者 vimes656超过 11 年前

4 条评论

tinco超过 11 年前
Perhaps the problem is that you&#x27;re trying to build a class hierarchy in Haskell, when class hierarchies just don&#x27;t really work in Haskell.<p>What about a component oriented design? This is the standard architecture for games anyway.<p>An enemy, actually every <i>entity</i>, would just be a map of components, which can be simple algebraic data types. The actions can be implemented as a system function with pattern matching or guards, like:<p><pre><code> -- The walking system walk :: Entity -&gt; Entity walk e = update entity &quot;position&quot; $ walk&#x27; (lookup &quot;position&quot; entity) walk&#x27; (x,y) = (x+1,y) </code></pre> In reality systems usually work on more than one component, so you&#x27;d have to make actions that work on tuples of components, but you&#x27;ll figure it out :)
评论 #6909280 未加载
flohofwoe超过 11 年前
The proposed class hierarchy for different types of &quot;game objects&quot; is exactly the type of hierarchy which will bite you in the ass very soon when adding more features, an entity-component-system would be a better solution for this specific problem (composition instead of inheritance). Newbie programmers should never be taught this &quot;animal - cat - dog&quot; nonsense IMHO because this approach of modelling software after real-world objects doesn&#x27;t work for anything slightly more complex. For instance, what if you have two different enemy types which both need to implement a common feature but differ in others? Please don&#x27;t say &#x27;multiple inheritance&#x27; ;) This doesn&#x27;t mean that OOP is bad at all IMHO, just that it shouldn&#x27;t be treated as a holy grail (applies to all design principles), and doesn&#x27;t mean that you have to go all-functional. All IMHO of course.
lelf超过 11 年前
These exercises have nothing to do with expression problem.<p>The goal of expression problem is ability to add new cases in type and new operations on type <i>without touching</i> existing code (and without compromising static type safety)
评论 #6909292 未加载
jokoon超过 11 年前
Any book that sounds like &quot;functional programming in practice&quot; for C++ programmers ?