Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

typescript - 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?

question from:https://stackoverflow.com/questions/65602929/correctly-join-monads

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...