This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-06-15
Channels
- # admin-announcements (7)
- # alda (1)
- # aws-lambda (1)
- # beginners (12)
- # boot (20)
- # cider (59)
- # cljs-dev (4)
- # cljsrn (69)
- # clojure (232)
- # clojure-austin (3)
- # clojure-austria (1)
- # clojure-belgium (2)
- # clojure-canada (3)
- # clojure-dev (16)
- # clojure-greece (33)
- # clojure-nl (4)
- # clojure-quebec (12)
- # clojure-russia (12)
- # clojure-spec (27)
- # clojure-uk (38)
- # clojurescript (29)
- # community-development (7)
- # component (53)
- # core-async (16)
- # core-logic (1)
- # datascript (7)
- # datomic (11)
- # editors (7)
- # emacs (69)
- # hoplon (157)
- # keechma (1)
- # lambdaisland (2)
- # lein-figwheel (31)
- # leiningen (8)
- # mount (3)
- # off-topic (11)
- # om (23)
- # onyx (64)
- # planck (2)
- # re-frame (18)
- # reagent (21)
- # specter (118)
- # untangled (145)
- # yada (1)
I have some sets that look like this: #{ {:name "sam" :age 7} {:name "sarah" :age 23 :job firefighter} }, #{ {:name "sam" :hobby "softball"} {:name "tom" :hobby "sewing"} }
I want to merge these sets into a single set, so it would be #{ {:name "sam" :age 7 :hobby "softball"} {:name "sarah" :age 23 :job "firefighter"} {:name "tom" :hobby "sewing"} }
there is a function called "merge" in clojure.core that merges two maps, but what is the most efficient way to find the maps that should be merged within the sets above?
I've been thinking about this for a while, but I can't come up with a clean algorithm that isn't O(n^2)
@bcbradley: I am on mobile, but doesn’t a map e.g. {“sam” {:age 7} “sarah” {:age 23 :job “firefighter”}}
seem mergeable in linear time? I think you should be able to convert to and from this map in O(n) time as well.
I guess if names are unique then the set of relations could just be represented better as a set of functions from those names to the other stuffs (in other words a map, the way you suggest).
does anyone here know whether or not the function returned by juxt runs on multiple threads?
if do-copy
would be public interface, It will be handy for extending custom io factory.
@bcbradley juxt does not do threading, you can see the source here https://github.com/clojure/clojure/blob/clojure-1.8.0/src/clj/clojure/core.clj#L2467
what would be an easy way to get a build date/timestamp or git checkout hash into clojure source code ?
at runtime? you can always drop down to shell and use something like this (clojure.java.shell/sh "git" "rev-parse" "HEAD")
@lmergen: https://github.com/search?utf8=%E2%9C%93&q=rev-parse+filename%3Aproject.clj&type=Code&ref=searchresults
So, I'm learning about Records. is there a good definition of record or associated with record that will help me understand?
The book Clojure applied
covers records in a wonderful way. But don't se any connection to your code snippet and Records.
@escherize: there's no perfect parity between the keywords you can create with keyword
and the ones the reader will accept. This is a known issue. Some argue keyword
should be more strict, not sure if there's a final decision on that
I have some question about dependency injection in clojure. What kind of DI do you use? I know the follow: Inject by argument. Inject by map. Inject by protocol. Inject by context. There are others kind ? What do you usually use ?
I have some idea what is component lib, it is nice useful, etc ..
Both can be used for DI; flexibility's achieved in Component by letting you change the system map, and in Mount by letting you compose your app in your mount/start call.
But at raw level, what is the techniques do you use ? How to make functions more decoupled ?
My question is more about design than about tools ...
Pass in a function to another function. That's how map, reduce, filter and so on work. They only define a behavior (e.g. apply function to every item in a sequence, ...), the actual implementation's up to you.
@mbrum can you define your use of decoupled? What do two coupled functions look like?
Splitting up your functions and composing them afterwards also helps decoupling on a smaller scale. comp
and the threading operators ->
, ->>
and their relatives are your tools there.
ok, I will study both libs and re-think about my question .. 🙂 thanks guys ...
@meowy So you usually use di as arguments ?
but if you have so many dependencies, how do you deal with it ?
(defn bla dependency1 dependency2 dependency3 dependency4 coll)
is not it weird ?
It's kind of like closing over some dependencies: https://gist.github.com/Integralist/6ba8b3effc03aa47ab93
it is like a map of resource ?
Yeah, and you can attach functions that use that map, with args if you wish to do stuff
but it stop to have a referential transparency, correct ?
creating a kind of state into function, correct ?
or a miss something ?
@mbrum Make sure you are seeking values in your domain that compose your dependencies together to reduce the number of args.
e.g (defn person [first-name middle-name last-name address-line1 ...]) -> (defn person [name address])
(def map_of_dependencies {:dep1 fdep1}) ; (defn a [coll] ((:dep1 map_of_dependencies) coll))
it isn't a little bit strange ?
Also at the very boundaries of your app (where effects live) you shouldn't seek to provide convenience, you should prefer more explicit let binding, multiple calls etc
@escherize: yes, it is easy
but a little bit indirect 😕
by the way, you have to know the implementation ...
I understand , but in your experience, what is better for large projects ?
pass args or a map ?
hum ...
it do not guide you to a poor design ?
because you can "by pass" things ?
I mean, you can fetch things freely ...
well, the things you put into system are long-lived stateful things that are unlikely to change.
pass by destructuring is better.
but still is a kind to pass by args .. 😕
(defn a [arg1-dep coll] ...)
the function dependency ...
In fact, I don't know
(defprotocol Iadd (add [this] [a b]))
(extend-protocol Iadd
Object (add [a b]))
(defrecord Scalar [a b]
Iadd
(add [this] (+ a b)))
(def my-scalar (->Scalar 1 2))
my-scalar
;; => #teal.db.customer.Scalar{:a 1, :b 2}
(add my-scalar)
;; => 3
inject by protocol ?
it is a alternative too ...
I have to study much more ... 😛
Then you can fake the call like this:
(defrecord FakeScalar [a b]
Iadd
(add [this] 3)) ;;<- notice 3 not (+ a b)
(def fake-scalar (->Scalar 1 2))
(add fake-scalar)
I see that have some implicit ways to inject dependency ..
protocol, context, maps ... 😕
It look me weird ..
the explicit ways, pass by argument (with destructuring or not) appear me better, but if you have a lot of dependency it can be a problem ... 😕
is there a common pattern for this type of construct:
(if (:foo bar)
(:foo bar)
(something-else))
?If you don't have something to evaluate in the else branch you can use the not-found clause as well
As it will return the value at :foo even if it is null/false. (e.g it does a presence check rather than a value truthyness check)
Just make sure you understand the difference in semantics before replacing (if x x y) calls with anything other than (or x y) 🙂
are there any clojure peeps up for helping me with an interesting project? OSS 👼
Is there a way to use s/multi-spec
multimethods with unqualified keywords? A multi-spec-un
, or something?
Anyone here has some experience with Automat, particularly advice on this issue: https://github.com/ztellman/automat/issues/24 ?
I’m trying to consolidate the usage of !
in my library’s function names, and I’m finding it difficult to find a coherent stance on the idiom. Does anyone have a good resource?
specifically I have functions that create something external (e.g. a rabbitMQ queue) so I’ve named them create-queue
but should it be create-queue!
?
The clojure.core usage seems to be 'something that is unsafe in a transaction (not pure/retry/rollback aware)' e.g swap!
vs send
Many side effects would fall in to the 'unsafe' camp, so typically impure functions will have a '!' on the end
so if create-queue
fails when called twice in a row, or has some other nefarious effect, then the !
is warranted?
I always say "if unsure use a '!'". I like libraries that somehow mark these potentially dangerous fns. Honestly I don't use the STM much so its more about just seeing what is likely to harbour a potentially dangerous side effect.
that’s interesting. So if I have a function that modifies an atom by assoc
-ing it, that’s idempotent, so it doesn’t need the bang?
I would need to think about it some more, but even idempotent actions given retries could potentially alter the possible histories of a set of concurrent threads. I've confused myself now 🙂
Ok I've thought about it, idempotency is not enough. Consider a rollback in the STM, caused by an exception. your swap! would still have happened. That is what is meant by 'unsafe in a transaction' - send
is not like this, because sends are guaranteed to happen only on successful completion of (and as part of) the transaction.
From a concurrency standpoint, other threads could be affected by a transaction that does not get committed, or is not yet committed. Which alters potential histories and could cause some really weird bugs
I don't think clojure.core is entirely consistent with this convention - Otherwise of course you could argue about def
!
I’m essentially wrapping java methods that operate on a java class that’s not thread safe, the onus is on the developer to manage concurrent access. So maybe then if you call a method like create-queue
on that object multiple times you don’t run the risk of another thread calling delete-queue
unbeknownst to you
create-queue is unsafe in a transaction. its a matter of whether you want to follow the convention or not. I understand the community as a whole doesn't follow this convention a lot of the time anyway. My impression is that its not widely advertised as a 'thing you must do' in clojure. I have my own set of naming rules, other people have theirs.
sounds like the important part is superimposing safety on the stuff you're calling, not the naming
You can always have create-queue
dispatch its effect via an agent if you want to keep the name and adhere to the convention 🙂
It's not entirely obvious from the docs, but am I correct in thinking that s/def
in clojure.spec
can register keywords from non-existing namespaces? Would it be an anti-pattern to use something like (s/def :foo.bar.api/id)
in order to distinguish it from (s/def :foo.bar/id)
without creating said namespace?
Is it then just a question of making sure namespaces are loaded in an order to be able to resolve that in the registry correctly?
Keyword namespaces don't have to exist as a Clojure namespace
So feel free to make good namespaces independent from your code (but be aware that affects your ability to use aliases)
At least for now
As a heads up, re: keyword namespaces, I’ve added a :qualifier
option to clojure.java.jdbc
to make it easy to produce result sets with namespaced keywords — will be in 0.6.2. Since namespaced keywords are going to be "the thing" everywhere soon… (jdbc/get-by-id db-spec :user 42 {:qualifier "user"})
=> {:user/id 42 :user/name "Sean" …}
Don't leave us in suspense @alexmiller !
Feedback on that ^ welcome, before it gets baked into a release.
I'm considering moving #C0702A7SB's non-ns keywords to ns ones.
@seancorfield: a symbol qualifier rather than a string would make sure that it follows the naming rules for a ns
How does one most efficiently run a shell command as a separate process and give it some kind of stringIO type thing to stick its output in?
@seylerius: I don't know if it's the "most efficient" but we just used the ProcessBuilder api directly:
(defn run-command-stream-output
"Runs a shell command and streams the output.
An exception is thrown if the exist status is non-zero."
[command]
(let [process (.start (ProcessBuilder. (into-array String command)))]
(future (io/copy (.getInputStream process) (System/out)))
(future (io/copy (.getErrorStream process) (System/err)))
(if (not= (.waitFor process) 0)
(throw (Exception. (str "Failed command '" (clojure.string/join " " command) "'"))))))
When using (clojure.spec/fdef my-macro ...)
, any uses of the macro (at least during testing0 automatically include a validation of the macro arguments. How come this isn't the case when using fdef
with functions?
@jannis: I believe performance. macros happen at macroexpansion time when performance is generally not a concern. functions get called in production. You can turn that validation on for all the functions in an ns using instrument-ns
@bfabry: Ah, performance... that's a valid concern. I just found out about instrument
while you replied. instrument-ns
sounds useful too. Cool, so that way I can either wrap a function or a namespace to activate validation.
What's the most elegant way to write simple automated, generated, random tests for functions with clojure.spec
? (deftest my-func-conforms-to-its-spec (is (some-fspec my-func)))
?
One problem I see with that approach is that it doesn't report anything useful if it fails, since all a function spec created with fspec
returns is true
or false
.
@jannis I believe the clojure.spec.test/check-fn fn is what you're looking for http://clojure.github.io/clojure/branch-master/clojure.spec-api.html#clojure.spec.test
@malcolmsparks: we're considering allowing aliases for non-existent namespaces
@jannis yeah, check the stuff in clojure.spec.test for more
(is (nil? (s/explain my-fun-spec my-fun))
might've worked as a hack as well but I'll check out clojure.spec.test
well s/valid?
prob more useful there
With check-fn
and check-var
, is there a way to get a "nice" explanation of the failures, like explain
produces?
Hmm, this convo probably belongs in #clojure-spec ...
@alexmiller: what would help would be some kind of tag literal that declares the default ns for any non-ns keyword keys in the following map literal. Well, that's one possible solution to the problem I'm facing: how to move to ns-keywords without giving users RSI.
@malcolmsparks: There’s a JIRA ticket for namespaced maps… let me dig that up...
@seancorfield: thanks. Would like to know what the tradeoffs are
Targeted for 1.9 if the open questions can all be resolved.
That would be terrific.
1910 will be added for 1.9. Also CLJ-1919 for namespaced key destructuring.
guys, any way to improve this?
I would like to, if possible, define the random number generator inside of random-double
well, I could define random-double as a variable that returns a function
ok, problem solved
but then I can't have operator overloading
@jorgedfbranco: I've made this wrapper around java math RNGs https://gist.github.com/tsulej/34a230f9d3bbf89fd618901f19b14723
The minor problem I need to fix now is that the timeout channel closes after an elapsed period of time, not at a specific point in time
Which is fine for e.g. cancel an operation if it takes too long scenarios, but not great for send a signal at a specific point in time scenarios
@jorgedfbranco: e.g.
(import java.util.Random)
(def random-double
(let [random (Random.)]
(fn
([] (.nextDouble random))
([min max] (+ min (* (- max min) (random-double)))))))
Whoops, wrong room, sorry for that non sequitur
@noonian: oh, thats cool