I’m getting an inexplicable error in the clojurewerkz/elephant library and would appreciate help
Looking at the library code,
(defn ^IPersistentMap create
[^IPersistentMap m]
(cnv/customer->map (Customer/create m)))
This is calling com.stripe.model.Customer, passing in the map. The codepath seems unrelated to the clj library. Have you tried inspecting the map and checking it is correct, and comparing it to the Stripe docs?Also the stacktrace seems to indicate the erroris when doing subscription creation. Can you give more context for: vendo.routes.home$create_subscription.invokeStatic(home.clj:62)
What’s the most efficient way to convert a string to a number without specifying the type of the number (Long, BigInt).
I was thinking of clojure.edn/read-string
but it will successfully reads strings that are not numbers.
Also, I am not sure that it’s the most efficient option.
you could look at how the reader does it. from memory a bit ago, it constructs a bigint from it and then if its sufficiently small turns that into a long
BigInteger bn = new BigInteger(n, radix);
...
return bn.bitLength() < 64 ?
Numbers.num(bn.longValue())
: BigInt.fromBigInteger(bn);
I found the code on Github https://github.com/clojure/clojure/blob/cbb3fdf787a00d3c1443794b97ed7fe4bef8e888/src/jvm/clojure/lang/EdnReader.java#L342-L344
I would actually like to have a core function for this and may even have created a CLJ ticket for it already
you can vote here: https://ask.clojure.org/index.php/4275/add-convenience-parse-int-parse-float-parse-short-functions
Seems like Jira exporter does some things wrong so that the resulting http://ask.clojure.org post looks off:
- Some code is enclosed in <code>
, as it should be; but some is just wrapped in {{...}}
and is left as a regular text
- Somehow parse-*
has become *} with a bunch of spaces
fixed
@dpsutton what is this Numbers.num
?
static public Number num(Object x){
return (Number) x;
}
static public Number num(float x){
return Float.valueOf(x);
}
static public Number num(double x){
return Double.valueOf(x);
}
from /clojure/src/jvm/clojure/lang/Numbers.java
in this case as a long its not doing any work i think
static public Number num(long x){
return Long.valueOf(x);
}
is what its doing in this case. its boxing it i think?Voted like a responsible Clojure citizen
Why boxing is required here?
thx, this really does help raise visibility - we are looking at votes for prioritization
this is reader code and the reader doesn't return primitives is my guess. but i'm very handwavy on boxing vs primitive. my work in Clojure normally doesn't depend on this stuff so i'm just surface level on that stuff
I am implementing linting for the core.match macro. I'm running through its unit tests to see what clj-kondo can't yet understand. Now I've arrived at the :when
syntax:
(match [y]
[([_ (a :when even?) _ _] :seq)] :a0
[([_ (b :when [odd? div3?]) _ _] :seq)] :a1
:else [])
but I can't find docs on this, neither in the Basic usage nor in the Advanced usage wiki page. Is this undocumented behavior?i see it in the changelog but basically nowhere else.
but that multimethod allows for dynamic additions. I've got one like
(defmethod match/emit-pattern-for-syntax [:isa? :default]
[[_ parent]] {::match/tag ::isa? :parent parent})
so there's no way to encompass all statically i thinkoh so :when
is defined using a multimethod and is something custom?
its custom but defined in the source just like :seq
:or
:guard
:<<
etc. it uses a mechanism that is exposed for others to add syntax to match
I just added support for :<<
. I'll take a look
"Handles patterns wrapped in the special list syntax. Dispatches
on the first or second keyword in the list. For example, the pattern
`(:or 1 ...) is dispatches as :or, and `(1 :as a)` is dispatched by :as."
docstring for emit-pattern-for-syntax
Haha, the advanced usage docs: > You should read Understanding the algorithm before reading the rest of this section. And then there is hardly any rest of this section ;).
@alexmiller Is github login the only form of authentication or am I missing something? I'd prefer to register separately but it's fine if not possible 🙂
it is the only form of authentication
the github auth does not grant access to anything except public info
hey people… I’ve been experimenting with finalize
on reified objects and I’ve noticed that finalize method is called twice for each object, which seems odd, since Java is supposed to call each finalize once.
(defn test1 []
(reify Object
(finalize [this]
(println "collected"))))
and this produces:
(do (test1) (System/gc))
collected
collected
=> nil
Seems oddadded this bit to the cljdocs examples
user=>
(defn test1 []
(reify Object
(finalize [this]
(println "collected" this))))
#'user/test1
user=> (do (test1) (System/gc))
nil
collected user=> #object[user$test1$reify__179 0x5802c0da user$test1$reify__179@5802c0da]
collected #object[user$test1$reify__179 0x202cf2e7 user$test1$reify__179@202cf2e7]
logging this
shows there are different things involvedseems to imply reify call creates two objects?
yes further testing proves that’s the case:
(let [t (test1)] (System/gc) t)
collected
i browsed the code a little bit. I see that Object is cons onto the interfaces without checking if its already there. i wonder if its duplicating this stuff accidentally. Note this is cursory glance and I could easily be wrong
I kinda feel like I only half understand the finer distinctions here, but in Clojure, is it correct to say that sequences are not collections? You can create sequences from collections. You can create collections from sequences. But sequences themselves are not collections? As far as official docs go, this page seems to imply that sequences are separate things from collections: https://clojure.org/reference/sequences e.g. these sentences on that page imply they are separate kinds of things: "Many of the functions in the seq library take one or more collections, call https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/seq on them, and then operate on the resulting seq. In other words, many of these functions take collections but operate on their seqs."
I've written a couple longer form things that might help: https://insideclojure.org/2015/01/02/sequences/ https://insideclojure.org/2016/03/16/collections/
Thanks for the links. If anyone has written more deeply on this topic, I figured it would be you 🙂
from a Java class pov, sequences ARE collections
ISeq extends IPersistentCollection
coll? returns true for seqs, etc
My question arises from wondering what an "authoritative" answer would be for explaining to someone why peek
throws an exception for at least some kinds of sequence inputs. peek
mentions a few collection types like vectors, lists, and queues explicitly, but then also says what it does for "collections" in general. If sequences are collections, then in some minds it might raise the question of whether sequences are collections, or not.
That might be just one of those corners of implementation or docs that is kinda near the hairy edge of these questions.
as usual, I think you should read absence of information as significance of undefined behavior
Ah, so perhaps the word "collection" in peek's doc string should be interpreted as "one of the aforementioned kinds of collection early in the doc string", not "any arbitrary kind of Clojure collection". That would make sense.
peek / pop really only make sense as a concrete sequential data structure operation, not as seq ops
this is an artifact of compilation
no its not
or at least that is not a good description
I am pretty sure the two instances are a result of the code the compiler emits to add any metadata attached to the reify form to the instance
and reify supports metadata as a value, so adding metadata returns a new instance
In order to write linting for :when
in core.match, I'm trying to understand it.
@dpsutton already pointed me to the docs of the defmulti. it dispatches on the first or second keyword in the pattern.
But I'm also seeing expressions like:
(match [y]
[([_ _ :when even? _ _] :seq)] :a0
[([_ _ :when [odd? div3?] _ _] :seq)] :a1
:else [])
How do these get handled? The keyword :when
occurs in seemingly random place in the pattern?where's that example from?
its possible that that is matching on the literal :when
?
Thank you for your help!
this example is from the tests
it seems it supports a "flattened syntax"? hmm, this complicates things
i've seen the group by stuff but never understood what it does
and i don't like it ha
is the word "class" a keyword in clojure?
having some inherent meaning when used as first element of a form?
It's not a keyword, it's a regular function in clojure.core
.
(map (juxt identity special-symbol?) '[if class deftype let* fn])
(let [class inc] (class 3))
yields 4 for me
Because it's a regular function. :)
user=> (source class)
(defn class
"Returns the Class of x"
{:added "1.0"
:static true}
^Class [^Object x] (if (nil? x) x (. x (getClass))))
nil
yeah i know. was showing how i knew 🙂
thank you, sorry for the stupid question, I am not a regular clojure user, was trying to understand some clojurescript, should have got a repl going
not stupid at all
anyway, the full syntax should now be supported on master
but repl-able, thanks a lot for your help
can you describe the weird behavior you are seeing?
oh i never mean "you can answer this question yourself so don't ask it". i always mean great question and here's an authoritative answer
Indeed not stupid. But in case you have a lot of entry-level questions, you'll find #beginners to be a great place.
it's understandable now I know it's a clojure core function, someone in this library: https://github.com/groundedSAGE/konserve has some cljc files whic haven't been used under cljs for a long time so don't compile, because they include that function without a guard for :clj
I was slightly ambitiously trying to get them to compile/run under node in order to get a version of datahike working under node for a project I'm mostly working in typescript but want a node logic db as backend
I believe they call this yak shaving
Yeah, class
is not defined in CLJS at all - because the platform has no proper classes, I guess.
one could imagine there being some special magic for cljc files under a cljs/clj compiler which detects the more obvious compatibility errors, but then I imagine they're obvious for more experienced devs
there's not a ton of magic to the cljc stuff.