Applying a function that may fail to all values in a list

Posted by Egwor on Stack Overflow See other posts from Stack Overflow or by Egwor
Published on 2010-04-06T23:44:03Z Indexed on 2010/04/06 23:53 UTC
Read the original article Hit count: 166

Filed under:
|
|
|

I want to apply a function f to a list of values, however function f might randomly fail (it is in effect making a call out to a service in the cloud).

I thought I'd want to use something like map, but I want to apply the function to all elements in the list and afterwards, I want to know which ones failed and which were successful.

Currently I am wrapping the response objects of the function f with an error pair which I could then effectively unzip afterwards

i.e. something like

g : (a->b) -> a -> [ b, errorBoolean]

f : a-> b

and then to run the code ... map g (xs)

Is there a better way to do this? The other alternative approach was to iterate over the values in the array and then return a pair of arrays, one which listed the successful values and one which listed the failures. To me, this seems to be something that ought to be fairly common. Alternatively I could return some special value. What's the best practice in dealing with this??

© Stack Overflow or respective owner

Related posts about haskell

Related posts about beginner