This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
hello everyone, i new in clojure. I start learn it by 4clojure puzzles. And i have trouble with varargs, please give me referens to few examples with it. Thank PS Sorry for miss chanel
Is there a more idiomatic way to calculate "map difference" (map with keys which are not in another map) than (select-keys a (clojure.set/difference (set (keys a)) (set (keys b))))
? It works, but it looks ugly.
@dottedmag: useful has a filter-keys
function which you could use to do (filter-keys a (set (keys b))
or summat https://github.com/amalloy/useful/blob/develop/src/flatland/useful/map.clj#L223-L227
@timvisher: Thanks. Useful looks, uhm, useful.
@dottedmag: ah. it looks like suchwow has exactly what you want though https://github.com/marick/suchwow/blob/master/src/such/maps.clj#L48-L56
@dottedmag: if you weren’t already familiar, useful, plumbing, and suchwow are the defacto bag-o-stuff libraries. i generally check them before i implement my own thing
@timvisher: I had hoped that clojure is not infected with underscore.js-ism, but alas.
By the way, is there a way to reexport symbols from the namespace? I'm thinking about project.util, pulling in useful functions from bag-o-stuff libraries.
fwiw any of the libraries i listed above are not that defacto (they’re not jquery/underscore). but i hate re-implementing things so if someone else already has, 🤷
@dottedmag: you might look at something like this https://github.com/marick/suchwow/blob/master/src/such/better_doc.clj
fwiw, i wouldn’t bother. if you want it for the docs then maybe just a basic wrapper function would do. if you don’t want it for the docs then the extra weight in the jar doesn’t matter that much
What's underscore.js-ism? (And what's wrong with it?)
It's a disease of languages with such a stagnant standard libraries that a bunch of "you-ought-to-put-it-into-stdlib-but-can't-be-bothered-so-I-wrote-a-separate-one" libraries spring into an existence.
Is there any language where you can say the standard libraries are so complete?
I think it’s a bit of an unfair comparison, really, and highly personal. Libraries like underscore and lodash present for JavaScript more than just ‘this should be in baked in’. I like the fact that, for the most part, Clojure’s core libraries are fairly lean. At the same time, I can understand why they’re in 1.8 adding a bunch of wrappers, for instance, in core.string for standard activities (e.g., starts-with?
and index-of
).
speaking of all these back-o-stuff libraries, I'm wondering if this is a common pattern at all?
(defn gen
"Returns a function that, when called, will call f with an incremented
generation value."
[f]
(let [generation (volatile! -1)]
(fn []
(vswap! generation inc)
(f @generation))))
I had a recursive process where I didn't want to introduce the logic to increment the counter each time through the loop so I put that logic in this wrapper.
Where the heck is the documentation on volatiles? I know I read something about them, but can't find anything on http://clojure.org
https://clojuredocs.org/clojure.core/volatile! and http://stackoverflow.com/questions/31288608/what-is-clojure-volatile seem to provide a decent overview.
I know it's not a great state of affairs when SO answers are more informative than the official documentation, but on the other hand, thank God for SO.
@amacdougall: Yeah, that was about all I found as well. Was hoping to find more examples, especially when used outside of a transducer, such as I'm doing here.
In my case I could use an atom as well, since performance isn't really an issue where I'm using this function.
That's way beyond me, I'm afraid. Probably someone on here can help, but you might need to ask again later. Depends on who's on.
@bronsa: Thanks. I'm actually good with volatile, just surprised there were no real docs on http://clojure.org is all.
Say, has anyone used the (is (thrown? ExceptionClass <body>))
form in clojure.test to catch SQL exceptions during invalid database usage? I've got this test:
; user-values was used to create a user earlier in the test, so this creation attempt violates a unique value constraint
(testing "creating user with duplicate email fails"
(is (thrown? SQLException (db/create-user<! user-values))))
But instead of catching the exception and reporting a success, the test runner surfaces the exception and reports an error. The actual exception type is org.postgresql.util.PSQLException
, but since that extends java.sql.SQLException
, it should be caught (and is caught, when I try/catch it in the REPL). And of course I've got (:import java.sql.SQLException)
in my ns
declaration.Just as a smoke test, I verified that the usual example, (is (thrown? ArithmeticException (/ 1 0)))
, does work.
Hm... when I change it to (thrown? Error
, it gives me slightly more useful console output:
ERROR in (test-user-creation) (QueryExecutorImpl.java:2270)
│creating user with duplicate email fails
│expected: (thrown? Error (db/create-user<! user-values))
│ actual: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "users_username_key" │
│ Detail: Key (username)=(tbogard) already exists.
...but since PSQLException is an Error, I'd think this assertion would pass. In Java, subclasses are considered instances of their superclass, right?Anyway, if I test (thrown org.postgresql.util.PSQLException
, it fails just the same, but it's worth noting that when ... oh. I get it. Since there was an exception, the transaction is aborted, so further tests can't occur!
Clearly I need to think carefully about when (or whether) to do multiple tests in one transaction.
@amacdougall: someone I work with just ran into a very similar issue, we have most of our db-affecting tests running in transactions (also against postgres)
He just figured out what was happening at the end of the day, hasn’t thought of a solution yet for it
Perhaps it should have been obvious when I noticed that I was not seeing the exception I expected, because it was correctly being caught by (is (thrown?
... but I was seeing later exceptions stating that the transaction had been aborted. But it took a while for it to come into focus.
But yeah, he was intenionally running code to throw a pgsql exception
I've solved it by simply doing one test per transaction, or a few tightly focused ones that don't expect to generate exceptions.
Cool, that seems like a good idea
Hey, I have a random question everybody: I’m trying to find a concise way to go from a map {:a true :b false :c false :d true}
to a seq of the just the keys whose values are true: (:a :d)
Not hard to do but I keep thinking I’m missing an obvious clojure-ism
(->> (vec m)
(filter (fn [[_ v]] v))
(map first))
Not exactly Shakespeare. It does seem like maybe there would be a standard library way of doing it.What I’ve got now is (map first (filter (fn [[k v]] (when v k)) m))
Pretty similar but I still feel like it could be simpler
Er, (map first (filter (fn [[_ v]] v) m))
I guess (keep (fn [[k v]] (when v k)) m)
works. I still hope to find a way to do it without destructuring inside of a (fn)
argument though
Oh hey, here's a real beginner question... in languages with infix notation, it's wrong to use "Yoda conditions", like saying if (1 == x)
. But in Clojure, it doesn't seem as obvious whether to say (= 1 x)
or (= x 1)
. Is there a consensus?
just for the record, i find yoda conditions convenient in C, since then you will not make a if (x = 1) mistake (that you'll get warned, but anyway
I like that best @manytrees, thanks!
Oh, actually: (keys (filter val m))
I’m a little surprised that keys can turn ([:a true] [:d true])
back into a map and then get the keys out of it though
It doesn't - keys works on a seqable of map entries