I have a function which returns an instance of Either where the Left side represent the exception / error, while the second side stores the return value.
If the Either instance has been Left instantiated to the Error branch I want to return immediately. If the instance has been Right instantiated I want to wrap that in a Maybe and continue on (as it comes into the function as a Maybe, and only gets looked up if it is Nothing).
This is working per my test cases:
- isNothing being passed in :: lookup is in error
- isNothing being passed in :: lookup is successful
- isJust(22) being passed in (lookup doesn't execute)
The code feels OK, but I don't supect I may be missing sme of the finer points of the Folktale data.either library.
// from data.monad
const someValue = Maybe.Nothing()
// ...snip...
if (someValue.isNothing) {
// from data.either :: Either.Left | Either.Right
const possiblySomeValue = yield lookupSomeValue()
if(possiblySomeValue.isLeft) {
return possiblySomeValue
} else {
someValue = Maybe.Just(possiblySomeValue.get())
}
}
I am combining ES6 (Node 4.1) with Folktale: data.either and data.maybe. My goal is really elevating my understanding in how to write properly in this style
update the problem is a little more complex I ahve back to back independent lookups, which I feel could be chained together:
// from data.monad
const someValue = Maybe.Nothing()
// ...snip...
if (someValue.isNothing) {
// from data.either :: Either.Left | Either.Right
const possiblySomeValue = yield lookupSomeValue()
if(possiblySomeValue.isLeft) {
return possiblySomeValue
} else {
someValue = Maybe.Just(possiblySomeValue.get())
}
}
// from data.monad
const someValue2 = Maybe.Nothing()
// ...snip...
if (someValue2.isNothing) {
// from data.either :: Either.Left | Either.Right
const possiblySomeValue2 = yield lookupSomeValue2()
if(possiblySomeValue2.isLeft) {
return possiblySomeValue2
} else {
someValue2 = Maybe.Just(possiblySomeValue2.get())
}
}
Its the back to back occurances whcih make the code super ugly...
This is the current state of my code which I think is better. First be able to convert the Maybe to an either, to allow me to chain / orElse with transformations (Maybe.orElse does not allow take a function, whereas the Either.orElse does take a function for the transformation)
then, since I ma unpackaging the Maybe.Just into the Either.Right as part of the conversion, I simply need to provide the orElse transformation.
Blending into my actual problem, with generators lead to a slightly ugglier solution. lookupSomeValue is a generator function, so we need to yield. Also since the value is used in multiple places we want to force this into a value with get.
so when repeated the code isn't nearly as bad as my original solution -
I am still looking for a more concise grammar. Will update with another solution if I find one.