TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

From Object Oriented Programming to Functional Programming

65 pointsby vimes656over 11 years ago

4 comments

tincoover 11 years ago
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 未加载
flohofwoeover 11 years ago
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.
lelfover 11 years ago
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 未加载
jokoonover 11 years ago
Any book that sounds like &quot;functional programming in practice&quot; for C++ programmers ?