clojure

New to Clojure? Try the #beginners channel. Official docs: https://clojure.org/ Searchable message archives: https://clojurians-log.clojureverse.org/
lgessler 2021-05-05T00:48:19.094100Z

I want to ship an uberjar foo.jar to my users that will allow them to optionally create a sibling file config.edn if they want to override defaults. what's the right way of locating this config.edn in my code so I can read it?

2021-05-05T01:00:15.094400Z

Depends, using -D to set a jvm property to point to the file and read it is not uncommon

๐Ÿ‘ 1
2021-05-05T01:01:13.094500Z

Depending on how your uberjar is being used you might stipulate that the config file be on the classpath and use io/resource to load it

2021-05-05T01:02:02.094600Z

(combining classpath specification with uberjars can be a little whacky though)

lgessler 2021-05-05T01:03:21.095600Z

oh duh, yeah, making it a param is not a bad idea. still curious about whether there's a not-terribly-hacky answer to my original question though

2021-05-05T01:05:06.095700Z

The user.dir system property gives you the current working directory when the jvm is launched, but that may not have anything to do with where your jar file lives

2021-05-05T01:08:24.095800Z

You can do something like look up a resource you know is in your jar, the url you get back will have the path to your jar file in it, so munge that

๐Ÿ‘ 1
2021-05-05T01:10:37.095900Z

Oh, look at that https://stackoverflow.com/a/16240426

๐Ÿ‘ 1
lgessler 2021-05-05T01:37:52.096600Z

nice! all good ideas, thanks

seancorfield 2021-05-05T01:56:50.096800Z

Ask in #clj-kondo but I think thereโ€™s a โ€œcatch-allโ€ def style linter you can have it lint-as that might help?

matheusashton 2021-05-05T02:12:25.097100Z

I'll put the question there

Janne Sauvala 2021-05-05T17:05:39.099600Z

Does anyone know good tutorials, articles or example repos of good ways to wrap Java/JS libs with Clojure(Script)? Iโ€™m interested in looking best ways to wrap libs that are mutating their internals and how to handle those smoothly in Clojure

isak 2021-05-05T17:52:18.099700Z

โ€ข Aleph (wraps Netty): https://github.com/clj-commons/aleph โ€ข rolling-stones (wraps Sat4J): https://github.com/Engelberg/rolling-stones

vemv 2021-05-05T18:23:36.100Z

some quick tips on making lib's mutability more safely consumable: - wrap the op under a threadlocal - create an object once per defn invocation and don't let references to it 'escape' to callers

vemv 2021-05-05T18:25:04.100200Z

the core idea is that a properly designed OOP lib, though mutable, will have that mutability limited to single objects (vs. global mutability). So it's fairly easy to control a single object

emccue 2021-05-05T18:44:34.100500Z

In general though if a lib is a stateful thing and that statefulness can't be "contained" then you will need to use the standard mechanisms for controlling that

emccue 2021-05-05T18:44:45.100700Z

e.g. the system pattern

emccue 2021-05-05T18:45:17.100900Z

obvious sort of example would be a database pool

emccue 2021-05-05T18:45:47.101100Z

and maybe that means there isn't going to be much benefit to doing a clojure wrapper beyond syntax convenience

Janne Sauvala 2021-05-05T20:28:28.101400Z

Thanks for the examples and tips. Like Ethan said, I think in my case that I have been thinking most of the benefit is coming from the nicer syntax when using with Clojure. It started to be quite tedious to write interop code all the time so Iโ€™m thinking about writing a wrapper to do my bidding

walterl 2021-05-05T22:29:47.103200Z

What's the idiomatic way to apply a decorator to a function defined with defn? Replace the defn's with (def decorated (decorator (fn ...)))?

dpsutton 2021-05-05T22:33:08.104Z

(defn foo [])
(alter-var-root #'foo decorator)
i saw tim baldridge do this with memoize and thought it was interesting

dpsutton 2021-05-05T22:34:51.104500Z

(alter-var-root #'index-data memoize) from his amazing https://github.com/halgari/build-your-own-logic-engine/blob/master/src/build_your_own_logic_engine/core.clj#L350

walterl 2021-05-05T22:37:26.104800Z

That certainly looks better, thanks! ๐Ÿ™Œ

walterl 2021-05-05T22:40:26.105Z

Best of all, it doesn't offend Eastwood in the way that my re-def-ing did ๐Ÿ˜… ๐ŸŽ‰

rutledgepaulv 2021-05-05T22:41:33.105200Z

another option:

rutledgepaulv 2021-05-05T22:41:48.105400Z

(defmacro defmemo
  "Define a function with a memoized implementation."
  [& defnargs]
  `(doto (defn ~@defnargs)
     (alter-var-root #(with-meta (memoize %1) (meta %1)))))

walterl 2021-05-05T22:43:18.105600Z

Nice!