Comments (4)
I'm probably missing something, but I bumped into (what I perceive to be) a hard limit of 9 fields using
dynamic.decodeN
withgleam/pgo
.
Hello! If you need to decode more than 9 things at a time you have a couple options:
- You can use a combination of decoders and
result.try
: notice how a decoder is nothing more than a function returning aResult
, so you can combine them with theresult
module's functions:This is probably going to be the most concise way to do it; the trade-off is that you're going to only get the first decoding error you meet sincepub fn decode_n(dynamic, decoder_1, ..., decoder_n) { use field_1 <- result.try(decoder_1(dynamic)) ... use field_n <- result.try(decoder_n(dynamic)) Ok(...) }
result.try
is short circuiting - You could have a look at how
decodeN
functions are implemented and copy that for anyN
, the code is probably not going to look great but with this approach you can accumulate all the decoding errors you run into. If that's not a dealbreaker for you I'd go for the first approach// It looks roughly something like this... pub fn decode_n(dynamic, decoder_1, ..., decoder_n) { case decoder_1(dynamic), ..., decoder_n(dynamic) { Ok(res_1), ..., Ok(res_n) -> Ok(...) res_1, ..., res_n -> [all_errors(res_1), ..., all_errors(res_n)] |> list.concat |> Error } }
from stdlib.
Thank you all for taking the time help.
from stdlib.
Why is this necessary over defining your own decoder as a function which would allow you to support an arbitrary number of fields?
from stdlib.
Or you could call one of the decode functions multiple times!
Thanks all
from stdlib.
Related Issues (20)
- On JavaScript, string.replace \r splits \r\n graphemes HOT 7
- string.inspect("\f") gives different results on Erlang vs JS
- More string trimming capabilities HOT 3
- More functions for working with `Set`s HOT 3
- `list.count` and `iterator.count` HOT 12
- Add `set.symmetric_difference` (and not `set.is_superset`) HOT 1
- Bit array slices of slices incorrect on JavaScript
- Add `dict.change`? HOT 5
- Escape backspace etc graphemes in string.inspect HOT 15
- Failing test when negating zero HOT 5
- `JSON.stringify` produces invalid Gleam escape sequences and should be replaced HOT 1
- `list.window` going into infinite recursion when `n` is 0 HOT 1
- Add primitives to set fields on `Uri` objects. HOT 4
- Add a `new()` primitive to the `uri` package. HOT 5
- Consider adding a replace function to regex HOT 2
- uri.origin should not have any path HOT 2
- Have iterator.yield not wait for the next to be available before yielding the previous HOT 3
- Question: round and truncate return Int instead Float HOT 1
- Return index of undecodable element in dynamic.list DecodeError path HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stdlib.