Comments (2)
I think there are three separate issues wrapped up in here.
- control over floating point printing.
- 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:
- Do something like
(symbol (.toFixed x 2))
, abusing the fact that Clojure's symbol constructor follows the garbage-in, garbage-out philosophy. - 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.
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)
- Optional println? HOT 1
- Make usage of commas optional HOT 3
- :pass rendered before indentation HOT 4
- clojurescript tests failing HOT 2
- consider automating tests on circleci HOT 3
- print-table of nested map HOT 1
- Use of undeclared Var clojure.core.rrb-vector.rrbt/Transient HOT 3
- java.sql.Timestamp not guaranteed to exist HOT 9
- BigDecimal Handling HOT 10
- Escaping of newlines in strings HOT 2
- Incompatible with Java 11 due to org.clojure/core.rrb-vector dependency HOT 1
- a strange case in generating regex with slashes, related to ClojureScript HOT 5
- print unreadable symbol utilizing #=(symbol ...) for *read-eval* HOT 1
- Babashka compatibility HOT 19
- ClojureScript: fipp interacts badly with console.log as default print strategy HOT 6
- UnsupportedOperationException when printing java.sql.Date HOT 2
- Optionally print without trailing comma HOT 1
- `fipp.ednize/override?` uses `satisfies?` HOT 5
- Printing a Var macro ad verbum may render an unreadable EDN file HOT 2
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 fipp.