elm-community / json-extra

Convenience functions for working with Json.
http://package.elm-lang.org/packages/elm-community/json-extra/latest
MIT License
37 stars 16 forks source link

Add `sequence` function #1

Closed cobalamin closed 7 years ago

cobalamin commented 7 years ago

See the docs for an explanation. It essentially turns a list of decoders into a decoder that returns a list, by zipping each decoder with one value in a JSON list.

lukewestby commented 7 years ago

If you need to be sure of the length of the list at compile time, say it's 3 like in this example, couldn't you use tuple3 instead and then only have to worry about the decoder at each position?

decoder : Decoder (List FloatOrInt)
decoder =
  Decode.tuple3 (\a b c -> [a, b, c]) intDecoder floatDecoder intDecoder
cobalamin commented 7 years ago

It's precisely not about compile time. The motivating problem someone had on Slack were:

I'll agree that my example code isn't ideal for demonstrating this, as it seems to do something that could be done hard-coded. I didn't think about that earlier and I felt like that was a minimal example to not get too long-winded with the docs, but it isn't great.

lukewestby commented 7 years ago

moving some discussion from slack over here for the sake of having it:

cobalamin [12:27 PM] [at]luke thanks for the feedback, I added a response :slightly_smiling_face:

luke [12:27 PM]
awesome! i totally get it now, i hadn’t considered the case of wanting to dynamically generate a sequence of decoders

cobalamin [12:27 PM]
yeah, it seems like that would be a better example. Would also be a lot of code though. Ideas?

luke [12:28 PM]
i think if the doc comment just mentions that idea of being able to generate your list of decoders dynamically we’ll be good to go

lukewestby commented 7 years ago

Beautiful. Thanks!