philipp
philipp

Reputation: 16515

correctly join monads

To shorten the time of the covid-19 lockdown i decided to jump into functional programming, since I just stumbled over this brilliant read: »Mostly adequate guide to FP« by @DrBoolean. I am unsure if I have understood the concept right, or if the code example is overly simplified, so hope to find some clue.

A little down on this paragraph it is said:

If we have two layers of the same type, we can smash them together with join

In the code sample the join() method of a Maybe is outlined like so:

Maybe.prototype.join = function join() {
  return this.isNothing() ? Maybe.of(null) : this.$value;
};

The one of Identity like so:

Identity.prototype.join = function () {
  return this.$value;
}

But none of the methods does any check, if there are indeed »two layers of the same type«. I feel that this should be done, probably in more production ready code? Or can such a check be omitted because of the implications of the underpinning structure?

If the there is just one layer, join() blindly returns the value it contains, but as stated here:

Once data goes into the Container it stays there. We could get it out by using .$value, but that would defeat the purpose.

Does the underlying data structure and its correct usage prevent the value to »flop« out, or, to accidentally »smash two layers of different types«, or is that something that has to be enforced by code?

Upvotes: 0

Views: 116

Answers (1)

SuperJumbo
SuperJumbo

Reputation: 541

Perhaps the functions presented in the "mostly adequate" docs are tailored more toward educational use and thus eschew formal correctness in favor of conveying a certain concept – not sure, just a guess (I have read them too but couldn't say definitively).

In the case of the functions for the "mostly adequate" guide, it always seemed to me that the onus was on the user to put them together correctly.

(From a quick look at Ramda.js the only join function I can see is to do with string concatenation)

Upvotes: 1

Related Questions