workspace
stringclasses
4 values
channel
stringclasses
4 values
text
stringlengths
1
3.93k
ts
stringlengths
26
26
user
stringlengths
2
11
clojurians
clojure
it lets you keep the logic you wanted without having all the race conditions (as long as you use it properly)
2017-11-21T17:35:35.000189
Margaret
clojurians
clojure
Oh, you're saying compare-and-set! should be used not only for the implementation of compareAndSet, but also for the other implementations too?
2017-11-21T17:37:05.000114
Earlie
clojurians
clojure
right - it lets you coordinate in a more flexible way than swap!
2017-11-21T17:37:51.000035
Margaret
clojurians
clojure
I apologize for not reading that more carefully. Makes sense. :slightly_smiling_face:
2017-11-21T17:38:08.000298
Earlie
clojurians
clojure
I didn't even know `compare-and-set!` was a thing until I went to implement IAtom.
2017-11-21T17:38:28.000013
Earlie
clojurians
clojure
<@Margaret> Like this? ``` (defn- project-g-counter [p] (reduce + (vals p))) ;; (defn- swap-g-counter [p n f &amp; args] ;; (let [oldv (project-g-counter p) ;; newv (apply f oldv args)] ;; (assert (number? newv) "Swap! G-Counter function must return a Number") ;; (assert (&gt;= newv oldv) "G-Counter is grow only") ;; (update p n + (- newv oldv)))) (defn- swap-g-counter [this f &amp; args] (let [p (.p this) n (.n this)] (loop [] (let [oldval @this newval (apply f oldval args) pval @p nval (update pval n + (- newval oldval))] (if (compare-and-set! p pval nval) newval (recur)))))) (deftype G-Counter [p n] clojure.lang.IDeref (deref [this] (reduce + (vals p)) ;; (project-g-counter @p) ) clojure.lang.IAtom (swap [this f] (swap-g-counter this f) ;; (project-g-counter (swap! p swap-g-counter n f)) ) (swap [this f a] (swap-g-counter this f a) ;; (project-g-counter (swap! p swap-g-counter n f a)) ) (swap [this f a b] (swap-g-counter this f a b) ;; (project-g-counter (swap! p swap-g-counter n f a b)) ) (swap [this f a b args] (apply swap-g-counter this f a b args) ;; (project-g-counter (apply swap! p swap-g-counter n f a b args)) ) (compareAndSet [this oldv newv] (loop [] (let [pval @p] (if (not= oldv (project-g-counter pval)) false (or (compare-and-set! p pval (swap-g-counter pval n + (- newv oldv))) (recur)))))) (reset [this newv] (assert (number? newv) "Reset! G-Counter value must be a Number") (swap! this (partial + (- newv @this))) newv)) (defn g-counter [] (new G-Counter (atom {:id 0}) :id)) ```
2017-11-21T17:51:19.000180
Earlie
clojurians
clojure
I don’t get what’s happening in `reset` but the rest looks better at a first glance
2017-11-21T17:53:04.000120
Margaret
clojurians
clojure
Rreset needs to take the value the user wants the counter to be and find the difference between the that and the current value of the counter, then it adds that to the value in the map the user is allowed to modify.
2017-11-21T17:54:48.000292
Earlie
clojurians
clojure
``` (reset [this newv] (assert (number? newv) "Reset! G-Counter value must be a Number") (swap! this #(+ % (- newv @this))) newv) ``` I had this originally. I suppose it's more readable.
2017-11-21T17:55:30.000187
Earlie
clojurians
clojure
Can a swap function deref the atom it's swapping?
2017-11-21T17:55:52.000153
Earlie
clojurians
clojure
It's weird, because derefing doesn't return the same value as the swap function accepts.
2017-11-21T17:56:25.000388
Earlie
clojurians
clojure
I don’t know if that’s correct yet, but fyi you can always replace `(swap! a #(f % b))` with `(swap! a f b)`
2017-11-21T17:56:33.000348
Margaret
clojurians
clojure
Oh yeah, good point. :thumbsup:
2017-11-21T17:56:52.000015
Earlie
clojurians
clojure
Much cleaner
2017-11-21T17:57:03.000132
Earlie
clojurians
clojure
Actually, swap does accept the current counter value. There's no need for the `@this`
2017-11-21T17:57:45.000119
Earlie
clojurians
clojure
a deref of the same atom inside the function arg to swap! is safe because if the atom changes you will retry the swap!
2017-11-21T17:57:53.000323
Margaret
clojurians
clojure
but yes, that too
2017-11-21T17:58:08.000255
Margaret
clojurians
clojure
That's what I thought, which is why I hadn't thought about it.
2017-11-21T17:58:29.000226
Earlie
clojurians
clojure
``` (reset [this newv] (assert (number? newv) "Reset! G-Counter value must be a Number") ;; (swap! this + (- newv @this)) (swap! this #(+ % (- newv %))) newv) ```
2017-11-21T17:58:36.000247
Earlie
clojurians
clojure
... I'm not sure why I'm doing this. It just needs to return the newv, and the swap! will handle the difference stuff.
2017-11-21T18:00:41.000062
Earlie
clojurians
clojure
``` (reset [this newv] (assert (number? newv) "Reset! G-Counter value must be a Number") (swap! this (constantly newv)) newv) ```
2017-11-21T18:01:48.000033
Earlie
clojurians
clojure
won’t the swap! call already return newv?
2017-11-21T18:02:24.000507
Margaret
clojurians
clojure
Uh, yeah, it will.
2017-11-21T18:02:42.000292
Earlie
clojurians
clojure
Well, that shrunk a lot...
2017-11-21T18:02:48.000201
Earlie
clojurians
clojure
It feels a little weird to use swap for reset, though I suppose that allows me to keep the difference logic in the same place.
2017-11-21T18:03:34.000311
Earlie
clojurians
clojure
I'm pretty happy with that. For anyone interested: ``` (defn- swap-g-counter [this f &amp; args] (let [p (.p this) n (.n this)] (loop [] (let [oldval @this newval (apply f oldval args) pval @p nval (update pval n + (- newval oldval))] (assert (number? newval) "Swap! G-Counter function must return a Number") (assert (&gt;= newval oldval) "G-Counter is grow only") (if (compare-and-set! p pval nval) newval (recur)))))) (deftype G-Counter [p n] clojure.lang.IDeref (deref [this] (reduce + (vals p))) clojure.lang.IAtom (swap [this f] (swap-g-counter this f)) (swap [this f a] (swap-g-counter this f a)) (swap [this f a b] (swap-g-counter this f a b)) (swap [this f a b args] (apply swap-g-counter this f a b args)) (compareAndSet [this oldv newv] (loop [] (let [pval @p] (if (not= oldv (project-g-counter pval)) false (or (compare-and-set! p pval (swap-g-counter pval n + (- newv oldv))) (recur)))))) (reset [this newv] (assert (number? newv) "Reset! G-Counter value must be a Number") (swap! this (constantly newv)))) (defn g-counter [] (new G-Counter (atom {:id 0}) :id)) ```
2017-11-21T18:05:23.000297
Earlie
clojurians
clojure
Thanks, <@Margaret>, <@Jonas>, <@Rebeca>! I appreciate the help.
2017-11-21T18:06:11.000051
Earlie
clojurians
clojure
how does the `deref` method work? isn’t `p` an atom?
2017-11-21T18:13:01.000402
Jonas
clojurians
clojure
seems like calling `vals` on `p` in the deref method would throw an exception
2017-11-21T18:14:13.000466
Jonas
clojurians
clojure
Yeah, that's a typo. I've fixed it: ``` clojure.lang.IDeref (deref [this] (reduce + (vals @p))) ```
2017-11-21T18:15:20.000247
Earlie
clojurians
clojure
I'm not sure when that got changed.
2017-11-21T18:15:29.000193
Earlie
clojurians
clojure
i might be reading it wrong, but seems like there’s still a race condition in your `swap-g-counter` function
2017-11-21T18:17:49.000013
Jonas
clojurians
clojure
the compare-and-set! will fail if p no longer olds pval
2017-11-21T18:18:25.000094
Margaret
clojurians
clojure
that’s the point of using that function
2017-11-21T18:18:30.000427
Margaret
clojurians
clojure
`p` is dereferenced twice
2017-11-21T18:18:40.000369
Jonas
clojurians
clojure
inside a loop that retries if the compare-and-set! fails
2017-11-21T18:18:52.000301
Margaret
clojurians
clojure
and the compare-and-set! will fail if p changes
2017-11-21T18:19:18.000039
Margaret
clojurians
clojure
so if it changes between the 1st and 2nd dereference, but not between the 2nd dereference and `compare-and-set!`
2017-11-21T18:19:26.000187
Jonas
clojurians
clojure
it would be inconsistent
2017-11-21T18:19:30.000185
Jonas
clojurians
clojure
since the new value is derived from both the 1st and 2nd dereference
2017-11-21T18:19:49.000234
Jonas
clojurians
clojure
and the first and second dereference might have different values
2017-11-21T18:20:00.000294
Jonas
clojurians
clojure
no- if it changes between binding oldval and compare-and-set! the compare-and-set will fail
2017-11-21T18:20:12.000080
Margaret
clojurians
clojure
`oldval` isn’t passed into `compare-and-set!`, `pval` is
2017-11-21T18:20:43.000243
Jonas
clojurians
clojure
oh, tricky
2017-11-21T18:21:05.000066
Margaret
clojurians
clojure
yeah, this is weird
2017-11-21T18:21:08.000308
Margaret
clojurians
clojure
Yeah, I need to get a better naming convention.
2017-11-21T18:21:33.000271
Earlie
clojurians
clojure
i think the code would be simplified a lot by having functions that work on immutable counter data
2017-11-21T18:22:01.000191
Jonas
clojurians
clojure
I think you need nested compare-and-set! calls to ensure consistency on both dereferences?
2017-11-21T18:22:16.000121
Margaret
clojurians
clojure
and using a plain ol’ atom to hold the data
2017-11-21T18:22:18.000057
Jonas
clojurians
clojure
if you really wanted it to work with this interface, you could have `pval` use the data from the `oldval` dereference
2017-11-21T18:23:02.000132
Jonas
clojurians
clojure
Yeah, I think I'm going to need to switch to immutable functions anyways for my merging logic. At the moment I'd need to have a function which takes one of these and a p (from somewhere else) and merge them. I'd also need another function to get the p out to send elsewhere.
2017-11-21T18:24:23.000413
Earlie
clojurians
clojure
the nice thing about atoms is that they just encapsulate identity. I don’t think you want to mix the identity and state pieces into one thing
2017-11-21T18:24:49.000419
Jonas
clojurians
clojure
pval can't use oldval, because oldval is just an integer.
2017-11-21T18:25:01.000243
Earlie
clojurians
clojure
Yeah, I think you're right.
2017-11-21T18:25:36.000152
Earlie
clojurians
clojure
right, I guess you would have to have `oldval` derive from `pval`
2017-11-21T18:25:40.000082
Jonas
clojurians
clojure
Good call, that's something that it could do.
2017-11-21T18:25:58.000183
Earlie
clojurians
clojure
Though I'd need to bring back project-g-counter for the reduction function.
2017-11-21T18:26:17.000450
Earlie
clojurians
clojure
yea
2017-11-21T18:26:51.000123
Jonas
clojurians
clojure
or as long as you dereference `pval` before you do the dereference for `oldval`, it would work
2017-11-21T18:28:19.000400
Jonas
clojurians
clojure
actually, nvmd
2017-11-21T18:28:29.000238
Jonas
clojurians
clojure
i take that back
2017-11-21T18:28:33.000062
Jonas
clojurians
clojure
I think you have to do it with a single dereference
2017-11-21T18:29:06.000189
Jonas
clojurians
clojure
other wise it could change in between dereferences and then change back
2017-11-21T18:29:21.000355
Jonas
clojurians
clojure
``` (defn- swap-g-counter [this f &amp; args] (let [p (.p this) n (.n this)] (loop [] (let [pval @p oldval (reduce + (vals pval)) newval (apply f oldval args) nval (update pval n + (- newval oldval))] (assert (number? newval) "Swap! G-Counter function must return a Number") (assert (&gt;= newval oldval) "G-Counter is grow only") (if (compare-and-set! p pval nval) newval (recur)))))) ```
2017-11-21T18:29:21.000388
Earlie
clojurians
clojure
Yeah, I'm pretty sure it's needed.
2017-11-21T18:29:30.000197
Earlie
clojurians
clojure
:thumbsup:
2017-11-21T18:30:17.000040
Jonas
clojurians
clojure
I'm using `wrap-json-response`, but I'd like to opt out and return `Content-Type: text/html` sometimes, is there an easy way out?
2017-11-21T18:45:22.000067
Lori
clojurians
clojure
(oh, I'm working within Compojure-API)
2017-11-21T18:45:37.000088
Lori
clojurians
clojure
so you have one endpoint, that would sometimes return json and sometimes return html?
2017-11-21T18:52:03.000303
Margaret
clojurians
clojure
<@Margaret> correct, based on their request header of `Content-Type`, so they can have json or html
2017-11-21T18:56:06.000355
Lori
clojurians
clojure
I think I need to just pull out this route from the main app, and add it back in separate without the offending middleware
2017-11-21T18:56:42.000251
Lori
clojurians
clojure
is there a less awkward solution tho, perhaps?
2017-11-21T18:56:53.000244
Lori
clojurians
clojure
there’s no rule saying you can’t generate json inside your handler (or have a conditional in your handler that either dispatches to an html function or a json function)
2017-11-21T18:57:11.000055
Margaret
clojurians
clojure
(the ring function `content-type` was a no go)
2017-11-21T18:57:12.000117
Lori
clojurians
clojure
those functions are just modifying data in a hash-map, if using them is at all awkward just skip them
2017-11-21T18:57:34.000351
Margaret
clojurians
clojure
oh no, I mean, I'd like `wrap-json-X` most of the time, but on one route, reset it. I think what I'm setting is getting over-written though by the middleware
2017-11-21T18:58:19.000116
Lori
clojurians
clojure
<@Lori> I don’t know if that’s wrong, but when I need to return a different content-type I just change manually and return something like this: {:status 200 :headers {“content-type” “text/html”} :body {:message (:message (any-function))}}
2017-11-21T18:58:47.000283
Audie
clojurians
clojure
yeah, if you want to conditionally make json you don’t want a middleware that always returns json
2017-11-21T18:58:59.000380
Margaret
clojurians
clojure
if you do that, the content-type is changed?
2017-11-21T18:59:12.000302
Audie
clojurians
clojure
that middleware isn’t doing as much as you think it is, just skip it
2017-11-21T18:59:18.000073
Margaret
clojurians
clojure
here I’m using `wrap-json-response` as well
2017-11-21T19:00:40.000009
Audie
clojurians
clojure
<@Audie> hm, I think it's not working, still debugging: ``` (-&gt; (ok {:x y) (assoc "headers" {"content-type" "text/html"})) Content-Type: application/json; charset=utf-8 ```
2017-11-21T19:03:09.000262
Lori
clojurians
clojure
How are configuring the handlers and middlewares of the app? can you put here?
2017-11-21T19:22:23.000084
Audie
clojurians
clojure
this is weird, I've checked that I'm setting the header where the route is defined, and turned off all middleware, but the header is still reverting to `"application/json"`
2017-11-21T19:24:22.000025
Lori
clojurians
clojure
the above isn't even valid code, so maybe start there before dumpling all the handlers and middlewares
2017-11-21T19:24:30.000082
Rebeca
clojurians
clojure
"headers" isn't the right key
2017-11-21T19:24:36.000029
Rebeca
clojurians
clojure
<https://github.com/ring-clojure/ring/blob/master/SPEC#L117>
2017-11-21T19:24:53.000120
Rebeca
clojurians
clojure
(it helps to use the `ring.util.response` functions to make sure you're creating valid responses)
2017-11-21T19:25:06.000207
Daniell
clojurians
clojure
<@Rebeca> apologies, I've mostly been using `(content-type "text/html")`
2017-11-21T19:25:18.000184
Lori
clojurians
clojure
and <@Daniell> thanks, ya that fn is in `ring.util.response`
2017-11-21T19:26:11.000073
Lori
clojurians
clojure
and <https://github.com/ring-clojure/ring-json/blob/master/src/ring/middleware/json.clj#L103-L112> clearly shows it doesn't check Content-Type at all to determine if it needs to encode
2017-11-21T19:27:45.000242
Rebeca
clojurians
clojure
the docstring here <https://github.com/ring-clojure/ring-json/blob/master/src/ring/middleware/json.clj#L114-L128> says it only encodes vectors or maps
2017-11-21T19:28:31.000123
Rebeca
clojurians
clojure
which of course doesn't match the actual behavior
2017-11-21T19:28:48.000040
Rebeca
clojurians
clojure
<https://github.com/ring-clojure/ring-json/blob/master/src/ring/middleware/json.clj#L107>
2017-11-21T19:28:49.000034
Rebeca
clojurians
clojure
<@Lori> And what exactly is your `ok` function doing?
2017-11-21T19:53:35.000001
Daniell
clojurians
clojure
just returns a simple map (`ring.util.http-response/ok`) ``` (defn ok "200 OK (Success) OK" ([] (ok nil)) ([body] {:status 200 :headers {} :body body})) ```
2017-11-21T19:54:39.000048
Lori
clojurians
clojure
Using your `ok` and `ring.util.response/content-type` and `ring-json`'s `ring.middleware.json/wrap-json-response` seems to work as expected: <https://www.dropbox.com/s/dzts47q5bwpg59x/Screenshot%202017-11-21%2016.57.17.png?dl=0>
2017-11-21T19:58:02.000165
Daniell
clojurians
clojure
The `(assoc "headers" ...)` you had before, definitely would not work (as <@Rebeca> said) because it uses a string instead of a keyword.
2017-11-21T19:58:56.000265
Daniell
clojurians
clojure
And even if you used `:headers`, you need to capitalize the header name for it to work.
2017-11-21T20:00:30.000163
Daniell
clojurians
clojure
Anyways, did you get it sorted out in the end? Or are you still looking for input? <@Lori>
2017-11-21T20:00:54.000305
Daniell