Fork me on GitHub
#clojurescript
<
2022-04-07
>
tianshu01:04:50

Is there an elegant way to handle big integers in ClojureScript? Some libraries or tricks?

pithyless05:04:15

Usually just include big.js, but this wiki describes some of the differences: https://github.com/MikeMcl/big.js/wiki If you're looking for a CLJS example, Fulcro RAD has a namespace worth checking out: https://github.com/fulcrologic/fulcro-rad/blob/develop/src/main/com/fulcrologic/rad/type_support/decimal.cljc

p-himik06:04:14

There's js/BigInt that exists in browsers and on Node. And it supports arithmetic just fine, so you don't have to treat it in a special way at least there. But (number? ...) returns false, so it might not work in some cases, and some libraries might not know about it.

=> (def a (js/BigInt "900719925474099100"))
#'cljs.user/a
=> (def b (js/BigInt "900719925474099101"))
#'cljs.user/b
=> (+ a b)
#object[BigInt 1801439850948198201]
=> (number? a)
false

tianshu08:04:13

I saw there's a library called bn.js. Is it necessary to be used in ClojureScript? Or js/BigInt is exact what I want?

tianshu08:04:06

There are so many libraries in js world

p-himik08:04:21

js/BigInt by itself does not need any libraries, it's built-in. But hard to say whether you need something in addition to that because we don't know your exact requirements.

tianshu09:04:04

Okay, thank you!

rolt10:04:13

Hello, I'm getting a warning when setting the meta of a record, yet it works:

cljs.user> (defrecord A [])
cljs.user/A
cljs.user> (meta (A. {:tag 1}))
WARNING: Wrong number of args (1) passed to A at line 1 <cljs repl>
{:tag 1}
Any idea how to resolve this ?

p-himik10:04:02

The warning is not from meta but from (A. ...). The text of the warning is rather clear IMO - you have defined a record that expects no arguments (`[]`), and yet you pass one argument when creating it. If you want {:tag 1} to be the metadata of a newly constructed record, then you have to extract that map from under (A. ...) and replace meta with with-meta.

rolt11:04:15

the thing is that when defining a record, the constructor can take 3 additional arguments on top on the positional fields: __meta, __extmap and __hash. Setting the __meta does seem to work despite the warning, I'm properly getting it back when calling meta on the result. If that's discouraged I would except a warning about that instead of the arity.

p-himik11:04:22

Ah, I see what you mean. The docstring says that there are two additional arguments though, not 3:

Two constructors will be defined, one taking the designated fields
  followed by a metadata map (nil for none) and an extension field
  map (nil for none), and one taking only the fields (using nil for
  meta and extension fields).
So in your case, it should really be (A. {:tag 1} nil). Even though when transpiled, the constructor does become a function with 3 arguments. But regardless of how many arguments you provide, there's still that warning, so seems like there's an issue in parsing the new statement.

p-himik12:04:52

Yeah, I think so.

hanDerPeder10:04:44

when I require an npm dependency for the first time (like (require '[moment])) the Var is nil. After manually reloading the browser the Var is set correctly. Is this expected? Using figwheel-main.

p-himik10:04:43

Are you using (require ...) from a REPL?

hanDerPeder11:04:08

no, in an ns macro

hanDerPeder11:04:21

does it matter?

p-himik11:04:53

There's a difference between require and :require. The ns form should use only the latter, and its inner vector should not be quoted.

p-himik11:04:08

So in the end, should be like

(ns my.ns
  (:require [moment]))

hanDerPeder11:04:33

yes, that’s what I’m using. Just for context, I’ve used clojure for several years, but new to clojurescript.

hanDerPeder11:04:48

expanding on your snippet

(ns my.ns
  (:require [moment]))

(println moment)
This prints nil. After a reload of the browser it prints the object as expected.

hanDerPeder11:04:14

I see in the output folder that the js file is generated with a line that says require("moment") and the bundler webpack is re-run. but still nothing until I refresh the browser

p-himik11:04:22

Oh, so you're using some non-trivial setup then, given that webpack is involved. Sorry, no clue then. I use only shadow-cljs and have never had such issues.

👍 1
zimablue11:04:29

custom reader conditionals eg. (node vs browser): did this ever go anywhere? https://clojure.atlassian.net/browse/CLJS-2396 according to shadow-cljs docs not https://shadow-cljs.github.io/docs/UsersGuide.html#_conditional_reading

1
pinkfrog12:04:35

I installed react-native-localize and performed a (js/require "react-native-localize") . However, the repl prompts the package not found.

zimablue12:04:45

I probably can't help, but the next question someone will probably ask you is how you're compiling/building your clojurescript (lein, boot, shadow-cljs, other) and where you're running it (in a browser, in nodejs, other)

zimablue12:04:08

and also how you installed it - npm install? yarn add? local? global?

pinkfrog12:04:42

I installed with npm. Compiled with shadow-cljs. It is a react-native project.

zimablue12:04:21

I hate to do it as the guy is a saint but you'll probably get a faster and more specific answer in the shadow-cljs slack, but also the best information you'll find without asking someone will be in the shadow-cljs user manual

zimablue12:04:54

it sounds like what you've done should work, shadow-cljs respects/integrates NPM and normally finds npm modules, have you killed your cache (in .shadow-cljs) and done a full rebuild?

zimablue12:04:10

also can you see the package in your node_modules subdirectory, with a reasonable package.json?

zimablue12:04:04

if you look into your .shadow-cljs/builds/<your-build-name> directory, you can see where shadow-cljs is physically moving your dependant packages

zimablue12:04:07

that might give you a clue

p-himik13:04:26

Why js/require though instead of require?

zimablue13:04:35

@U2FRKM4TW that's a better question thanks, js/require works right but if shadow-cljs hasn't bundled the dependency then it won't, and is it the (ns (:require... part that actually prompts shadow-cljs to bundle in the npm package?

p-himik13:04:23

I think so - I'm not an expert here. And maybe it's common to use js/require in React Native, which I myself don't use, but you definitely don't do that if you target a browser.

thheller17:04:01

did you reload the app after adding the package? in react-native metro is responsible for loading packages and as such is only available if metro has seen it

thheller17:04:19

if you load the namespace with the require via the REPL first then metro won't see it

thheller17:04:24

and as such the package won't be avialable

pinkfrog11:04:21

Hi @U63D7UXJB when is the files in .shadow-cljs/builds/<your-build-name> directory, generated, and how are the moved? I see they are all named in the format of .cljs.cache.transit.json