Comments (10)
Thanks! Fixed by #74.
from fipp.
I think a patch would be welcome here, but not exactly sure what it should do. Fipp always "ednizes" data, but BigDecimal
notation (the trailing M
) is not valid edn as far as I know. Seems like inventing our own tagged-literal for it would be weird. I'm open to suggestions.
from fipp.
I thought that might have been it too, but I looked at the spec and it does mention, under floating point numbers:
In addition, a floating-point number may have the suffix M to indicate that exact precision is desired.
If I'm reading that right / what I'm asking for is desired behavior, I'd be happy to write a patch.
Would it be as simple as modifying ednize.clj (since cljs doesn't have a BigDecimal equivalent) and extending the IEdn
protocol for decimals?
from fipp.
but I looked at the spec
Ah, that's great then!
Would it be as simple as modifying ednize.clj (since cljs doesn't have a BigDecimal equivalent) and extending the IEdn protocol for decimals?
Yup, that should do it. PR welcome. Please update the tests as well. Thanks!
from fipp.
Great. After reading the code and sitting down to figure out an implementation, I think the way that I proposed above is actually the worst of three potential approaches.
Also as a side note, I really like the way you've designed this library! Very pleasant to read through. :)
The three approaches I'm considering are:
- The way that I was originally thinking — extend
IEdn
andIOverride
to apply tojava.math.BigDecimal
. The implementation of-edn
would have to return(symbol (str x "M"))
. Otherwisevisit
gets called with a string value, which gets surrounded with quotes. - Make a quick change to
visit-number
:(visit-number [this x] [:text #?(:clj (if (decimal? x) (str x "M") (str x)) :cljs (str x))])
- Modify
IVisitor
to- Add a
(visit-decimal [this x])
method toIVisitor
, - Update
visit*
to check for autils/decimal?
(which I'd implement likeutils/char?
) before checking fornumber?
(similar to how the ordering insidecond
is careful to check forrecord?
beforemap?
), - Add a
(visit-decimal [this x])
implementation toEdnPrinter
.
- Add a
(1) feels a bit awkward, (2) is nice and quick, but (3) feels more in line with how the only other Clojure-only EDN construct (char) is handled. But (2) is less intrusive.
What do you think?
from fipp.
(1) is definitely too ugly to be right :)
(2) seems promising. BigDecimal does satisfy Number, after all.
(3) would be perfectly reasonable, but all the other cases are disjoint types except for the overlap of strings/chars in ClojureScript.
I was about to reply "let's go with (2)", but then I decided to try this:
user=> (pr-str 5M)
"5M"
Boom!
Unless you can think of another number type that pr-str
will print in an unacceptable way, let's go with that :) Probably don't even need to bother with a test
from fipp.
Hahaha thank you, that's way better. I thought that I tried that, but I used pr
instead of pr-str
and ran the tests without even thinking about it. 🤦
from fipp.
Huh it breaks an assertion in fipp.edn-test/pprint-edn:
(testing "Not affected by *print-dup* binding"
(is (= (with-out-str (binding [*print-dup* true]
(pprint [(Integer. 1) (Long. 2)])))
(with-out-str (pprint [(Integer. 1) (Long. 2)]))
"[1 2]\n")))
; FAIL in (pprint-edn) (edn_test.cljc:179)
; Not affected by *print-dup* binding
; expected: (= (with-out-str (binding [*print-dup* true] (pprint [(Integer. 1) (Long. 2)]))) (with-out-str (pprint [(Integer. 1) (Long. 2)])) "[1 2]\n")
; actual: (not (= "[#=(java.lang.Integer. \"1\") 2]\n" "[1 2]\n" "[1 2]\n"))
For context:
(binding [*print-dup* true]
(let [x (Integer. 1)]
{:pr-str (pr-str x)
:str (str x)
:core.pprint (with-out-str (pprint x))}))
;=> {:pr-str "#=(java.lang.Integer. \"1\")",
; :str "1",
; :core.pprint "#=(java.lang.Integer. \"1\")\n"}
Back to (2)?
from fipp.
Some discussion of *print-dup*
and the related *print-readably*
can be found here: #56
For strings and characters, we call pr-str, but bind *print-readably*
to true
. Can we do the same for binding *print-dup*
to false
when stringifying numbers?
from fipp.
Great, and that covers us for arbitrary precision integers ("N" suffix) as well. PR incoming.
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
- 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
- How to specify fixed width floats in specific tagged-literals for output purposes HOT 2
- 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.