This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-06-19
Channels
- # announcements (9)
- # babashka (11)
- # beginners (157)
- # calva (10)
- # cider (18)
- # clara (4)
- # clj-kondo (40)
- # cljsrn (8)
- # clojure (29)
- # clojure-europe (11)
- # clojure-italy (1)
- # clojure-nl (2)
- # clojure-spec (4)
- # clojure-sweden (1)
- # clojure-uk (39)
- # clojurescript (32)
- # conjure (1)
- # core-async (2)
- # cursive (20)
- # datomic (7)
- # duct (9)
- # emacs (1)
- # figwheel-main (1)
- # fulcro (24)
- # helix (1)
- # hoplon (20)
- # hugsql (3)
- # jackdaw (5)
- # jobs-discuss (7)
- # lambdaisland (1)
- # malli (5)
- # music (4)
- # off-topic (54)
- # parinfer (2)
- # pedestal (13)
- # re-frame (12)
- # reagent (22)
- # reitit (9)
- # shadow-cljs (89)
- # spacemacs (2)
- # xtdb (21)
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
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)
(update-fn)))
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.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.
Similar question on StackOverflow suggests agents: https://stackoverflow.com/questions/6404717/idiomatic-file-locking-in-clojure
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
http://clojuredocs.org/clojure.core/locking this does exist if you need it
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
Not Clojure, but Guava has a Striped lock concept: https://guava.dev/releases/19.0/api/docs/com/google/common/util/concurrent/Striped.html
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
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...
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
would
(defn safe-memoize [f]
(let [mem (atom {})]
(fn [& args]
(swap! mem (fn [m] (if (contains? m args)
m
(assoc m args (apply f args)))))
(get @mem args))))
be a threadsafe version?