Giter VIP home page Giter VIP logo

Comments (4)

giacomocavalieri avatar giacomocavalieri commented on August 11, 2024 1

I'm probably missing something, but I bumped into (what I perceive to be) a hard limit of 9 fields using dynamic.decodeN with gleam/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 a Result, so you can combine them with the result module's functions:
    pub 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(...)
    }
    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 since result.try is short circuiting
  • You could have a look at how decodeN functions are implemented and copy that for any N, 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.

fwgreen avatar fwgreen commented on August 11, 2024 1

Thank you all for taking the time help.

from stdlib.

vxern avatar vxern commented on August 11, 2024

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.

lpil avatar lpil commented on August 11, 2024

Or you could call one of the decode functions multiple times!

Thanks all

from stdlib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.