Git Product home page Git Product logo

Comments (2)

brandonbloom avatar brandonbloom commented on August 17, 2024

I think there are three separate issues wrapped up in here.

  1. control over floating point printing.
  2. extensible formatting of tagged-literals, similar to what is supported symbols.

.toFixed is converting the output element type to string too early, so the visitor pass detects it as a string

There are two ugly workarounds for your immediate needs:

  1. Do something like (symbol (.toFixed x 2)), abusing the fact that Clojure's symbol constructor follows the garbage-in, garbage-out philosophy.
  2. Monkey-patch the print-method for Float and Double:
 (defmethod print-method Float [o, ^Writer w]
  (cond
    (= Float/POSITIVE_INFINITY o) (.write w "##Inf")
    (= Float/NEGATIVE_INFINITY o) (.write w "##-Inf")
    (.isNaN ^Float o) (.write w "##NaN")
    :else (.write w (.toFixed o 2))))

is there a way to control formatting by passing through a fixed float

We delegate to Clojure here and - unfortunately - Clojure's print-method for numbers (modified above) does not provide a parameter for controlling this.

I'd be open to providing an option for this, but I'm hesitant because duplicating an underlying print method for a primitive decouples us from future changes. For instance, when #inf was added, Fipp got support for it for free by delegating to Clojure.

Is there a way to pass through formatting instructions during the EDN conversion prior to visiting each element

No. An intentional design feature of the EDN conversion is that it is formatting-agnostic. In theory, a custom type could be used to pass through formatting hints, but I haven't seen a good reason to do so yet.

Would it make sense to add another map key like :symbols, such as :tagged-literals that supported overriding the printer visitor for each tagged literal?

Maybe? I'd have to think about the implications of that. My gut reaction is to say no unless additional use cases are understood.

Should I look into writing my own EdnPrinter with this functionality?

That's the less hacky workaround: Simply copy/paste this part of the implementation and modify as necessary. It's small enough to be a low maintenance burden.

--

In summary, let me know if one of the workarounds works for you. If number formatting control is important, we can survey/lobby other printers and Clojure itself in case it makes sense to add a clojure.core/number-precision or similar.

from fipp.

dgtized avatar dgtized commented on August 17, 2024

Oh, the symbol approach is a really useful hack to be aware of for pass-through even if it's extra gross. I think that might be enough of a work-around for the cases I'm dealing with for now. Otherwise I'll take a look at my own EdnPrinter implementation with the :tagged-literals override. That seems like the correct spot to override output formatting, but I can understand if it's too niche a use case to update here.

Thanks for pointing out the print-method override approach, but I think I want scoped control over the precision of output and not a global change. It's a useful override if the floating point values are coordinates in a vector, but less useful if they represent something else. That said, I guess it's also possible to override the display of all floating point numbers using a custom EdnVisitor by overriding visit-number to check integer?, float? and format the output there.

Thanks for all the help, I'm trying to think how this could be better documented, but this discussion might be sufficient for now. I'll play around with the symbol and custom EdnVisitor with tagged-literal overrides as those seem to make the most sense and then see if anything to document jumps out.

from fipp.

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.