Elm Exactly
Results / elm-bytes /

BytesDecoderMutualRecursiveTest

elm-0.19.1 passelm-0.19.2 passelm-0.19.3 passlamdera-1.5.0 pass
A self-recursive top-level decoder that also delegates to a second top-level decoder decodes a small expression tree correctly.

`exprDecoder` refers to itself for both operands of `Add`, and to `atomDecoder`
for a literal. `atomDecoder` does not refer back, so despite the module name the
two decoders are not mutually recursive.

The encoding is `0 n` for `Lit n` and `1 left right` for `Add left right`. The
input `1 0 10 0 20` decodes to `Just (Add (Lit 10) (Lit 20))`.

Results

elm-0.19.1

passran in 77 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets.
decoded: Just (Add (Lit 10) (Lit 20))

elm-0.19.2

passran in 69 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets.
decoded: Just (Add (Lit 10) (Lit 20))

elm-0.19.3

passran in 55 ms
Output
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets.
decoded: Just (Add (Lit 10) (Lit 20))

lamdera-1.5.0

passran in 51 ms
Output
decoded: Just (Add (Lit 10) (Lit 20))

Discussions

Discuss this test·Search the discussions on GitHub

Discussions are on GitHub; one about this test has [t:elm-bytes/BytesDecoderMutualRecursiveTest] in its title.

Source

The -- CHECK: lines give the expected output.

module BytesDecoderMutualRecursiveTest exposing (main)
{-| A self-recursive top-level decoder that also delegates to a second top-level decoder decodes a small expression tree correctly.
`exprDecoder` refers to itself for both operands of `Add`, and to `atomDecoder`
for a literal. `atomDecoder` does not refer back, so despite the module name the
two decoders are not mutually recursive.
The encoding is `0 n` for `Lit n` and `1 left right` for `Add left right`. The
input `1 0 10 0 20` decodes to `Just (Add (Lit 10) (Lit 20))`.
-}
-- CHECK: decoded: Just (Add (Lit 10) (Lit 20))
import Bytes.Decode as D
import Bytes.Encode as E
import Html exposing (text)
type Expr
= Lit Int
| Add Expr Expr
exprDecoder : D.Decoder Expr
exprDecoder =
D.unsignedInt8
|> D.andThen
(\tag ->
case tag of
0 ->
atomDecoder
1 ->
D.map2 Add exprDecoder exprDecoder
_ ->
D.fail
)
atomDecoder : D.Decoder Expr
atomDecoder =
D.unsignedInt8
|> D.map Lit
main =
let
bytes =
E.encode
(E.sequence
[ E.unsignedInt8 1
, E.unsignedInt8 0
, E.unsignedInt8 10
, E.unsignedInt8 0
, E.unsignedInt8 20
]
)
result =
D.decode exprDecoder bytes
_ =
Debug.log "decoded" result
in
text "done"