This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-01-16
Channels
- # beginners (115)
- # boot (13)
- # boot-dev (13)
- # chestnut (1)
- # cider (1)
- # clara (10)
- # cljs-dev (21)
- # cljsjs (1)
- # cljsrn (2)
- # clojure (120)
- # clojure-dusseldorf (2)
- # clojure-greece (13)
- # clojure-ireland (1)
- # clojure-italy (3)
- # clojure-nlp (3)
- # clojure-russia (2)
- # clojure-spec (13)
- # clojure-uk (82)
- # clojured (5)
- # clojurescript (47)
- # core-async (2)
- # core-logic (8)
- # cursive (45)
- # datomic (2)
- # editors (1)
- # emacs (39)
- # fulcro (166)
- # graphql (1)
- # hoplon (16)
- # keechma (5)
- # off-topic (202)
- # perun (4)
- # protorepl (7)
- # re-frame (28)
- # reagent (13)
- # ring (27)
- # ring-swagger (16)
- # rum (1)
- # shadow-cljs (25)
- # spacemacs (20)
- # sql (141)
- # yada (4)
https://kotka.de/blog/2010/02/gen-class_how_it_works_and_how_to_use_it.html This example shows the name, init, post-init as names
but when I declare them as string I get this error Exception in thread "main" java.lang.IllegalArgumentException: Wrong number of args passed to keyword: :gen-class
if you are newish to clojure(which evidence suggests) I would strongly recommend you stay as far away from gen-class in all its guises as possible
(and also take advantage of the folks in #beginners who have opted in to help answer new-to-Clojure people's questions @rnagpal!)
I have a vector of int/strings and I want to calculate a positive hash of it. Are better options than (Math/abs (hash [ids]))
?
just a thought - hash returns an int, if you are OK with a long, you could do a bit-or of the raw bottom 16 bits of the int with long 0 and get a 64 bit number guaranteed positive
try #(bit-and (hash %) (dec (bit-shift-left 1 32)))
if you prefer, 0xffffffff
is the same as (dec (bit-shift-left 1 32))
So I'm trying to turn arbitrary data maps into json strings. And that's usually pretty simple, but cheshire/generate-string
will sometimes fail when given things like regex patterns. Is there someway to tell cheshire to just get over it and call to string on anything it doesn't recognize?
where it just does (try ((encoder-lookup thing) thing) (catch Exception e (encode-str thing))
I believe Math/abs also works on floats, and I'm not sure you can take abs of a float by just bit shifting.
also does anyone know what the proper classname for functions would be? I want to tell cheshire to use generate-str for functions. but they all seem to be generated classnames, e.g. ring.middleware.multipart_params$wrap_multipart_params$fn__19512
@tjtolton: why not use https://clojure.github.io/clojure/clojure.walk-api.html and pre convert all "should be converted to string" things first ?
this is for all log statements. which means 2 things -- a) I don't know what all the things that "should be strings" are in advance, and b) performance matters so I don't want to do case analysis on everything in the tree.
anything that does clj -> json is going go have to 'walk the tree' anyway, there's no magical "clj -> json" function out there
cheshire uses some valuable performance tricks, and I'd certainly prefer not to walk the tree twice?
what is the standard way/work-around in clojure to propagate exception via a promise?
assuming I do not want to use a sentinel in the returned value nor a future
for that?
@tjtolton I don't know how Cheshire and pr-str compare, but Cheshire isn't even that fast. It uses definline
(which prevent JVM inlining) and dynamic vars.
@tjtolton Maybe you could try add-encoder Object
, and see if that is used if other encoders don't match
I think so, it seems to be implemented using protocols (it extends the given class with JSONable protocol)
And for protocols the first implementation is used
floats/doubles can NOT represent all integers, at some point, it starts "skipping values"; so if we have a float/double that we repeatedly call +1 on, does JVM/JS say what happens when we reach a "skip number" ? (does it always increases, always stay the same, or is behaviour undefined)
@qqq https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger
thanks @juhoteperi works like a charm
Well to answer your question: No nothing happens when do you do double/float math and enter this range
`(= (Math/pow 2.0 54)
(+ 1 (Math/pow 2.0 54)))` evals to true
-- you're right -- and this does answer my questino; thanks!
what the cljc way to generate incrementing' ids? (I'm not looking for probabilistic uuid, but something that is 'counterish', but perhaps string or vector backed instead of long/float based)
If I want to pr-str
an arbitrary nested data structure containing lazy seqs, what’s the best way to do this?
basically I want this to work without changing map to mapv, and by using some function that ensures all (potentially nested) data is fully realized before being printed
(->> (range 30)
(map #(do (println %) %))
(pr-str))
are you also against using https://clojure.github.io/clojure/clojure.walk-api.html
@martinklepsch i think laziness is a property of the collection type (sequence vs vector)
@qqq yeah walking is the option that also came to mind but I don’t know, this seems harder than it should be. 🙂
I guess there's really no function in Clojure core that does (awesome-fn :id [{:id 1 :last-name "Doe" :first-name "John"} {:id 2 :last-name "Doe" :first-name "Jane"}])
=> {1 {:id 1, :last-name "Doe", :first-name "John"}, 2 {:id 2, :last-name "Doe", :first-name "Jane"}}
? I know about set/index
and group-by
, but I'd like the val to be a single value instead of a sequence of values.
that will clobber duplicate id keys. if you have the possibility of multiple id keys you can just use `group-by :id ...~
hi, is there a way to compile a single source file to a .class file and run it just like it were compiled with java java file.class
(and not java -cp target/classes:clojure-1.8.0.jar file.core
) ?
You can make an executable jar file.
i know, but there is a tool that just accepts plain .class files, that's why I started wondering
Hi, guys, how could I write a Java 8 lambda function like in
stream.filter((key, value) -> value > 0);
with Clojure? Should I reify something or the Clojure 1.9.0 function implementation just works?Ah, well clojure does compile down to .class files, but it's like one .class for every function, not sure how useful that would be in this case.
@pfeodrippe you need to use reify
with the appropriate functional interface: https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
@schmee Thanks, man o/
Wondering if transducers can make the typical ->>
transformations more efficient, without drastically reshaping them / making me think
i.e., given code in the shape (->> [1 2 3] (map inc) (map str))
, is there always a transducer equivalent that will avoid intermediate collections?
that transducer equivalent being machine-writable, e.g. I can just have an emacs keybinding
and they are not the same - there are cases (like mapcat) where you could stumble into drastically different behavior
So seems that the easiest way to make sure all nested seqs are realized is (clojure.walk/prewalk identity some-data)
Some one use Quasar\Pulsar in clojure or it outdate?
What's the best way to create a worker threadpool?
Loom maybe?
@boogie666 how about this one? https://github.com/bugra/knn
I'm trying to create a portable version of ns-interns
: the Clojure version takes a namespace, and the ClojureScript version takes a quoted symbol. My thought is to create something like
(defn ns-interns* [ns-sym]
(ns-interns #?(:clj (find-ns ns-sym)
:cljs ns-sym)))
And called like (ns-interns* 'com.example.ns)
. The Clojure version works fine. I can't figure out how to quote the value of ns-sym (which is a symbol) for the ClojureScript version.(quote ns-sym)
of course returns 'ns-sym
, rather than 'com.example.ns
. I'm open to changing the argument type of ns-interns*
.
@grzm clojure ns-interns accepts a symbol
penguin.tracking=> (ns-interns 'user)
{clojuredocs #'user/clojuredocs, help #'user/help, le #'user/le, find-name #'user/find-name, non-macros #'user/non-macros, lf #'user/lf, src-dir #'user/src-dir, with-use #'user/with-use, user.proxy$java.lang.Object$SignalHandler$d8c00ec7 #'user/user.proxy$java.lang.Object$SignalHandler$d8c00ec7, l #'user/l, all-debugged #'user/all-debugged, cdoc #'user/cdoc, exercise-coverage #'user/exercise-coverage, ll #'user/ll, set-debug #'user/set-debug, unset-debug #'user/unset-debug, apropos-better #'user/apropos-better}
@grzm if what you have is a symbol, you don’t need to quote it, quoting is an operation in the reader, to ensure you get the form itself (in this case a symbol) instead of the evaluated result of the form (which would be something in scope with that binding, or an error)
if what you have is a symbol, you’re done, no special operation required at that point
so you are not going to be able to invoke ns-interns on a symbol not known until runtime (a symbol passed in as a function argument)
@noisesmith thanks. That saves me the find-ns
call.
@itaied What do you mean by "test the specs"?
I usually try to have specs that I can s/exercise
which means they need to generate -- which isn't always possible (or sometimes is just a lot of work). Depending on exactly what I'm writing a spec for, I'll sometimes run a few sample s/conform
calls via the REPL. But in some ways, the specs are the tests -- so it's sort of like asking "how do you test your tests?"...
Some things that come to my mind are "is the email regex works correctly?"
"Does the ::name
in the user
map is required?" etc...
And yes it does actually sound like testing your tests,
I just feel like writing just the specs leave me vulnerable for mistakes that may happen in future changes
That's true of any system you use to specify the expected behavior of your code tho'... unit tests, specs...
Even if you used formal methods to specify your system, you still have the question "What if I made a mistake in my formal definition?"
I don't see how writing specs makes you any more vulnerable than writing unit tests...?
I think an important part of any verification system you use is integrating information from runtime regressions - so if there’s a bug in production that slipped past a test (or caused by a test being misguided), the resolution should include verifying the system now handles that case correctly
your system is turing complete, it’s about making it as useful as possible, because there will always be some case that sneaks through
@boogie666 you might check nd4j. e.g. https://gist.github.com/MikeDepies/679540cfbace153d4358
@seancorfield So do you test your predicates or use generative testing? I can't really find a way to test the specs in any other way other than unit tests... As for functions that uses the specs I understand generative testing, it's just the docs doesn't mention verifying the specs themselves
my M.O. is to find an example of data the spec should reject, and write an example based test that verifies the test rejects that input
(in a unit test, that is)
(I’m using prismatic/schema at the moment and not spec, but that’s how I’d do it)
@itaied I work with a live REPL all the time, so I'm constantly evaluating code as I write it. I tend to use (comment ...)
forms containing scratch expressions I write along the way to verify behavior of actual production or test code, and leave them in the code if they provide a useful crumb trail of how I arrived at my final solution.
So as I develop a spec, I tend to be evaluating forms using that spec as a "live" sanity check: maybe a s/conform
call, maybe a s/exercise
call, maybe just some notes in code form.
That work in the live REPL is not "unit testing" but code that I've evaluated might become unit tests at some point (or might become production code). It's a flow, a process. There's no real distinction into "phases" which is what your question seems to imply.
i opened this ticket in october, apparently it was a stumper. anyone else tried to get cloverage to cope with dynamic vars and reader literals? https://github.com/cloverage/cloverage/issues/197
TL;DR: this causes cloverage to die: (clojure.edn/read-string {:readers data-readers})
i feel like i might be doing something obviously wrong here tho
my guess is cloverage is intercepting code reading (so it can instrument code), and is calling tools.reader without passing in *data-readers*
https://github.com/cloverage/cloverage/blob/master/cloverage/src/cloverage/instrument.clj#L411
makes sense. any ideas how ot hack around it?
i'm guessing either nobody uses cloverage, or nobody uses datomic, or nobody uses cloverage and datomic together