map_throwing

Function map_throwing 

Source
pub fn map_throwing<A, B, E>(
    transform: impl Fn(A) -> Result<B, E> + Clone + 'static,
) -> impl Fn(Option<A>) -> Result<Option<B>, E>
Expand description

Free map on Option for throwing function composition. Equivalent to Swift’s map<A, B>(_ transform: @escaping (A) throws -> B) -> (A?) throws -> B?

§Examples

use overture_core::options::map_throwing;

let safe_divide = map_throwing(|x: i32| {
    if x == 0 { Err("Division by zero") } else { Ok(10 / x) }
});
assert_eq!(safe_divide(Some(2)), Ok(Some(5)));
assert_eq!(safe_divide(Some(0)), Err("Division by zero"));
assert_eq!(safe_divide(None), Ok(None));