Underling thought of Either is so simple. If some value is fit for the condition, then it will be wrapped with ‘Right’. Otherwise ‘Left’ will wrap its value. I also agree with the claim that it may enough in most cases. But I needed more on it.
As a functional programming learner, I’ve been studying the way of FP by writing actual implementation like RamdaJS1https://ramdajs.com/, FxJS22https://github.com/marpple/FxJS. In those days I found rejoice of programming because of FP. So, why not make enhanced version of Either if there is no suitable implementation for me.
The reason I made throwable/catchable Either implementation is the following requirements while making Blockchain Contract Manipulating webpages which is identical with myEtherWallet’s.3https://vintage.myetherwallet.com/#contracts(But for Klaytn for this time. Klaytn4https://www.klaytn.com/ is private Blockchain service that will be launch several days later by Ground X corp. in South Korea.5https://groundx.xyz/)
A variable that will be given contains string value and it will be one of these ‘string’, ‘address’, ‘bool’, and ‘uintNNN’. ‘NNN’ part could be ‘8’, ‘16’, ‘32’, ‘128’ so on. I decided to categorize by ‘string’, ‘address’, ‘bool’, ‘number’ and ‘unsupported’.
Of course the requirement is so simple, anybody can code for it. But like I said, why not to use Either for more understanding of FP. At first, I’ve coded like following.
yeah,, not bad. but also not enough for me. looks ugly somewhat.
And later, I realized that the depth of handling code have to be nested due to the reason that traditional Either does not have ‘GOTO’ feature. So I imaged.
“What if I can throw the ‘Right’ to some point and only catch its value whiling ignoring other methods.” , “And .. It must be cool if I throw the ‘Right’ to the very end of chain. Then I can manipulate the order of control flow”
Throw and Done class is for the purpose. Its implement is here.
One thing that you may think is that “Why Try.. Either in Either?”, “Do we really need it?” Of course, No we don’t need. My Throw and Done class is not for the Exception handling. It is carrier of the ‘Right’.
If you throw ‘Right’, new instance of Throw will wrap it and go on the chain. Only
.catch() method can handle the Throw instance.
If you Done the ‘Right’, new instance of Done will wrap it and go on the chain.
.take() handle the Done instance.
By this simple mechanism, I could change the previous code like the following:
Yeah~ much simpler than the first one, isn’t it? 😉
You can use
Either.try() to handle some case that exception could be occur. Simply this method returns ‘Right’ if no exception. Otherwise, if exception occur, it returns Left. So you can continually chain on it.
Combination of its usage is here, in this React component. 6https://github.com/andrwj/myKlaytnContract/blob/andrwj/develop/src/Routes/Contract/RunCommand.js
And one more thing, though.
I combined Either into Promise. And named it Fromise. 7https://github.com/andrwj/FPJS/blob/master/fromise.js
(Not to be serious, FP is my pleasure currently. So I made for my own tries)
FP gives me joy of programming these days. I hope that I could get understand more about the way of FP.
References [ + ]