I have two functions: getUserFromDB :: Token -> Either NotFound User
and getNewUser :: Token -> Either NoMoreInfo User
The Token is a token from OAuth api response. getNewUser uses it to get more info from api and create new user in my DB. and getUserFromDB just gets a user from DB with the help of token info. The problem is: how do I combine these two functions into one like getUser :: Token -> Either OneOfTwoPossibleErrors User, which will first try to getUserFromDB feeding token to it and then, if it fails, try to getNewUser again feeding the same token to it? FP is very good at creating consecutive streams that process data, but how to create two or more parallel pipes that are tried in sequence until one of them returns good result, and return any result from the last one if none succeeds?
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
Thanks! Good to know. But what about more general way, for example if I had a list of functions to try in order, like [getUserFromDb, getNewUser, trySmthElse, ....]? And I don't want to map that list into a list of results because that would mean execution of all of them, and I don't need a new user if getUserFromDB had found the one I needed.
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
Isn't leftMap, as a function (not a method .leftMap) takes an Either as its first argument? And to put up a pipe of leftMaps with Eithers I would need to first get all the Eithers and that would mean running all the functions again, which I try to avoid.
There is a function until in PureScript that is the closest to what I'm looking for: until :: ( a -> Boolean ) -> m a -> m a, but m is Monad, and I don't quite get yet how I can put a list of functions in it so that they will all get the same input value and be run one by one and tested after each run. until then returns a m a that is also can just be a list of results, kind of like map would produce, but that list will end as soon as (a -> Boolean) evaluates to true. Then I could just last on that m a result and, according to last definition, -> maybe get a successful output from the first succeeded function in the list =)
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
I have two functions:
getUserFromDB :: Token -> Either NotFound User
and
getNewUser :: Token -> Either NoMoreInfo User
The Token is a token from OAuth api response.
getNewUser
uses it to get more info from api and create new user in my DB. andgetUserFromDB
just gets a user from DB with the help of token info. The problem is: how do I combine these two functions into one likegetUser :: Token -> Either OneOfTwoPossibleErrors User
, which will first try togetUserFromDB
feeding token to it and then, if it fails, try togetNewUser
again feeding the same token to it? FP is very good at creating consecutive streams that process data, but how to create two or more parallel pipes that are tried in sequence until one of them returns good result, and return any result from the last one if none succeeds?It depends on the library you are using for the Either. But maybe these docs from Folktale's Either can help: folktale.origamitower.com/docs/v2....
There's a
leftMap
method that will run when the either is a Left. So it would look likeThanks! Good to know. But what about more general way, for example if I had a list of functions to try in order, like
[getUserFromDb, getNewUser, trySmthElse, ....]
? And I don't want to map that list into a list of results because that would mean execution of all of them, and I don't need a new user ifgetUserFromDB
had found the one I needed.Maybe something like this?
Isn't
leftMap
, as a function (not a method.leftMap
) takes anEither
as its first argument? And to put up a pipe ofleftMap
s withEither
s I would need to first get all theEither
s and that would mean running all the functions again, which I try to avoid.There is a function
until
in PureScript that is the closest to what I'm looking for:until :: ( a -> Boolean ) -> m a -> m a
, butm
isMonad
, and I don't quite get yet how I can put a list of functions in it so that they will all get the same input value and be run one by one and tested after each run.until
then returns am a
that is also can just be a list of results, kind of likemap
would produce, but that list will end as soon as(a -> Boolean)
evaluates totrue
. Then I could justlast
on thatm a
result and, according tolast
definition, -> maybe get a successful output from the first succeeded function in the list =)I think I found a somewhat simple solution!
Ahh okay. I see what you are trying to do. Nice solution!