Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dict2 is not tail recursive #36

Open
prasmussen opened this issue Aug 19, 2024 · 0 comments
Open

dict2 is not tail recursive #36

prasmussen opened this issue Aug 19, 2024 · 0 comments

Comments

@prasmussen
Copy link

I got the following error when decoding a big data structure:

RangeError: Maximum call stack size exceeded

I'm not sure if this repo is actively maintained so I won't bother creating a PR, but here is a tail recursive version of dict2 if anyone needs it:

dict2 : Decoder comparable -> Decoder v -> Decoder (Dict comparable v)
dict2 keyDecoder valueDecoder =
    Decode.keyValuePairs valueDecoder
        |> Decode.andThen (decodeDictFromTuples keyDecoder)


decodeDictFromTuples : Decoder comparable -> List ( String, v ) -> Decoder (Dict comparable v)
decodeDictFromTuples keyDecoder tuples =
    let
        helper : List ( String, v ) -> Dict comparable v -> Decoder (Dict comparable v)
        helper remainingTuples dictAcc =
            case remainingTuples of
                [] ->
                    Decode.succeed dictAcc

                ( strKey, value ) :: rest ->
                    case Decode.decodeString keyDecoder strKey of
                        Ok key ->
                            helper rest (Dict.insert key value dictAcc)

                        Err error ->
                            Decode.fail (Decode.errorToString error)
    in
    helper tuples Dict.empty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant