workspace
stringclasses
4 values
channel
stringclasses
4 values
text
stringlengths
1
3.93k
ts
stringlengths
26
26
user
stringlengths
2
11
clojurians
clojure
Np! A small side note: your original implementation can be simplified to ``` (defn all-combinations "Return all possible combinations of values from as and bs collections." [as bs] (for [a as b bs] [a b])) ```
2017-11-21T09:34:46.000684
Adrien
clojurians
clojure
oh lovely! thankyou
2017-11-21T09:35:46.000691
Timmy
clojurians
clojure
Plz help cteate and use data base. Telegram Bot. Morse dep
2017-11-21T09:36:34.000524
Dakota
clojurians
clojure
🦌
2017-11-21T09:40:34.000407
Dakota
clojurians
clojure
🧐
2017-11-21T09:45:31.000038
Dakota
clojurians
clojure
:flushed:
2017-11-21T09:45:38.000234
Dakota
clojurians
clojure
<@Dakota> Are you the beginning of the singularity? Morse dep, bro!
2017-11-21T12:01:22.000303
Hedwig
clojurians
clojure
Hello, wanted to hear your opinions on `honeysql` and `yesql`. I want to build a small web app on top of a RDBMS and I wonder how should I handle the queries. Any thoughts or past experience?
2017-11-21T12:24:10.000178
Mina
clojurians
clojure
I prefer <#C3EATG7EV|hugsql>
2017-11-21T12:24:51.000745
Mia
clojurians
clojure
you can also ask in <#C1Q164V29|sql>
2017-11-21T12:25:13.000164
Mia
clojurians
clojure
i have a long list of `let` bindings where a conditional could branch an error at any time options: 1. I have many-nested `(let [...] (cond ... (let [...] (cond ...` 2. ~would it be idiomatic to build the `let` "context" as a record getting passed down a `(cond-&gt;`?~ 3. something else?
2017-11-21T15:07:14.000568
Lori
clojurians
clojure
actually, step 2 doesn't seem to work like I want
2017-11-21T15:08:59.000184
Lori
clojurians
clojure
basically, I'd just like a long list of let bindings, that depend on things before them, but if I have a conditional branch during that process, I can "short circuit" the whole thing
2017-11-21T15:09:41.000319
Lori
clojurians
clojure
Is there a way for a deftype to extend Atom, or do I need to have an atom in its parameters and implement the lang.IAtom interface?
2017-11-21T15:11:33.000323
Earlie
clojurians
clojure
I assume I should do that instead of manually using mutable fields? (using `set!`?)
2017-11-21T15:12:13.000530
Earlie
clojurians
clojure
<@Earlie>, there are some cases where manually setting mutable fields might be more straightforward (eg. java interop)
2017-11-21T15:18:40.000464
Jonas
clojurians
clojure
why won’t using a regular atom work for your usecase?
2017-11-21T15:19:11.000160
Jonas
clojurians
clojure
<@Lori> let-later from useful works for that kind of thing <https://github.com/amalloy/useful/blob/develop/src/flatland/useful/utils.clj#L224>
2017-11-21T15:22:48.000092
Margaret
clojurians
clojure
<@Jonas> I'm using deftype, because there are a number of other protocols to be implemented and modifications to the normal atom functions. I didn't know if there was a way for it to extend an atom, since its base data is just an atom.
2017-11-21T15:30:46.000002
Earlie
clojurians
clojure
interesting, that could possibly work, but I'd rather not bring in a big dependency, nor copy out what I need from EPL'd code... is there not a standard trick for me to obviate heavily nested code?
2017-11-21T15:32:21.000273
Lori
clojurians
clojure
the typical approach is to extend all the interfaces / protocols atom implements, instead of concrete inheritance from atom itself
2017-11-21T15:33:14.000138
Margaret
clojurians
clojure
I usually implement `IDeref` when I want a thing that acts like an atom
2017-11-21T15:33:17.000202
Hugo
clojurians
clojure
```=&gt; (supers (class (atom nil))) #{clojure.lang.IRef java.lang.Object clojure.lang.IMeta clojure.lang.AReference clojure.lang.ARef clojure.lang.IDeref clojure.lang.IReference clojure.lang.IAtom}```
2017-11-21T15:34:34.000555
Margaret
clojurians
clojure
it depends what you need - IDeref doesn’t help with eg. swap! or add-watch
2017-11-21T15:35:04.000342
Margaret
clojurians
clojure
isn’t `swap!` specifically for atoms and not any other reference type?
2017-11-21T15:35:50.000408
Jonas
clojurians
clojure
it depends what someone means by “acting like an atom” here - it’s fully in our power to make something that is a drop in replacement for an atom
2017-11-21T15:36:57.000208
Margaret
clojurians
clojure
not that I know of - you could do something similar to what let-later is doing, but more manually - making delays and only realizing them as you need them for example
2017-11-21T15:37:53.000581
Margaret
clojurians
clojure
there are also “monad” libraries that use the Either monad for a similar result
2017-11-21T15:38:09.000356
Margaret
clojurians
clojure
right. I ask mostly because most of the time when I’ve seen someone try to implement `IAtom`, they don’t plan to also implement all of the same semantics. if you’re not implementing all of the same semantics, I think it probably makes more sense to just use another function to manipulate your reference type just like the other clojure reference types each have their own functions, (eg. `vswap!`, `send`, `ref-set`, etc)
2017-11-21T15:41:19.000630
Jonas
clojurians
clojure
Thanks, guys, this helps.
2017-11-21T15:44:52.000605
Earlie
clojurians
clojure
<@Earlie> going back to your initial question, `proxy` can extend an atom, or you can implement all the right protocols / interfaces to replicate an atom (maybe even using a closed over atom) with deftype, reify, defrecord, etc.
2017-11-21T15:45:05.000319
Margaret
clojurians
clojure
but it really depends on what you are trying to do, which behaviors you need, what you exactly you wanted from IAtom
2017-11-21T15:45:25.000049
Margaret
clojurians
clojure
because extending a clojure built in tends to be a bit tedious because of how the interfaces / protocols are factored
2017-11-21T15:45:48.000095
Margaret
clojurians
clojure
Closed over meaning the deftype includes an atom as a parameter?
2017-11-21T15:45:48.000666
Earlie
clojurians
clojure
as a parameter, or just creating a reify inside a let block that defines the atom - there’s a few ways to “capture” an atom for internal usage
2017-11-21T15:46:20.000147
Margaret
clojurians
clojure
:thumbsup:
2017-11-21T15:46:39.000308
Earlie
clojurians
clojure
<@Margaret> When implementing IAtom's swap functions, is there a way to reduce duplication of swapping code? Do I need the 4 different arrity versions?
2017-11-21T15:55:43.000611
Earlie
clojurians
clojure
since you can’t control the caller, and the interface supports all those arities, I think the only safe thing is to define them all (even if they all end up calling the same variadic function)
2017-11-21T15:56:53.000063
Margaret
clojurians
clojure
clojure will let you skip arities or even methods, but it will lead to a runtime error if they get called
2017-11-21T15:57:26.000102
Margaret
clojurians
clojure
Is there a performance benefit of not colling the other arities of the type (duplicating the function). (e.g. should I call deref on the `this` argument, or does it make sense to reimplement the logic of the deref inline?)
2017-11-21T15:57:47.000628
Earlie
clojurians
clojure
Oh, so an external variable arity function could be called by all of them. That's probably the cleanest.
2017-11-21T15:58:21.000531
Earlie
clojurians
clojure
that’s a more general software design question isn’t it? sometimes it’s worth it to avoid the abstraction for performance / resource reasons, sometimes it makes sense to abstract and avoid the repetition
2017-11-21T15:58:36.000498
Margaret
clojurians
clojure
right, yeah, that’s probably cleanest
2017-11-21T15:58:47.000673
Margaret
clojurians
clojure
Yeah, I suppose it is. Thanks!
2017-11-21T15:58:58.000204
Earlie
clojurians
clojure
<@Margaret> You mind giving me your opinion on this?: ``` (defn- swap-g-counter [this f &amp; args] (let [oldv @this newv (apply f oldv args)] (assert (number? newv) "Swap! G-Counter function must return a Number") (swap! (.p this) update (.n this) + (- newv oldv)) @this)) (defn- compare-and-set-g-counter [this oldv newv] (let [value @this] (if (not= oldv value) false (do (swap! this + (- newv value)) true)))) (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 b)) (swap [this f a b args] (apply swap-g-counter this f b args)) (compareAndSet [this oldv newv] (compare-and-set-g-counter this oldv newv)) (reset [this newv] (swap! this + (- newv @this)) newv)) (defn gcounter [] (new G-Counter (atom {:id 0}) :id)) ```
2017-11-21T16:09:39.000650
Earlie
clojurians
clojure
using @ and swap! in the same function is usually a race condition
2017-11-21T16:10:21.000662
Margaret
clojurians
clojure
On the reset function? So the `@this` should be bound before `swap!`?
2017-11-21T16:11:20.000024
Earlie
clojurians
clojure
you probably want compare-and-set! which allows more complex logic
2017-11-21T16:11:20.000422
Margaret
clojurians
clojure
<@Earlie> at a quick skim, every single function that uses @ and swap! in the same body is a likely race condition
2017-11-21T16:11:57.000448
Margaret
clojurians
clojure
I mean, you even have compare-and-set in the name, so implementing via compare-and-set! is much more likely to be correct
2017-11-21T16:12:23.000458
Margaret
clojurians
clojure
I'm not following how it would be a race condition.
2017-11-21T16:13:00.000234
Earlie
clojurians
clojure
and yes, looking closer, those are definite race conditions in both of the first two functions
2017-11-21T16:13:05.000028
Margaret
clojurians
clojure
Right, I didn't think about how the normal `compare-and-set!` would use my custom reset logic. I'll switch to that.
2017-11-21T16:13:27.000174
Earlie
clojurians
clojure
<@Earlie> `(+ newv oldv)` - this is a race condition, oldv came from a deref, the swap! itself will retry, but your function won’t so it will use stale data in a race
2017-11-21T16:13:57.000086
Margaret
clojurians
clojure
`(not= oldv value)` - the truth value of this can change before the swap! runs
2017-11-21T16:14:27.000659
Margaret
clojurians
clojure
<@Margaret> Oh! Thanks for clerifying that. I'm following now.
2017-11-21T16:14:30.000288
Earlie
clojurians
clojure
I'd completely neglector the retrying functionality of atoms.
2017-11-21T16:14:56.000575
Earlie
clojurians
clojure
<@Margaret> Oh, I can't use the normal `compare-and-set!`, because I'm not wanting the atom reset to the newval; only a subset of the atom can ever be set.
2017-11-21T16:16:14.000630
Earlie
clojurians
clojure
`compare-and-set!` uses a java interop `.compareAndSet`
2017-11-21T16:16:40.000658
Earlie
clojurians
clojure
you can’t use it to implement your full functionality, but to preserve expected atom semantics it needs to be your building block
2017-11-21T16:17:09.000314
Margaret
clojurians
clojure
It is? `compareAndSet`
2017-11-21T16:17:31.000038
Earlie
clojurians
clojure
Between swap and reset
2017-11-21T16:17:49.000255
Earlie
clojurians
clojure
you can’t just mix @ and swap! of the same atom and expect correctness of any sort
2017-11-21T16:17:52.000661
Margaret
clojurians
clojure
but you can get it by using compare-and-set, with your own retry condition, etc.
2017-11-21T16:18:08.000669
Margaret
clojurians
clojure
Oh, I think I'm following. I'll try implementing that.
2017-11-21T16:18:41.000451
Earlie
clojurians
clojure
the idea is to use compare-and-set to wrap both the read and the modification, and then recur as your retry if the operation did not succeed
2017-11-21T16:19:20.000413
Margaret
clojurians
clojure
is there a reason you want your reference type to use the atom functions instead of creating new functions to interact with your reference type?
2017-11-21T16:25:06.000076
Jonas
clojurians
clojure
Mostly as a proof of concept / out of curiosity at the moment.
2017-11-21T16:26:07.000133
Earlie
clojurians
clojure
it seems like instead of putting counter related logic inside of `swap!` and `deref`, it might make more sense to put that logic in another function that takes an atom as an argument
2017-11-21T16:27:10.000030
Jonas
clojurians
clojure
Well my thought was to abstract it, so users can completely ignore the implementation details of the crdt. :shrug: I think I'll do it both ways and see how it feels.
2017-11-21T16:28:22.000355
Earlie
clojurians
clojure
Not a Monad, no. By fmap I mean (at the minimum) to be able to apply a function to the members of something that this can be done to such that it creates another thing that this can be done to whose members are the result of applying that function. This is very much something we’d want to do with abstract data structures. Like a vector or a map. If it happens to follow the rules of being a Functor (which a Monad does, yes), then great.
2017-11-21T16:28:46.000433
Pattie
clojurians
clojure
I would actually probably have the functions work on immutable data
2017-11-21T16:28:47.000041
Jonas
clojurians
clojure
and then let the consumers of the counter decide if they want to use an atom or other reference type
2017-11-21T16:29:05.000318
Jonas
clojurians
clojure
Yeah, that's likely how I'd do it.
2017-11-21T16:29:19.000013
Earlie
clojurians
clojure
How can I get two `swap!`s to run at the same time to test retrying?
2017-11-21T16:51:41.000331
Earlie
clojurians
clojure
Oh, it was just too fast... A Thread/sleep did the trick.
2017-11-21T16:52:49.000381
Earlie
clojurians
clojure
yeah - adding sleeps (maybe randomized) is a quick way to get race conditions to happen
2017-11-21T16:53:34.000129
Margaret
clojurians
clojure
Randomized is a good idea
2017-11-21T16:53:56.000285
Earlie
clojurians
clojure
combining randomized timeouts / sleeps with looped calls is a great way to stress test things that might misbehave
2017-11-21T16:54:35.000120
Margaret
clojurians
clojure
(in terms of race conditions that is)
2017-11-21T16:55:00.000587
Margaret
clojurians
clojure
<@Margaret> So I'm still not quite seeing the purpose of `compare-and-set!`. Is there a reason I shouldn't just implement `reset!` inside it with the new value if the old value is the expected value?
2017-11-21T16:58:43.000149
Earlie
clojurians
clojure
the point of compare-and-set! is that atoms don’t lock, and you need to be able to retry if the value changes before your update completes
2017-11-21T16:59:29.000034
Margaret
clojurians
clojure
Which `reset!` would do, right?
2017-11-21T17:00:00.000427
Earlie
clojurians
clojure
no
2017-11-21T17:00:05.000492
Margaret
clojurians
clojure
how would it even know?
2017-11-21T17:00:09.000431
Margaret
clojurians
clojure
Oh, unless you have some sort of abstract logic based on the value?
2017-11-21T17:00:19.000315
Earlie
clojurians
clojure
I think I'm starting to understand.
2017-11-21T17:00:27.000005
Earlie
clojurians
clojure
reset! never retries, and the condition you would need to detect in order to retry is outside the scope of the reset! call
2017-11-21T17:00:32.000434
Margaret
clojurians
clojure
which is why you have compare-and-set!
2017-11-21T17:00:41.000002
Margaret
clojurians
clojure
<@Margaret> Mind giving this another look? ``` (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)))) (deftype G-Counter [p n] clojure.lang.IDeref (deref [this] (project-g-counter @p)) clojure.lang.IAtom (swap [this f] (swap! p swap-g-counter n f) @this) (swap [this f a] (swap! p swap-g-counter n f a) @this) (swap [this f a b] (swap! p swap-g-counter n f a b) @this) (swap [this f a b args] (apply swap! p swap-g-counter n f a b args) @this) (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 gcounter [] (new G-Counter (atom {:id 0}) :id)) ```
2017-11-21T17:21:10.000413
Earlie
clojurians
clojure
that is all race conditions
2017-11-21T17:23:19.000134
Rebeca
clojurians
clojure
<@Earlie> I can see you are trying but <@Rebeca> is right - all the code that uses deref and swap! in the same block of code is setting up a race of some sort
2017-11-21T17:24:41.000223
Margaret
clojurians
clojure
swap returns the value swapped in, derefing after the swap could return anything
2017-11-21T17:25:42.000278
Rebeca
clojurians
clojure
what I meant about compare-and-set! is that to make this code correct the whole thing needs to be inside a compare-and-set! loop which can either let the caller know if it failed or retry if there was any modification outside
2017-11-21T17:26:07.000344
Margaret
clojurians
clojure
Oh, right, I need to use `project-g-counter` on the return value of `swap!` instead. Is there something somewhere else?
2017-11-21T17:26:33.000213
Earlie
clojurians
clojure
the various places you call swap! then return @this for example
2017-11-21T17:26:59.000607
Margaret
clojurians
clojure
I realize that problem now: ``` clojure.lang.IAtom (swap [this f] (project-g-counter (swap! p swap-g-counter n f))) (swap [this f a] (project-g-counter (swap! p swap-g-counter n f a))) (swap [this f a b] (project-g-counter (swap! p swap-g-counter n f a b))) (swap [this f a b args] (project-g-counter (apply swap! p swap-g-counter n f a b args))) ```
2017-11-21T17:27:40.000153
Earlie
clojurians
clojure
someone posted and example of using `compare-and-set!` recently, <https://github.com/plumatic/plumbing/blob/master/src/plumbing/core.cljx#L360>
2017-11-21T17:28:56.000328
Jonas
clojurians
clojure
Is there something wrong with the implementation of compare-and-set! above?
2017-11-21T17:30:05.000155
Earlie
clojurians
clojure
you don’t seem to understand why I was mentioning compare-and-set! - in order to check an arbitrary condition and retry if the value is modified and also return an arbitrary value other than the thing you just modified, compare-and-set! is the ideal tool
2017-11-21T17:35:11.000033
Margaret