DecodeAndThenTest
`Bytes.Decode.andThen` lets a value already decoded choose the next decoder: a length byte followed by that many bytes of string decodes to `"Hi"`.
Results
elm-0.19.1
passran in 54 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. DecodeAndThenTest: "Hi"
elm-0.19.2
passran in 60 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. DecodeAndThenTest: "Hi"
elm-0.19.3
passran in 68 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. DecodeAndThenTest: "Hi"
lamdera-1.5.0
passran in 53 msOutput
DecodeAndThenTest: "Hi"
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm-bytes/DecodeAndThenTest] in its title.
Source
The -- CHECK: lines give the expected output.
module DecodeAndThenTest exposing (main)
{-| `Bytes.Decode.andThen` lets a value already decoded choose the next decoder: a length byte followed by that many bytes of string decodes to `"Hi"`.
-}
-- CHECK: DecodeAndThenTest: "Hi"
import Bytes exposing (Bytes)
import Bytes.Decode as D
import Bytes.Encode as E
import Html exposing (text)
main =
let
-- Encode: length (1 byte) followed by string
bytes =
E.encode
(E.sequence
[ E.unsignedInt8 2
, E.string "Hi"
]
)
-- Decode length, then decode that many chars
decoder =
D.unsignedInt8
|> D.andThen (\len -> D.string len)
result =
D.decode decoder bytes
|> Maybe.withDefault "FAIL"
_ =
Debug.log "DecodeAndThenTest" result
in
text result