Closed cobalamin closed 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
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.
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 decoderscobalamin [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
Beautiful. Thanks!
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.