If I have a java class which has 2 methods get
, one which returns a String, one which returns a byte array. How do I inform Clojure I'd like the byte array form?
What are the signatures of the two get
methods besides the return values? That is, their number and types of parameters?
@andy.fingerhut Are differing args a prerequisite for this in java?
In this case, it is String vs byte array arguments too. I am curious to know if it was just get() if java return typing works
Overloaded methods in Java require having different arguments
Otherwise compiler can't guess what was meant to be called
In cases where the return is ignored, for example, or assigned to Object variable
Okay, cool. So I'll never run into that situation then! Great.
I have a ns in which I am declaring a bunch of vars (def a 1) (def b 2)
is there a way I can get a list of all vars defined in a ns ?
@murtaza52 ns-publics or ns-interns, depends what you're after
There's a few other ns- functions too
@dominicm thanks, both of them are giving me the intended result, how do they differ ?
@murtaza52 ns-interns
gives you all mappings, including private functions
thanks @schmee also is there a way to specify the current ns, trying to see if I can escape hardcoding the ns name when calling (ns-publics 'ns-name)
you can use *ns*
as a shorthand for the current namespace
Is there a specific function I can use to map on a map and change its values?
no, you’ll have to use something like reduce-kv
or Specter
I see almost everybody suggesting to write your own function such as
yeah, if you don’t want to use a lib for it then that is the way
All right, thanks!
@schmee thanks
is there a way to have private (def- )
just like private (defn- )
yes: (def ^{:private true} your-var)
, no shorthand for that one
Since there’s only one element in the meta, there’s the tiny abbreviation of saying: (def ^:private your-var)
But it’s not much
great thanks
so if I have *ns*
in a fn, then it refers to the ns from which the fn is called.
One way to get the ns where the function is defined is to have something like (def my-ns *ns*)
in the namespace where a function is defined, and use my-ns
in the definition of that function.
@andy.fingerhut cool thanks for the tip
@murtaza52 There was a similar question about this on SO recently: https://stackoverflow.com/questions/64077153/ns-unexpectedly-evaluate-to-user-namespace-using-lein-run-but-not-when-us/64077477#64077477
@borkdude thanks will check it out
Is there a library that lets me run SQL over a vector of maps?
For
Hey guys, I'm using the java-time api. If I do something like (def now (local-date)) (println now) it prints #object[java.time.LocalDate 0x153739a 2020-10-03] how could I make it so it only prints the date? I've tried many things along the lines of (println (. java.time.LocalDate now)) with no success
(str (java.time.LocalDate/now))
"2020-10-03"
Damn, that easy. Thank you so much. Was stuck for way longer than Id like to admit.
Out of interest is there a reason to use java time direct over and above clojure.java-time?
I’d flip it around: is there a reason to use clojure.java-time over java.time? 🙂
Touche!
java.time is actually a legit good API, and it’s also compatible with Clojure by default since it’s immutable classes all the way
^ I second this. We just interop with java.time directly. My only gripe is the the inst reader macro is actually a java.util.Date and not an Instant (which I get for back-compat/historical reasons)
Thanks for these perspectives. The rationale on the clojure-time page seems reasonable, is it in general only used in edge cases then (I appreciate this is a question that probably belongs in #beginners, but I loiter here too and obviously this came up ...)
it’s a matter of taste I think, some people prefer to avoid interop, I prefer to not use libraries that “just” wrap a Java library unless it provides substantial benefits over using library directly, such as providing some data- or macrodriven abstraction over a heavy OOP / imperative API
in the case of java.time, IMO a wrapper lib is not warranted since the library is so easy to use via interop already
Thanks again!
np! 🙂
@will08rien At work we use a mixture of Java Time via interop and also clojure.java-time
-- the latter makes certain coercions and date arithmetic a lot "cleaner" (and easier to read) than plain interop.
Thanks Sean. I'll keep this in mind and try to make the decision on a case by case basis.
Hi all, I have a really basic build/deploy question. In https://github.com/eamonnsullivan/github-pr-lib, I have a simple little lib that uses the Github GraphQL API to create/modify/merge pull requests. It all worked fine until I had the bright idea to move the GraphQL queries and mutations into separate files and slurp them in to the source code like
(def get-repo-id-query (slurp "./src/eamonnsullivan/graphql/get-repo-id-query.graphql"))
Now, everything works fine locally, but not when I build and deploy the library.
Execution error (FileNotFoundException) at java.io.FileInputStream/open0 (FileInputStream.java:-2).
./src/eamonnsullivan/graphql/get-repo-id-query.graphql (No such file or directory)
What is the correct way to include resources like this in a build and how do I refer to them in the source code?