Fork me on GitHub
#clojure-europe
<
2023-03-17
>
grav06:03:04

Go morgen 🙂

mdiin08:03:23

morgen

dharrigan08:03:07

Good Morning!

simongray08:03:05

good morning

otfrom08:03:03

What is the obvious thing I'm missing here?

(defn decimal-points [n points]
  (double (.setScale (java.math.BigDecimal. n) points java.math.RoundingMode/HALF_UP)))

(comment

  (with-precision 2 (double (/ 22 7)))
  3.142857142857143 ;; not what I want
  
  (decimal-points (double (/ 22 7)) 2)
  3.14 ;; what I want

  )

reefersleep11:03:16

In the examples I see on Clojuredocs, using BigDecimal gets you what you want… I think.

(with-precision 3 (/ 22M 7))
;;=> 3.14M

mccraigmccraig11:03:16

you can't necessarily get a precise decimal value out of a base-2 floating point (i.e. double ) value ... BigDecimal is the way to go

otfrom11:03:35

then what am I doing wrong here?

(with-precision 2 101323.2345M)
  101323.2345M

  (with-precision 2 (java.math.BigDecimal. 101323.2345))
  101323.234500000005937181413173675537109375M

  (decimal-points 101323.2345 2)
  101323.23

otfrom11:03:49

decimal-points is still the same as above

otfrom11:03:31

a fair number of changes between Java 8 and Java 17 in this class

mccraigmccraig11:03:48

dunno... this works on the java8 repl i currently have open:

(-> (java.math.BigDecimal. 1.234567) 
  (.round (java.math.MathContext. 3))))

👍 2
reefersleep13:03:38

@U0525KG62 it seems to me like what you’re doing wrong is expecting with-precision to work as intended 😅 I agree that it looks off!

👍 2
otfrom13:03:57

obviously I can do the interop, but it feels like there is something in clojure that I should be using instead

mccraigmccraig14:03:40

ohhhhh... i see - you have to do an arithmetic op with BigDecimals for with-precision to work:

;; no op - does nothing
(with-precision 1 3.123456789M) ;=> 3.123456789M

;; not a BigDecimal op - does nothing
(with-precision 2 (double (/ 22 7))) ;=> 3.142857142857143

;; does an op on BigDecimals - uses precision
(with-precision 1 (/ 1M 3)) ;=> 0.3M
(with-precision 1 (+ 3.123456789M 0)) ;=> 3M

;; sadly - does nothing
(with-precision 1 (+ 3.123456789M)) ;=> 3.123456789M

mccraigmccraig14:03:08

so really with-precisionis there to rescue you from an ArithmeticException caused by a non-terminating expansion during BigDecimal operations

think_beret 2
otfrom15:03:25

ok, at least I'm not doing anything stupid then (or copying something in core)

otfrom08:03:33

other than I probably want to use HALF_EVEN

thomas09:03:43

Morning, TGIF!!!!!

genRaiy09:03:50

good morning (from last night)

❤️ 6
genRaiy11:03:59

With a subject

❤️ 2
schmalz11:03:55

Morning all.

genRaiy12:03:00

What am I moaning about?

genRaiy12:03:56

I just got an email from Netflix telling me to not forget finishing to watch a show

genRaiy12:03:23

This sort of corporate nagging infuriates me

genRaiy12:03:11

I’m already annoyed when they ask me what I thought about something cos they have made us think that our thoughts should be given to them for nothing

genRaiy12:03:39

On some vague promises of improvements which have definitely not arrrived and I see no prospect of them arriving any time soon

genRaiy12:03:29

And … Yes, I deleted my Twitter account 😂

👏 2
simongray13:03:29

When I get sent shit like that, I go straight to email preferences and disable everything. If they don’t allow that, I send them a GDPR threat 😛

genRaiy13:03:28

I know what you mean and I could do all of that but I feel like that's a me solution to a them problem if you know what I mean

otfrom13:03:39

you know you can complain about this on Mastodon as well 😉

🐘 2
2
simongray14:03:41

Another option is piracy :man-shrugging:

otfrom14:03:23

it does feel like the right way to watch something like Our Flag Means Death

😁 2
simongray14:03:11

Make a piracy jar. Every time someone sends you a marketing email you didn't ask for, you put it in the piracy jar. Every time you need to torrent something without a guilty conscience, you remove an email from the piracy jar. Problem solved.

simongray14:03:44

Well not really... 😅

genRaiy16:03:04

Now we all have problems 🙂

genRaiy16:03:46

I feel like a daily 🧵 on my whinges over here might at least let some of the pressure out. And sorry @U0525KG62 I can't be 🍑 'd with Mastadon.

otfrom16:03:45

@U04V5V0V4 I like to think you are doing that to help support older Mastodons that are out there.

2
grav13:03:54

What's a sane way of testing that time has passed, without doing a Thread/sleep?

grav13:03:29

I'm aware that it's a silly test, but it's an existing, flaky test that I want to fix up.

simongray13:03:57

can’t you just use timestamps?

grav13:03:29

In this case the functionality that needs to be tested doesn't take any time as input.

grav13:03:32

The existing test just assumes that time has passed, which it sometimes has, sometimes hasn't. Optimally I'd be able to with-redef some kind of central time-taking thing. I could redefine java.time.Instant/now but that's pretty implementation-specific

javahippie13:03:36

You could use Clock for that. In the „prod part“ it just can run with the System time, while you can set it to fixed time for the test and manipulate it

grav13:03:17

Nice! Seems like exactly the thing I need 👍

javahippie13:03:25

You would then create Instant/now with the clock as a parameter. The downside is, that then you have to provide the clock from the outside, so you can switch it in the test

👍 2
Sam Ritchie23:03:57

That is a nice style imo but if you don’t want to thread it through, make the clock a dynamic binding and rebind it in your tests

👍 2
2
dominicm17:03:25

@mccraigmccraig I ❤️ helix. You’ll have fun 🙂

dominicm17:03:30

What’s taken you over to the hooked side?

mccraigmccraig17:03:41

i've been working on an app with local-first (SQLite/IndexedDB based) persistence... using hooks with promises for db queries seemed to make a lot more sense than awkward event flows copying data over to a re-frame app-db... and once you've drunk the hooks kool-aid then helix starts to look rather more elegant than reagent

👍 2
dominicm17:03:05

Still part of the yapster app? Or something internal or similar?

mccraigmccraig17:03:32

the local-first storage stuff was for a rewrite of the yapster app in RN ... right now i'm looking at helix for a mini-webapp to support users who have forgotten their yapster login details, and after the less than stellar reagent+hooks experience i thought i would take helix for a spin