elm-community / elm-community/json-extra
dict2 is not tail recursive
- Dominant language
- Elm
- Stars
- 40
- Forks
- 16
- PR merge metrics
- No merged PRs in 30d
Description
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:
```elm
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
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by locating the existing `dict2` implementation and compare it with the tail-recursive version in this issue. Exercise it with a large JSON data structure to reproduce the stack overflow, then confirm decoding completes without the RangeError and existing behavior remains intact.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- elm
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100