# How to mapProblems for elm/parser?

**URL:** <https://discourse.elm-lang.org/t/how-to-mapproblems-for-elm-parser/3505>\
**Category:** Learn\
**Created:** [April 18, 2019, 4:52pm UTC](https://discourse.elm-lang.org/t/how-to-mapproblems-for-elm-parser/3505 "2019-04-18T16:52:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![neurodynamic](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/neurodynamic/32/984_2.png) [@neurodynamic](https://discourse.elm-lang.org/u/neurodynamic)\
**Post date:** [April 18, 2019, 4:52pm UTC](https://discourse.elm-lang.org/t/how-to-mapproblems-for-elm-parser/3505/1 "2019-04-18T16:52:37Z")

</div>

Does anyone know a way to write a `mapProblems` function for [elm/parser](https://package.elm-lang.org/packages/elm/parser/latest) that works like `mapError` does for `Result`? The closest thing I can figure out (see below) seems complicated and probably expensive. My read after doing some digging is that the parser library doesn’t expose all the stuff necessary to write something more concise (it exposes the `Parser` type but not as `Parser(..)`, and I can’t just copy the whole Parser library into my app and edit it there because it has `Kernel` code in it). But maybe I’m wrong? Any ideas?

```auto
mapProblem : (problem1 -> problem2) -> problem1 -> Parser context problem1 data -> Parser context problem2 data
mapProblem problemFunc defaultProblem parser =
    withOffsetAndSource <|
        \( offset, source ) ->
            case parseStartingAt offset source parser of
                Ok okResult ->
                    succeed okResult

                Err deadEnds ->
                    deadEnds
                        |> List.head
                        |> Maybe.map .problem
                        |> Maybe.withDefault defaultProblem
                        |> problemFunc
                        |> problem

parseStartingAt : Int -> String -> Parser context problem resultType -> Result (List (DeadEnd context problem)) resultType
parseStartingAt offset source parser =
    source
        |> String.dropLeft offset
        |> run parser

withOffsetAndSource : (( Int, String ) -> Parser context problem data) -> Parser context problem data
withOffsetAndSource decoder =
    succeed Tuple.pair
        |= getOffset
        |= getSource
        |> andThen decoder

```

---

<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:** [April 28, 2019, 4:52pm UTC](https://discourse.elm-lang.org/t/how-to-mapproblems-for-elm-parser/3505/2 "2019-04-28T16:52:43Z")

</div>

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