This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-04-07
Channels
- # beginners (41)
- # boot (38)
- # cider (17)
- # cljs-dev (52)
- # cljsjs (3)
- # clojure (200)
- # clojure-italy (8)
- # clojure-russia (50)
- # clojure-spec (28)
- # clojure-uk (45)
- # clojurescript (28)
- # core-async (9)
- # core-matrix (2)
- # cursive (16)
- # datascript (15)
- # datomic (50)
- # dirac (5)
- # emacs (20)
- # figwheel (8)
- # flambo (2)
- # hoplon (10)
- # incanter (1)
- # jobs (1)
- # leiningen (2)
- # lumo (26)
- # mount (171)
- # off-topic (22)
- # om (54)
- # onyx (2)
- # pedestal (27)
- # re-frame (10)
- # reagent (12)
- # ring (27)
- # ring-swagger (3)
- # rum (2)
- # slack-help (1)
- # spacemacs (134)
- # specter (6)
- # sql (15)
- # testing (20)
- # uncomplicate (5)
- # unrepl (49)
- # untangled (9)
- # yada (29)
@st3fan You can also set defaults
(defn string->integer [s & {:keys [base] :or {base 10}}]
(Integer/parseInt s base))
Though I'd say taking a map is probably more idiomatic, as it let's you continue to use overloaded function arity.
Like
(defn string->integer [s {:keys [base] :or {base 10}}]
(Integer/parseInt s base))
instead. Useful if you needed arity overloading.@qqq Slingshot? (but I'd suggest trying @xiongtx suggestion first -- before you stray too far off the reservation 🙂 )
I believe Slingshot obviates exceptions altogether and lets you just "throw" arbitrary data? I'd have to go read the docs to be sure tho'...
I know it's pulled in by several projects I depend on but I've never used it directly!
I have a question, I have protocol and record and can call protocol functions on that record like so
(defprotocol IProtocol
(foo [_]))
(defrecord Record []
IProtocol
(foo [_]
(println "hi from Record foo!")))
(def record (->Record []))
(foo record) ;; hi from record foo!
(defn get-fn-ptr [protocol record-type key-word-fn]
;; What do I put here?
;; I can see func
)
(def xxx (get-fn-ptr IProtocol Record :foo ))
(xxx record) ;; calls foo for record
And my clojure fu is poor, looking the clojure source on github isn’t elucidating me much 🙂
I want to generate state entry / exit funcs based on the methods supported by a record or reified object
would call on-game-start and exit-game-start if the functions were found at bind time
I need a doseq
to keep some state around. Is there soething more efficient than an atom
? It's thread local, so I'm willing to use weird hacks.
thheller: is this faster than https://clojure.org/reference/vars ?
Changes to references made volatile are always "written through" CPU caches all the way to main memory (which is somewhat expensive), this means changes are guaranteed to propagate to other threads (nescessary in stateful transducers).
seems to suggest they are expensiveit seems like volatile has to write out to ram, whreas per thread things don't need to do that
in my limited understanding of computer arthiceture, it seems like bindings would be faster than volaltiles
to me, it seems like 'volaltile` here doens't mean fast -- it means "oh shit, another thread can change this behind my back"
not sure what kind of performance you are going for here, if it matters that much use java (or just loop/recur)
Hi folks, I've posted this question on #architecture but didn't got an answer, so I'll ask here: what libraries have you been using for PubSub/Presence? Also, thoughts on Pulsar? I’ve an Erlang/Elixir background so I’m trying to find similar tools. 🙂
I have the same issue I'm struggling with. I've been using vert.x, I've been thinking about Pulsar but never used it so far and I'm experimenting with 0MQ and RabbitMQ (written in Erlang, which is basically all about messaging).
My current setup (rewriting some stuff): it depends on your level of abstraction 🙂. Between deployables, I use 0MQ or RabbitMQ, inside deployables, on the JVM, I tend to use vert.x, although just using netty may be ok as well. Inside 'components' (parts of deployables) written in clj, core.async
, although I'm moving towards manifold
from time to time.
Rationale: high level abstraction: as tech independent as possible. low level abstraction: deployables and certainly components thereof have to be able to choose their internals; the lower the abstraction, the less 'generic' the lib can be. (less generic usually being faster/more performant)
I already have:
(s/def ::entity (s/map-of keyword? any?))
how do I add the additinoal constraint that
:db/eid must be an integer AND
:db/table must be a keyword ?How do I easily get indicies with for
? Best thing I have so far is
(for [[i v] (map vector (range) ['a 'b 'c])]
...)
I probably understand the difference between doing list comprehension and mapping over a list?
Yes, that’s a good case for for
. I don’t know, I just think for is really easy to use in the context of Reagent
yeah looping was not the right term, it only make sense if you do something with the return value obviously
since for
is a looping construct in most every language, people already come to clojure misusing it, not realizing it is for list comprehension, so my mission in life is to dispel the notion that for
is for looping in clojure 😉 (j/k about the mission in life part)
@grav if you are using for
to create nested structures, you can still do that, but use the appropriate map-indexed
to create the indexed structure, right?
(for [[i v] (map-indexed #(vector %1 %2) '[a b c])
sub-v '[x y z]]
[i [v sub-v]])
#(vector %1 %2)
is vector
@joshjones Yes, that’s kind of what I’m doing now, though I’m doing (map vector (range) ['a 'b 'c])
instead of (map-indexed ...
In my case, I’m creating nested data structures, so no worries 😉 I really like for
for this purpose, it’s very clojuresce imho. But there should be a for-indexed
🙂
Hi all, I noticed clj-webdriver is no longer maintained as of Dec 2016. Anyone have a suggestion to succeed it?
I’ve found these two: https://github.com/jackrusher/sparkledriver https://github.com/tmarble/webica
@nfisher I’ve been using https://github.com/igrishaev/etaoin
It’s not a Selenium wrapper, so it might not be exactly what you’re looking for, but if you need to drive a browser it is nails
@nwjsmith thanks! Will take a look. I was using clj-webdriver, but it means I’m pinned to an old version of FF (47) and selenium(2.x). 😕
I have project with integration tests that I would like to run locally as well as in other environments. When I put them in /test
(which intuitively makes sense), they aren't added to the JAR so I can't run them in other environments (UAT, staging, etc). What is the best place for these?
you could make another jar which when run runs the integration tests
have another project essentially
I have (defn myfn [] (shouldnt-be-called))
and I want to stub it out with with-redefs
, like this:
(with-redefs [myfn (constantly nil)] ...)
but when myfn
is called, it doesn't return nil
but instead tries to call the original shoudnt-be-called
am I doing something wrong?I've found I can successfully stub out shouldnt-be-called
, but I don't think I should need to stub things down stream from myfn
somethings do cross namespaces, is that what you mean?
and shouldnt-be-called
comes from a def
d var
if you do a lazy sequence with (map myfn ....)
, and then redef myfn, the lazy sequence will use the old definition
(lots of gotcha with with-redefs -- it's not a recommended function, but an occassionally necessary evil -- lots of previous discussion on this)
hrm... thanks for helping me get out of this earlier!
i thought i was going crazy!
i just need it for testing, but, perhaps i need to find another way
myfn
lives in (def thing {:myfn myfn})
in another ns than the one getting tested...
oh, that totally makes sense I think. not obviuos, but makes sense now 🙂
hi guys, quick question: why (= 1024 1024.0) returns false and (== 1024 1024.0) returns true, I thought that = compares values
I’m new to clojure but if it work like it does in JS then the ==
compares types too
it does not, those are two different types (Long vs Double)
so it's kinda the opposite of JS 🙂
interestingly, the docs both say each are type-independent, but the example for = shows (= 1 1.0) => false
https://clojuredocs.org/clojure.core/=
https://clojuredocs.org/clojure.core/==
exactly!
and both functions were added to Clojure 1.0 so it's not like they introduced == later and the docs are out of date
There is a difference between "=" and "==". For primitives you definitely want to use "==" as "=" will result in a cast to the wrapped types for it's arguments. This may not be the case come Clojure 1.3 (see [1]) [1] http://github.com/clojure/clojure/commit/df8c65a286e90e93972bb69392bc106128427dde
'== is defined only for numbers, where '= is general equality. The example showing (== :foo) as true is a bit misleading because (== :foo :foo) produces an exception. Unary == always returns true as an optimization.
Just pasting the notes from : https://clojuredocs.org/clojure.core/==
ok, thanks guys, that gives me some clarification
I gotta start using http://clojuredocs.org more, the samples and notes are great source of knowledge 🙂
For your case @josh.freckleton , better to parameterize your side effect:
(defn myfn [impl] (... use the impl))
rather than always assuming the impl is shouldnt-be-called
-- this usually requires a few refactorings in a codebaseAnother quick hack that you can do is: (def thing {:myfn #'myfn})
but I recommend parameterizing 100 million times more
thanks @ghadi , I couldn't have solve that otherwise!
@ghadi, so #'
solved it easily, why do you recommend against it?
am I confused about it? the docs for = state that "Equality. Returns true if x equals y, false if not. Same as Java x.equals(y) except it also works for nil, and compares numbers and collections in a type-independent manner."
because the reason for #'
here is to make the with-redefs work - there are better ways to mock things in Clojure (like making the implementation a passed in argument)
@piotr.owsiak I don't think you are confused. That's precisely what the doc says.
or at least the first half. > Clojure's immutable data structures define equals() (and thus =) as a value, not an identity, comparison
= also says > Same as Java x.equals(y) except it also works for nil, and compares numbers and collections in a type-independent manner
really?! that's confusing as hell 😄
instead of : > Same as Java x.equals(y) except it also works for nil, and compares numbers and collections in a type-independent manner. should say: > Same as Java x.equals(y) except it also works for nil, and compares numbers and (collections in a type-independent manner).
it compares numbers type-independently as long as both numbers agree on how lossy they are - so doubles/floats or bytes/ints/longs/bigints but nothing is equal to bigdecimals except other bigdecimals
@noisesmith thx for detailed explanation
i was being snarky because he asked if he was confused and just pasted the doc string 🙂 (without stating what the potential confusion could be)
ok guys, thanks for clarification
Hello guys, can you give me some link of moder best practice in clojure/clojurescript. thanks
Without manually doing lots of transient/persistent management away, is there an easy way to: (1) do a bunch of assoc-in update-in on transients, then make the whole thing persistent ?
qqq: check if the functions you use use transient themselfves. For example into already does it.
I know it's all personal preference generally speaking, but say I have a function whose purpose is to update/populate state in an atom and this function only runs once and on app startup. What would be an idiomatic return for that function? Is there a good design pattern for such a thing? Returning a reference to the atom in question? Returning true
? Leaving it as a nil
return?
We like to end the names of functions like that with a bang !
, and have them return a keyword, e.g., :done-building-indexes
. --- returning a keyword instead of, say, the contents of the entire index, tends to be friendlier on one's repl during development.
Agreed. Would you say that it is idiomatic for the returned keyword to be more informational like :done-building-indexes
for the sake of the repl, or would you find yourself ever using that keyword for something? Again, obviously this is all relative and personal preference, I'm just trying to understand what "clean" or idiomatic Clojure would look like in the generic case.
I can't say too much for idiomatic, our team is perhaps a bit insular 😉, I can't think of a case where the actual value returned from a function like that was ever used. I guess I try to keep those keywords unique/grep'able in case they do end up in some log somewhere, but yeah, something friendly for development more than useful-as-a-value.
@mbcev ideally I write functions that update an atom such that they have no atom related logic in them - I write the function to take a foo, and return a foo, and then use the function as an argument to swap! on an atom containing a foo
another way to say that is that I don't write functions that update atoms (ideally at least) and just write arguments to swap!
@noisesmith --- now that sounds idiomatic. ha! What does your function that calls swap!
return? I think that's the question at hand...
your function to swap! must return the new value to put in the atom
that's the only correct result from such a function
oh, it should return the new value
the reason I called it is to swap! on something, so the reasonable return is the resulting data
right
I find that with realistic data I end up blowing up my repls when I do that. 😉 But, then we're back to personal preference/tools. 😄
your repl won't blow up if you stop using your editor as a repl btw
it might take a few moments to print, but not much
sorry, that's another topic
"big data is when your application data is large enough to stall your debug console"
I have definitely had the case where my repl went unusable a bit because I looked at the data coming from kafka, for example
but I always had a usable repl again after a control-c or just waiting
Thanks @harold and @noisesmith
to avoid blowing up your repl you can always set *print-level*
and *print-length*
in the repl profile
That's a good idea, I had that for a while, but then I copy/pasted some edn for a colleague and data was lost. 😐
clj-kafka has worked OK for me, haven't tried kafka-clj
a lot of my code is using direct interop with kafka itself though