Git Product home page Git Product logo

Comments (3)

DrBoolean avatar DrBoolean commented on July 21, 2024

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.

omasback avatar omasback commented on July 21, 2024

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.

DrBoolean avatar DrBoolean commented on July 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.