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

Improve decoder binding #47

Open
Profpatsch opened this issue Aug 2, 2021 · 2 comments
Open

Improve decoder binding #47

Profpatsch opened this issue Aug 2, 2021 · 2 comments

Comments

@Profpatsch
Copy link

Profpatsch commented Aug 2, 2021

I’m trying to write a decoder which parses the whole remaining columns with the same decoder.

Something like remaining :: Decode e s a -> Decode e s [a].
and then use it like: remaining utf8 :: Decode e ByteString [Text].

Now there is a function row :: Decode e s (Vector s) which does basically that, but the return type is fixed to s.
So I’d expect to be able to bind a monadic function.

And indeed, there is two different bind functions:

(>>==) :: Decode e s a -> (a -> DecodeValidation e b) -> Decode e s b

bindDecode :: Decode e s a -> (a -> Decode e s b) -> Decode e s b

which both seem close, but none of them works.

row >>== ??? does not work, because >>== wants an a -> DecodeValidation e b, not a utf8 :: Decode e s a. Unfortunately, the DecodeValidation version of utf8 is not exported.

row bindDecode (\bs -> ???) doesn’t work, because it requires a function ByteString -> Decode e s a which I don’t know how to construct.

I think for now I’ll just copy & paste the inner DecodeValidation part of utf8, but the binding story is really not great right now, it doesn’t compose with any of the primitives the Decode module exposes.

@Profpatsch
Copy link
Author

Profpatsch commented Aug 2, 2021

Alternatively, a way of converting a Decode e s a back to a s -> DecodeValidation e a would make these composable.

@Profpatsch
Copy link
Author

I figured out that row would always take all rows, not like I expected the rest of the rows from the current index.

This function does that:

rowFromCurrentIndex :: Dec.Decode e a (Vector a)
rowFromCurrentIndex =
  Dec.Decode . Compose . Dec.DecodeState . ReaderT $ \v ->
    state (\(Dec.Ind i) -> (pure (Vector.drop i v), Dec.Ind (Vector.length v)))

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