Comments (3)
You are certainly correct. It is my fault for not explaining that it is bad code on purpose. I just pushed: d51c5b5
from mostly-adequate-guide.
Please explain why 32 is incorrect and 16 is correct. In your seagull class, a flock is able to conjoin with itself and double its own size, therefor 32 is correct. A simple check for equality of flocks in the conjoin method would have prevented this unnatural behavior:
Flock.prototype.conjoin = function(other) {
if (this === other) {
throw('a flock cannot conjoin with itself!')
}
this.seagulls += other.seagulls;
return this;
};
But I don't see a way for the functional approach to prevent self-conjoining, since the functional code is operating on number literals instead of objects. To me, this example is actually an argument against FP.
from mostly-adequate-guide.
Hi @omasback
You have a good point - if flock_a
were to breed with flock_b
twice, its state probably should be altered and the object would keep track for us.
The intention was to show unexpected consequences of using state and custom methods in what should be a very simple program. Well, that and show that mathematical properties can be held if we code within the "framework". I didn't realize it until just now, but it fails miserably to highlight the common problem of unintended mutation as the result is arguably more correct in the OO version.
This is a problem lots of folks (including me) hit when working with mutable state so I'll try to find a better example to demonstrate it.
To answer your question about why I thought 16
was correct:
I was using equational reasoning to run the example in my head. In other words, if I were to replace each variable with its value (knowing numbers don't have breed/conjoin) I was interpreting it as:
var result = 4.conjoin(0).breed(2).conjoin(4.breed(2)).seagulls;
var result = 4.breed(2).conjoin(8).seagulls;
var result = 8.conjoin(8).seagulls;
var result = 16;
from mostly-adequate-guide.
Related Issues (20)
- Type safety or null safety
- help needed to run the exercises, ideally in VS Code HOT 3
- `id` function does not exist (Chapter 8)
- ch 04: 3rd exercise missing HOT 1
- Download files not accessible HOT 3
- Chapter 10, Part Laws. We don't need liftA2(concat). HOT 1
- Interesting
- Links broken on Gitbook.io
- Timeline for Part 3 (chapters 14+)
- Hey, great stuff :)
- Has this been ported over to other programming languages? HOT 2
- broken gitbook navigation HOT 2
- ch12: The result of `comp1` is not proper
- This code executes, where as per the book it should not execute - Ch08, Use Cases HOT 1
- Instructions for running Exercises are incorrect HOT 2
- Japanese Translation
- Chapter 3 why decrement hp is a pure function if it modifies input argument HOT 2
- Crashes Kobo HOT 1
- where is the chapter 14+
- In Chapter 07: Hindley-Milner and Me, why is it said that compose(head, map(f)) is faster than compose(f, head)?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mostly-adequate-guide.