Fork me on GitHub

I used it at least once in universal gui datastructures viewer (as part of my debugger), very useful for infinite or long lazy lists. Thow it can be implemented in user code, via take or loop or reduce or any else way


I just tried to gui-inspect one funny structure (defn f [] (lazy-seq (repeat (f)))) 🙂


core.async has a private bounded-count (pre-dates the core one) but the args are the reverse of the one in core. messes me up every time.


I am trying to parse a string date for inserting into datomic using java-time lib - (jt/java-date "dd/MM/yyyy" "26/06/2020") - Can someone help on this one.


@murtaza52 we use this code

[java.text SimpleDateFormat]

(defn parse-date ^Date [^String format ^String date-string]
  (.parse (SimpleDateFormat. format) date-string))

(defn format-date ^String [^String format ^Date date]
  (.format (SimpleDateFormat. format) date))


hi @U0A6H3MFT thanks. let me try it out.


I'm trying to serialize operations executed inside a directory (e.g. executing only one git operation at a time) and the best thing I could make is this:

(defn update-repo-using
  [repo-path update-fn]
  ;; intern the repo-path string to make sure the same paths are actually the same object
  (locking (.intern repo-path)
Is that a good approach? it's slightly hacky with the .intern method call but otherwise the same Strings can be different objects and it wouldn't work. In general, I'd like to be able to serialize access on objects that are equal (or even use an arbitrary predicate) but not neccessarily the same instance.


@jumar that probably works if you have just one writer process


What do you mean by that?


if you have multiple processes (JVMs) writing to that file, this won't work


but often that's a reasonable assumption


Ah of course - that’s not an issue in this case


for clj-kondo I'm using RandomAccessfile which supports inter-process locks


That could work but I don’t really like file locks sonce they tend to be quite brittle and it’s more complicated in this case. But thanks for sharing that.


often the better answer is: use a database 😉


These are git repositories on the disk so that’s not really possible


Thanks for the link. Actually my colleague ended up using agent based approach which I found much more complicated and more error prone. The example in the SO answer isn’t really complete I think


Yeah. I think locking on interned strings could work, but you would need to be very careful that you didn't lock on these strings elsewhere, as you could end up with deadlocks, etc


May be a good approach


Good point about possible deadlock. I’ll chech Guava too.

Daniel Stephens12:06:44

for the general equals case, perhaps you could have a memoized function that looks up your locking object (def m (memoize (fn [s] (Object.)))) and lock on the value of that function, should work for values other than strings

👍 3

Is the memoized version guaranteed to be thread safe, that is to return always the same lock object when multiple threads call it at once? Looking at the implementation I’d say it could return two or more different locks...

Daniel Stephens12:06:26

that's a good point, I think the current implementation is as you say, you could reimplement the same functionality that would be thread-safe to that extent


@jumar memoize is not thread-safe

Daniel Stephens13:06:18


(defn safe-memoize [f]
  (let [mem (atom {})]
    (fn [& args]
      (swap! mem (fn [m] (if (contains? m args)
                           (assoc m args (apply f args)))))
      (get @mem args))))
be a threadsafe version?


you can get away with using the result value of swap!

👍 3