# How to make errors bubble up quickly?

**URL:** <https://discourse.elm-lang.org/t/how-to-make-errors-bubble-up-quickly/7758>\
**Category:** Learn\
**Created:** [September 14, 2021, 3:35pm UTC](https://discourse.elm-lang.org/t/how-to-make-errors-bubble-up-quickly/7758 "2021-09-14T15:35:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AvailableUsername](https://avatars.discourse-cdn.com/v4/letter/a/cc9497/32.png) [@AvailableUsername](https://discourse.elm-lang.org/u/AvailableUsername)\
**Post date:** [September 14, 2021, 3:35pm UTC](https://discourse.elm-lang.org/t/how-to-make-errors-bubble-up-quickly/7758/1 "2021-09-14T15:35:38Z")

</div>

Hello,

say I have a `List String` that I want to transform into a `List Int`.  
In an imperative language, I would use a for loop and at the first string that can’t be converted into an int, I would throw an exception.  
But how would I achieve something similar in Elm?

I think I’d like to write a function that has this signature:

```
mapOrFail : (a -> Maybe b) -> List a -> Maybe (List b)

```

If all the values in the first list map to `Just` variants, then all is good and a `Just (List b)` is returned.  
But at the first value that maps to a `Nothing`, no more computation would be done and I would get `Nothing` returned from the whole function.  
How would you achieve that? I guess I would have to use some kind of lazy functionality of the language?

---

<div class="post-metadata">

**Author:** ![sebbes](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/sebbes/32/3356_2.png) [@sebbes](https://discourse.elm-lang.org/u/sebbes)\
**Post date:** [September 14, 2021, 3:50pm UTC](https://discourse.elm-lang.org/t/how-to-make-errors-bubble-up-quickly/7758/2 "2021-09-14T15:50:39Z")

</div>

Elm has no lazyness. You can do what you want with recursion:

```elm
mapOrFail toMaybeB theAs =
    mapOrFailHelper toMaybeB [] theAs

mapOrFailHelper toMaybeB reversedBs theAs =
    case theAs of
        [] -> Just (List.reverse reversedBs)
        a::otherAs ->
            case toMaybeB a of
                   Just b -> mapOrFailHelper toMaybeB (b :: reversedBs) otherAs
                   Nothing -> Nothing

```

It is a bit entangled since you want stop the computation as soon as possible. Feel free to ask for clarification.

---

<div class="post-metadata">

**Author:** ![AvailableUsername](https://avatars.discourse-cdn.com/v4/letter/a/cc9497/32.png) [@AvailableUsername](https://discourse.elm-lang.org/u/AvailableUsername)\
**Post date:** [September 14, 2021, 4:24pm UTC](https://discourse.elm-lang.org/t/how-to-make-errors-bubble-up-quickly/7758/3 "2021-09-14T16:24:03Z")

</div>

Excellent, thank you!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [September 24, 2021, 4:24pm UTC](https://discourse.elm-lang.org/t/how-to-make-errors-bubble-up-quickly/7758/4 "2021-09-24T16:24:58Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
