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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…