I am trying to do the following:
processRights :: [Either a Int] -> Int
processRights xs = map (\Right x -> x, \Left x -> 0) xs
So, xs
is a [Either a Int]
, and I wish to produce a mapped list of the same length where for each int there is the same int, 0 otherwise.
How can I acomplish that?
You can use the either
, id
and const
functions:
processRights :: [Either a Int] -> [Int]
processRights = map $ either (const 0) id
either
runs the first function for any Left
, the second function for any Right
.
id
returns its argument.
const
ignores its second argument and returns its first argument, its intended use is that e.g. const 0
becomes a function that ignores its argument and just returns 0.