This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-09-23
Channels
- # announcements (2)
- # beginners (150)
- # calva (59)
- # cider (10)
- # clj-kondo (63)
- # cljs-dev (15)
- # cljsrn (34)
- # clojure (127)
- # clojure-dev (16)
- # clojure-europe (12)
- # clojure-italy (18)
- # clojure-nl (6)
- # clojure-russia (3)
- # clojure-uk (23)
- # clojurescript (18)
- # clojutre (15)
- # clr (2)
- # cursive (3)
- # datomic (26)
- # duct (6)
- # fulcro (9)
- # graalvm (3)
- # joker (9)
- # lambdaisland (1)
- # off-topic (12)
- # om (1)
- # pathom (29)
- # re-frame (16)
- # reagent (2)
- # shadow-cljs (57)
- # slack-help (2)
- # spacemacs (45)
- # sql (20)
- # xtdb (3)
- # yada (3)
I have map of lists and would turn it into a list of maps eg.:
{:a (1 2) :b (2 3)} -> ({:a 1 :b 2} {:a 2 :b 3})
. How could I do this? Or better a list of lists to a list of maps with a given set of keys:
((1 2) (1 2)) (:a :b) -> ({:a 1 :b 2} {:a 2 :b 3})
Assuming you wanted to write ((1 2) (2 3)) ...
in the last code snippet I'd do something like this:
(let [my-keys [:a :b]
my-values [[1 2] [2 3]]]
(map (fn [values]
(zipmap my-keys values))
my-values))
Thank you.
Did you mean for your second example to be more like this?
((1 2) (3 4)) (:a :b) -> ({:a 1 :b 2} {:a 3 :b 4})
yes your right. @U06BE1L6T already has a solution.
can I big up an amazing library here? I don’t see it being mentioned much, but it’s become an essential part of our toolset: https://github.com/alexanderjamesking/spy
This test only evaluates the first println first at the first execution of the test:
(deftest foo (is (= (println 1) (println 2) (println 3) (println 4) (println 5))))
on clojure 1.10.1
$ clj
Clojure 1.10.1
user=> (require '[clojure.test :refer :all])
nil
user=> (deftest foo (is (= (println 1) (println 2) (println 3) (println 4) (println 5))))
#'user/foo
user=> (foo)
1
2
3
4
5
nil
user=> (foo)
1
2
3
4
5
nil
Is that what you see?
(foo)
2
3
4
5
1
there's a testing plugin that adds an assert-expr
for =
that makes it ensure it has multiple forms. forgot what its called
@alexmiller Ok yes it works from cli
@ghadi cider
@jsabeaudry781 can you be more specific? lol I just noticed Calva doesn't do printlns at all when using the run test harness, but has the same output when I evaluate directly.
I’m digging in my plugins, I have under 10 should be pretty quick
sequential when running cider's testing commands and invoking the test directly in the repl (under nrepl)
Is there a way to list enabled plugins?
lein-pprint is made for this use case https://github.com/technomancy/leiningen/tree/master/lein-pprint
ironically it's another plugin
I think I removed everything but cider-nrepl but I’d like to confirm I didn’t forget some place
Calva (so cider-nrepl
):
REPL connected.
user=> (clojure-version)
"1.10.1"
user=> (require '[clojure.test :refer :all])
nil
user=> (deftest foo (is (= (println 1) (println 2) (println 3) (println 4) (println 5))))
#'user/foo
user=> (foo)
1
2
3
4
5
nil
user=> (foo)
1
2
3
4
5
nil
user=>
@pez odd...I had @jsabeaudry781’s output one time, then another time after jack-in I'm getting the same 🙀
@jsabeaudry781 did you find the culprit?
(-->
id "28"
op "info"
session "fe90d4ec-0faa-4e6c-a5a1-f4ca347efd3f"
time-stamp "2019-09-23 14:18:37.911936000"
ns "foo"
symbol "foo/foo2"
)
(<--
id "28"
session "fe90d4ec-0faa-4e6c-a5a1-f4ca347efd3f"
time-stamp "2019-09-23 14:18:38.508446000"
column 1
file "..."
line 7
name "foo2"
ns "foo"
status ("done")
)
(-->
id "29"
op "test"
session "fe90d4ec-0faa-4e6c-a5a1-f4ca347efd3f"
time-stamp "2019-09-23 14:18:38.528316000"
load? "true"
ns "foo"
tests ("foo2")
)
(<--
id "24"
session "fe90d4ec-0faa-4e6c-a5a1-f4ca347efd3f"
time-stamp "2019-09-23 14:18:38.681178000"
out "2
"
)
(<--
id "24"
session "fe90d4ec-0faa-4e6c-a5a1-f4ca347efd3f"
time-stamp "2019-09-23 14:18:38.686466000"
out "3
"
)
(<--
id "24"
session "fe90d4ec-0faa-4e6c-a5a1-f4ca347efd3f"
time-stamp "2019-09-23 14:18:38.691242000"
out "4
"
)
(<--
id "24"
session "fe90d4ec-0faa-4e6c-a5a1-f4ca347efd3f"
time-stamp "2019-09-23 14:18:38.695911000"
out "5
"
)
(<--
id "24"
session "fe90d4ec-0faa-4e6c-a5a1-f4ca347efd3f"
time-stamp "2019-09-23 14:18:38.700735000"
out "1
"
)
(<--
id "29"
session "fe90d4ec-0faa-4e6c-a5a1-f4ca347efd3f"
time-stamp "2019-09-23 14:18:38.705696000"
gen-input nil
results (dict ...)
summary (dict ...)
testing-ns "foo"
looks legit
What gives you this impression?
timestamps match the order
the 1 message is sent last, so if you assume that code is being executed in the correct order, then the clear indication is the messages are in the incorrect order
I haven't really been following but I still think it is super likely some plugin, either nrepl middleware, or to clojure.test extensions is screwing with things
yup this is most likely https://clojurians.slack.com/archives/C03S1KBA2/p1569256179351500
(deftest foo1
(let [a (atom 0)]
(is (= (swap! a inc) (swap! a inc) (swap! a inc)))))
just showing that execution is incorrect confirming that some extensions is screwing with something
if you change =
for >
then you see order is correct
the is macro has that multimethod extension thing based on the first of its argument
(get-method assert-exp '=)
might help you figure out which library is screwing with it
alternatively if you use the fully namespaced form, clojure.core/=
it will bypass the extension for =
, unless someone also extend assert-expr for clojure.core/=
, which I dunno, if someone was careless enough to break evaluation order I doubt that were careful enough to catch namespaced symbols
I think it might also be cider’s handle-retest-op
because it only happens from 2nd execution
Great find,
I’ll work on a patch for cider
Let me know if you see possible improvements
So nREPL is delivering stdout in the wrong order
how about (every? zero? [a b])
the = 0 version fails on 0.0 (might not matter in your case)
there's (== 0 a b)
which addresses that though
huh, there is an exact integer representation of zero in floating point… ah okay
yeah, = is never true between float and fixed
== works as expected for -0.0 as well
There are exact integer representations of all integers from about -2^53 to +2^53 in the double type, I learned a month or so ago.
that is because there is a 53 bits mantissa ^^
Which I think is why JavaScript isn't completely insane to use 64-bit doubles as its default numeric type.
yeah, I learned that 53 bit trivia in the context of JS
yeah, I learned it from JS, then I looked back at university and remembered about the format of floats
and because there is a sign bit, and you can fix the exponent on 1, you have 53 bits of integers positive and negative
There is not one, but 2 exact integer representations of zero in IEEE 754 floating point 🙂
yeah, no 2s complement there
floats are somewhat evil and elegant at the same time
like so many things in cs generally :D
Tools with no sharp edges or warning points often make weak tools.
words of wisdom ^^!
You’ll like Rust then. I had a small headache dealing with floats in Rust land, coming from a far more lenient place. For example, they are unsortable (by default), because they may be NaN. This is IEEE approved as I understand it. You think you can just assume that 2.0 is greater than 1.0, you silly goose? Not around here, it ain’t.
No "guaranteed-not-a-NaN" subset that is comparable and hashable?
2.0 is greater than 1.0, but there is no total order over all IEEE floats if NaN is allowed.
No, this is all true to their actual representation, as I understand it, and it’s your job to work out the limitations and safety in your program.
The Rust PoV is that we’re all coddled and worse off for having someone else impose compromises over the reality of them.
The examples you cite sound like they could be examples of trying to prevent you from shooting yourself in the foot, aren't they?
That’s very much it, you have to explicitly agree to shoot yourself in the foot, or define a safe subset.
well, would you grant that some people might use floats as keys in maps and shoot themselves in the foot because of a lack of understanding of the properties of floats?
The Rust approach sounds reasonable to me. Here are your shackles. You may break them if you want, but then it's on you to deal with the freedom given.
But if you are staying awake while breaking them, you should be thinking to ask why it was there in the first place, and presumably there are some docs explaining why.
Using NaN as a key in a map is a bad idea in Clojure, though. One of those ways to shoot yourself in the foot.
I'm not arguing for changes to Clojure here -- I have written my fair share of doc pages explaining where the sharp corners are, and how to avoid them. I should probably learn more about Rust, to learn more about their approach. Sounds interesting.
Right, and in Rust, unless you can somehow guarantee at compile time that there never will be a NaN, it’s a no thank you.
Sounds like an opportunity for someone to define a NaN-free subset of floats that compilers don't have to go to extraordinary lengths to preserve (someone may already have done such a thing, and I haven't heard about it)
I like Rust, it’s got some nice things going for it. Shame about all the syntax, but hey, that’s ALGOL for you.
That exists. There’s a crate you can pull in that has a different set of compromises (you’re allowed no NaNs)
Oh, apparently there’s this one as well. Defines NaN as equal to NaN to get around it. https://crates.io/crates/fraction
Sounds like a potential performance impact, depending upon the implementation technique. Huh.
Yeah possibly. Might be acceptable in some circumstances, for some programs, I suppose. Either way, you’re forced to choose, which is all they want in the first place.
There are a lot of things in Rust that speaks to the sensibilities of someone coming from Clojure. Like, if you’re sharing a value, you’re going to share a constant. If you’re mutating it, you’re not also sharing it. You want to “share” it and also mutate, you have to make a copy.
(so easy to forget negative zero)
related to the testing issue above: is this a bug? it seems you have to redef tests for the new multimethod entry for assert-expr '=
to take effect?
the problem being: CIDER doesn't load middleware until a request comes in requiring it. The testing overrides here are brought in after the deftest
are eval'ed, so that even after loading the middleware the defmethod has no effect until the tests are redefed. seems quite strange