Fork me on GitHub

For small and specifically performance-sensitive sections in a program, is it possible to write "imperative with local mutable variables" style code using Clojure? In other words, is it possible to "write Java in Clojure"? The closest thing I could find were volatiles, and those are still one extra layer of boxing, right?


For what it's worth, I understand this goes heavily against the idioms of Clojure in the standard case.


@mingp, if you're thinking of writing "Java in Clojure", why not just write the Java code and call that? What I mean is, Java's a very good Java, if that's what you want 🙂


you can write “java in clojure”


@the2bears Yes, that's the other option. Mainly, the benefit from a theoretical "Java in Clojure" would be locality/organization of the source files, for whatever that's worth.


depending on how much performance you need to eek out


most of the code I’ve seen that focuses on performance talks about type hints to avoid reflection which is one of the slower parts


@smith.adriane I'm looking for on-par with writing regular imperative Java. Which is to say, I need mutable locals in an unboxed form.


are you creating lots of volatiles?


Yes, and using things like transient collections, as long as they don't leak outside the function.


you may be able to create them once and reuse them, depending on the context


For now, it's purely hypothetical. I want to know if I have this option available before I get into it.


Because, whether I do or don't have this option available will probably change what I do.


because other than creation, I think it’s the same as just regular java mutation


Can you be more clear by what you mean with "write Java in Clojure" though? There's a wide scope of what it could mean.


@the2bears Say I take a generic computer algorithms textbook. It has well-known algorithms in a form that can be translated near-verbatim into Java, same as any other traditional imperative language. If I wanted to do the same for Clojure, and maintain Java-level performance, my (admittedly limited) understanding is that I would need mutable locals.


I see. I haven't tried (at least on purpose) to program imperatively in Clojure having spent so much energy to leave Java behind 🙂 I can't think of anything beyond mutability you'd need, it seems all flow control mechanisms are there.


For the most part, I try to learn and write idiomatic Clojure. But for that low-single-digit percent of code that needs it, I'm exploring options for getting Java-level performance.


I'd just use Java for that.


Yea, that makes sense.


yeah - particularly because of the issues with boxed numerics, it's just easier to write a bit of java once you know which code has to be that fast


@noisesmith True. Although, it seems like (from what I can tell from, unboxed math is possible in Clojure.


it's possible, it's a pain to get right


@the2bears @smith.adriane @noisesmith Well, thank you for your responses. I think I should go reconsider what I am doing and why, as it sounds like I may be entering "when all you have is a hammer" territory.


I think one of the nice things for clojure is that if you do want to use some of the great work in the jvm ecosystem, you can use it as needed


So, I just went to look at the Benchmarks Game to see how they micro-optimized algorithms in Clojure. Unfortunately, it looks like, at some point recently, they removed Clojure from their languages list.

Alex Miller (Clojure team)03:10:09

Looks like it was removed from the web site on sept 20th, along with scala?


@lovuikeng Looks like that one's full of dead links, unfortunately. I can click into the language pages, but not the implementation source code pages. It just returns 404 Not Found.


2016 clojure removed? Guess we'll have to wait until 2032... "abandoned in 2002...restarted in 2004...continued from 2008...Everything has changed; several times."


anyone know a handy way to detect circular dependencies in graphs?


I took a look at ubergraph and loom, no luck


By chance I happened across this yesterday: It sounds like it's maybe not what you need, @U45T93RA6, but FYI just in case it's useful to you.


should be translated to clojure very quickly


can eventually try out, thanks! meanwhile checking if I can steal something off stuartsierra/component


guess so! (def has-circular-dependencies? (complement dag?)) ?


yes, a dag has no cycle by definition


this function won't give you the culprit however


Does anyone know how to add accents or diacritical marks to characters? As in add ¨ to a to make ä?


Found that you can do "e\u0305".

metametadata11:10:36 Comparing to java.jdbc I like clearer docs and simpler API, especially that there's a difference between connections/dbspecs and how transactions are implemented. Other advantages are pointed out in the FAQ:


I'm not having much luck trying to write a pmap-like function that times-out after a specified number of milliseconds. I tried two implementations: one with futures and one with core.async. The more complicated futures one passes tests but doesn't stop processes when running in my project. The core.async one is even worse in that regard. I'm new to core.async. If you care to take a look, the code is here: There are tests in utils4pmap_test.clj


future-cancel is unreliable, if you want something to be cancellable you need to ensure it regularly calls some cancellable method (the list is relatively short)