This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-09-13
Channels
- # aws-lambda (21)
- # beginners (8)
- # boot (67)
- # braveandtrue (2)
- # cider (12)
- # cljs-dev (38)
- # cljsjs (87)
- # cljsrn (11)
- # clojure (307)
- # clojure-austin (29)
- # clojure-finland (1)
- # clojure-italy (9)
- # clojure-russia (19)
- # clojure-spec (71)
- # clojure-uk (33)
- # clojurescript (109)
- # clojutre (1)
- # core-async (2)
- # cursive (24)
- # datomic (11)
- # devops (5)
- # ethereum (5)
- # figwheel (2)
- # hoplon (25)
- # ipfs (1)
- # jobs (1)
- # luminus (17)
- # off-topic (2)
- # om (38)
- # om-next (3)
- # onyx (166)
- # other-lisps (1)
- # proton (5)
- # re-frame (15)
- # reagent (45)
- # ring (2)
- # spacemacs (6)
- # specter (12)
- # untangled (58)
- # yada (23)
Does anyone know if it’s possible to run just a single test or test class? It’s a bit tedious to run all tests for clojure everytime
are you using lein?
if so, lein help test
will explain how to run single test namespaces or how to set up selectors for subsets of your test suite
lein has test selectors, but there's no way that I've found to use them from the repl, so for me they seem like a bit of a non-starter. clojure.test/run-all-tests accepts a regex for namespaces, and clojure.test/run-tests will accept a list of namespaces. which I've found mostly sufficient
@deiga if it helps, all deftest
macros create a function that you can call in the general fashion of calling clojure functions, which will report whether the tests succeeded or not
oh, you’re actually talking about clojure itself!
yeah, there is a how-to page
the big index is at http://dev.clojure.org/display/community/Contributing
specifically http://dev.clojure.org/display/community/Creating+Tickets
and a little bit http://dev.clojure.org/display/community/Screening+Tickets
the developing patches page has info on this
under "Run An Individual Test"
also see #clojure-dev here or the clojure-dev mailing list
for places to discuss with people doing similar things
what are you working on?
I’m looking into clojure.xml. I had a problem getting the parsed XML as string, so I though I could add that. Also add some tests and maybe rewrite parse to use records since structmaps seem to be outdated
ok, it’s unlikely we will do much with that though - while we’re not planning to remove it we also don’t have plans to spend time enhancing it. It seems like switching from structmaps to records would possibly be a breaking change too.
generally it’s best to discuss these kinds of things on clojure-dev first before working on them
@deiga: have you looked at https://github.com/clojure/data.xml?
I’ve been trying to get into the google group where I was supposed to open a discussion about this. But my name still hasn’t appeared on the contributers list so I can’t get in there. I’d rather work on something and then scrapping it than just wait around 😛
it uses defrecords and because it isn't part of the core language, active development is a little easier
I’d rather spend my efforts to improve the language core than work on some external library
I understand the impulse, but it is extremely unlikely that any work on clojure.xml will actually get merged
you are likely looking at years of watching the patch go no where in jira, there is no guarantee a patch for data.xml wouldn't do the same thing, but it is slightly less likely
alexmiller: You say something about not planning to spend time enhancing clojure.xml, how much time can it take to look over a contribution and merging that?
particularly in opportunity cost of not working on something else we consider important
data.xml is where I would put any energy around xml
well, I’ve been trying to be better about that :)
there was an issue in the clojure jira to make subs take negative numbers, the guy wrote a patch, it sat there for years, and then was closed as declined
As someone who actively maintains one contrib library (and less actively maintains another, plus occasionally helps out with others), I would strongly support hiredman’s suggestion that you direct your efforts at the contrib library for XML stuff @deiga and not worry about the core stuff.
Changes to core are very carefully considered and reviewed which means they have to be much more conservative by definition. That’s what gives Clojure its amazing stability (and why we happily run Alpha builds in production all the time!).
my point with subs is not to argue about subs, but that was a pretty small change, it took years to be dealt with, so expect a larger change to take longer
it may have been like, there was an issue for it open for three years without a patch, someone opened a new issue, with a patch, and the patch was declined and both issues were closed
I would like to get a hash map to respond to clojure.core/name like a keyword. Suggestions?
@creese could you give an example of input/output you'd expect?
Mmm...I guess a spec where all the keys can be missing is not a very good one right? I can do: (s/def ::info (s/keys :opt-un [::name ::twitter-handle ::facebook-id ::domain ::telegram-token]))
but then {:asd "asd"}
is always valid, am I approaching the problem from the wrong angle?
well that’s a perfectly fine spec and the opt-un will help it generate interesting values
you could just do (s/keys)
too to validate all the keys per their specs
Thanks Alex, the generative part is working fine, maybe I am missing a way to validate that ::info
can only have this spec and {:anything "bla"}
is not a valid one
maybe it will be valid in the future
Is there a predicate that matches seqs, lists, vectors, and sets, but not maps? i.e. I want to be able to call seq on the collection and iterate over the values. map is the odd one out here
sequential?
maybe, not sure about sets though
usually when I ask this question I end up changing my mind about needing that later though :)
yeah I do wonder that too 🙂
@richiardiandrea you can use an additional predicate to exclude all but a known set of keys but spec (and Rich) would encourage you to maybe not do that
yes I was wondering if it was good practice, but I am using spec to validate data at the boundaries and I cannot avoid adding that check, or anything will pass through
@alexmiller Thanks for your answer earlier about specialized sets! Good to know that my core objective was ill-advised. I found a better way of efficiently rendering my lines, fortunately, and if I ever do have to confront this use case, I will be better armed.
@richiardiandrea that is one of the best places to need such a thing
@alexmiller thanks for confirming!
so (s/merge (s/keys) (s/map-of #{:name :twitter-handle :facebook-id :domain :telegram-token} any?))
will work
or add the opt-un if you want generative
yes I'll opt for opt-un
🙂
@alexmiller if I may add a question, will this work in the 1.9.0
release?
(def ::info-keys [::name ::twitter-handle ::facebook-id ::domain ::telegram-token])
(s/def ::info (s/keys :opt-un ::info-keys))
It would be neat 😄This whole exact same conversation happened in #clojure-spec this morning btw :)
oh too bad ... ok tnx, oh cool let me go there and read 😄
No worries, but there is more explanation there
btw this now works:
(s/def ::info (s/merge (s/keys :opt-un [::name ::twitter-handle ::facebook-id ::domain ::telegram-token])
(s/map-of #{:name :twitter-handle :facebook-id :domain :telegram-token} any?)))
(s/explain ::info {:twitter-handle "@trat" :asr "rst" :domain ""})
In: [:asr 0] val: :asr fails spec: :app.division/info at: [0] predicate: #{:telegram-token :name :facebook-id :domain :twitter-handle}
nil
For everyone who has been answering all my dumb questions, you'll be glad to know that I'm at least making good progress on this map rendering business! Perhaps this SVG will render when I upload it...
...okay, not particularly. Guess Slack doesn't recognize SVGs as images it can embed. Just take my work for it that it works! Eventually I'll get an actual web page up to render this stuff for live coding with Figwheel.
I see it in Chrome! Nice.
@rui.yang yep - Clojure has multimethods
. different functions are called depending on a dispatch function http://clojure.org/reference/multimethods
Having a protocol and dynamic var in ns1:
(ns ns1)
(def ^:dynamic *m* {:a 1 :b 2})
(defprotocol Protocol
(f [arg]))
(extend-protocol Protocol
clojure.lang.MapEntry
(f [arg] (println *m*)))
and a second namespace ns2:
(ns ns2)
...
(binding [ns1/*m* (conj ns1/*m* {:c "three"})]
(f m))
I expect the map *m*
, inside the binding, to contain 3 entries. However, there are only 2, the result of the (println *m*)
being {:a 1 :b 2}
, so the binding doesn't seem to work. Why is that?Or better, how to change the dynamic var temporary inside the scope of a function in another namespace?
yeah, I actually did require - I just removed all non-important code (well, at least, I think it's not important 🙂 )
got an error using bound-fn
😛
I wanted to test if there is any async code between your (f m) and println, that would explain it
well, there's really not much more code in between
hmmm weird... binding doesn't seem to work fine. Just a sec.
oh, the binding does work in ns2
but it doesn't on ns1
can you show your ns2
code?
or is it the exact same?
(binding [ns1/*m* (conj ns1/*m* {:c "three"})]
(println ns1/*m*)
(-> ...
ns1/f)
the println
here shows the conj
ed binding.
the println
inside ns1
shows the wrong one. There's nothing in between them.
oh, got it...
so I didn't add all important code:
with:
(ns2
(:require [ns1 :as n]))
I cannot do: (binding [n/*m*...] ...)
but I should do: (binding [ns1/*m*...] ...)
I really thought I could use the namespace alias. Seems I can'tHi, I’m trying to create a spec for a vector of maps that insures uniqueness based on key of map. For Example, I’d like to insure the maps in below vector are unique by the :id key.
[{:id 1 :name “foo”}
{:id 2 :name “bar”}
{:id 3 :name “foo”}]
Below only works if complete maps are distinct
(s/coll-of map? distinct true)
This would fail, as the maps are distinct, but it’s not what I want because the id’s are not unique.
[{:id 1 :name “foo”}
{:id 2 :name “bar”}
{:id 2 :name “foo”}]
you’ll need a custom predicate for that
no s/and’ed with the coll-of spec
(s/and (s/coll-of map?) #(thing that verifies uniqueness))
did you ever think of allowing functions to be passed to distinct, e.g. (s/coll-of map? distinct :id)
would make distinct by :id.
it’s an interesting idea, but it might be harder to make that gen automatically
understandable. I’ll see where I get with using (s/and as generating a sample set is one of my goals.
you may also find it hard then :) you might need a custom generator with s/with-gen (or maybe the generated things will be unique enough that it doesn’t matter, not sure)
Does clojure have the concept of an interruptible function?
Yeah, for instance
And it does so without blocking the thread.
This same behavior can (and is, when accounting for older browsers that don't support this syntax) be implemented with generators
expected: (== (+ (:margin render-env) (* (:mazes.grid/x top-right-cell) (:cell-width render-env)) (* (:mazes.grid/x top-right-cell) (:cell-h-spacing render-env))) (:x top-right))
actual: (not (== 920.0 919.9999999999999))
Floating point inaccuracy is the devil. Is there a better solution than writing is-more-or-less-equal-i-guess?
and using that instead?
or reducible sources. costs in the compiler & runtime would have to be quantifiably minimal... Definitely possible
I would like to encourage all of you to try Clojure 1.9.0 alphas. Spec is great, but the new checking of macros and many builtin forms is uncovering bugs in a lot of software. Most of these are super easy to fix, but that’s work that parallelizes well. Here’s an example: https://github.com/lvh/prone/commit/7ec6bc8ed6db151bed1b980ce7b05107c73eae76
Syntax aside:
(reify clojure.lang.IReduceInit
(reduce [_ rf init]
(loop [res init]
(let [v (f)]
(if (identical? v eof)
res
(let [res (rf res v)]
(if (reduced? res)
@res
(recur res))))))))
(__generator_fn__
[f eof]
(loop []
(let [v (f)]
(when-not (identical? v eof)
(yield v)
(recur)))))
If a lot of folks give this a shot, then by the time 1.9.0 rolls around, a lot of newbies are going to have a much better time 🙂
btw, I have been keep tracking of things like this at http://dev.clojure.org/display/design/Errors+found+with+core+specs
absolutely!
it’s a wiki :)
maybe you’re just lucky :)
=> (map #(.hashCode %) {0 0, 7 7, 1 1, 4 4, 6 6, 3 3, 2 2, 9 9, 8 8, 10 10})
(961 1185 993 1089 1153 1057 1025 1249 1281 1217)
maps don’t use .hashCode
they use hash (backed by hasheq)
so map hash
instead
=> (map hash {0 0, 7 7, 1 1, 4 4, 6 6, 3 3, 2 2, 9 9, 8 8, 10 10})
(-1663711614 825985219 670845861 1457028219 -645429335 921681221 1199864066 -813835253 2145648727 1228926976)
it coerces its arg to a sequence, then takes the first element
in this case that’s a sequence of map entries (2-element tuples)
maps are unordered, so no particular order is guaranteed
The order is deterministic… if you know the algorithm. I was just trying to pick at it without looking at the code 🙂
well of course, but you should not rely on it as it can (and has) changed
if you want the order to be consistent, you could use a sorted map
I know. I spent a week fixing people’s borked tests when Java changed their hash function from 1.5 -> 1.6
well in this case it’s Clojure’s hash function that’s important (and it changed in 1.6)
though I suppose your keys would need to be comparable
I think I saw a couple of tests fail when Clojure 1.6 came out too… luckily by that point people had learned (after all, Clojure devs at that point were usually pretty experienced Java devs first) so it wasn’t too messy
(map hash (keys { 7 7, 1 1, 4 4, 6 6, 3 3, 2 2, 9 9, 0 0, 8 8}))
(0 -137604029 1392991556 -803074778 1795257809 -1556392013 -971005196 1887164919 -153401025)
Calling clojure.java.shell/sh
in user.clj hangs. The same call in another namespace doesn't hang. Requiring that other namespace in user.clj hangs. There's something special about user
at load time that I don't understand.
well, hash tables usually order by (hash key), and then mod into the size of the table. The keys here are longs, and Java uses identity for the Long.hashCode (or they did? They still seem to). Based on that, I was wondering if an entry of [0 0] would end up at the start of the table, no matter what. That’s what I went looking for
but Alex pointed out hash
… which, now that I think about it, I think I knew that once, and then forgot
I was just poking around for fun, to see if there was another explanation for [0 0] showing up first, other than pure randomness. Even if there were, Clojure 1.10 could change the ordering again. It was just navel gazing, since any expectation of ordering on a hash is The Wrong Thing™
@quoll there is no mod. entries in hash maps are stored in hash array-mapped tries and the sequence ordering is based on traversing that tree
also, aren't maps ordered under 7 keys maybe, and then swap to an unordered implementation above that?
yes, it’s <= 8 although there is at least one case (maybe with literals) where there is an off-by-one and it’s actually 9 I think
http://dev.clojure.org/jira/browse/CLJ-1587 is the case I’m thinking of
ah, I had presumed that hashmaps were using a vector to get their persistence. OK… I now have code to learn from. Thank you
array maps will produce sequences based on the order of insertion (as it’s just backed by an array)
vectors are also backed by hash array-mapped tries, keyed by index
it’s a little different but same idea
is a good starting point
at clojure/west, there was a guy who did an excellent presentation on reordering the way key/value pairs and pointers to other tries were stored in HAMT. Has there been any more movement on his work?
@alexmiller Since we’re talking about insertion order; given what I understand of prefix tries, they wouldn’t have the attacker-triggrable pathological insertion behavior that prompted randomized hashing for Python, Perl… &c’s hash tables — right?
@dpsutton not to my knowledge
@lvh that is still a concern as there are hash collision nodes
which devolve to linked lists
there’s actually a ticket about this
not sure if you’ve looked at how Java handled it, but the details are pretty interesting
but it doesn’t have many of the details
in Java 7 they started seeding hash maps so every map was basically hashing differently
in Java 8 they undid all that and just changed the implementation to not devolve to linked list behavior on hash collision but instead to use a red/black tree
a similar approach could be used in Clojure as well
another workaround would be to wrap keys in a deftype that used an alternate hash function
if you’re interested in this, tracking down the Java JEPs had a lot of useful info iirc
haven’t thought about this in a couple years :)
having looked at this stuff a bit I have a lot of appreciation for the apparent thought involved in Java’s choices, both tactically and strategically
Alex, one of the reasons I moved from Java to Clojure was the HashSet/HashMap change in 1.8. It used to be that Set membership was defined by .equals() which had to be consistent with .hashCode(). Now they sometimes use the Comparable interface (HashSet is implemented using HashMap). We had code break on the 1.7->1.8 upgrade.
The Javadoc for Set.contains still claims "More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e))." which is not true for HashSet in all cases.
Clojure’s sets are based on =
and hash
of course, it helps that =
is not something that is open for interpretation in Clojure :)
Ahh, and so maybe I just setup the anonymous functions to be argument compatible with threading macro?
in fact my rule is (which I think I inherited from technomancy) that if you're having to use function literals its already complicated enough for a for
, being able to name your iteratee and do lets and whens makes a for
much more readable IMO
Is there a switch to lein run to watch the project files and recompile? I'm trying to learn Clojure and Pedistal, and would like to see my changes with out stopping and starting with each change. Thanks!!!
@linicks nothing for lein run
that I know of. There are some specialized tools for autotesting, or in the case of ring there's a reload middleware. Perhaps there's something like that for Pedestal? Otherwise you could hook up a REPL to your editor and run the Pedestal app from your REPL, then you can recompile code into the same process that Pedestal is running from.
@smw, as an alternate to @pjstadig’s rule, I usually just create defn-
s outside of the map
and call them that way. That way, you end up with flexibility, functions can be used elsewhere, and with good naming, the map
becomes self-describing.
@akiva yes, i agree that splitting out functions is also a good way to go. sometimes I find it hard to come up with good names for intermediate computations, but also a for
can get to the point where it's doing too much as well.
When it comes to internal functions like that, I don’t fuss too much since the API’s… well, internal. So, going back to @smw’s example, that inner function would be fine for me as get-source
or something. And then I’d make both maps transducers and string them together rather than nest them.
Honestly, as long as there isn’t a performance hit and it reads well, then go with what you like.
I’ve discovered there’s almost always a more succinct way to do things but nothing wrong with a little verbosity if it makes the code easier to reason about.
Not sure if letfn
would be better here for this rather than defining functions in a let
.
Yeah, I just don’t like polluting the namespace with one-off function defs that aren’t obviously related to the task at hand… even if they’re private.
Actually, @smw, that’s one of the foundations of functional programming (those foundations being rather fractalized) is that it’s better to have 100 functions that each do one thing incredibly well than one bloated function that does many things poorly.
Sure, but if the functions are one-shot and not really composable, does it make sense to name them at the root of the namespace?
Good question and something I sometimes struggle with. But I’ve no qualms wrapping a deeper namespace in a one-liner in a namespace closer to the API layer if it makes the code clearer. For instance, in one project, I have a bunch of entities that need to be saved to the database and instead of just always calling directly to the raw database save
, they each have their own save
function that literally just wraps the raw save
. That way, I can have code like (entity/save entity-instance)
.
And if later I need to do some customization before save, I won’t break calling code. And I’d rather compose off of that than an integration layer.
Yep. Once you get comfortable with function proliferation, it becomes a matter of organization, really.
I still use anonymous functions, mind you, when it’s something super-terse and easily understood.
If you want to see how it actually looks in practice, check out https://github.com/akivascript/furthermore/blob/dev/src/clj/furthermore/entities/posts.clj.
Does Clojure have any notion of self-referential data structures, such as you might see in Lisp '#1=(1 2 3 . #1#)
you can do it via atoms or other stateful things if you work at it, but: don’t :)
similar to 'tying the knot'. Reiterating it's not a good idea because clojure favors values rather than references.... but @bbloom has done it in fipp
i think
mostly just curious, but sometimes in maps such as the one in lein's project.clj, I've had to duplicate stuff in another key. Thought it might be nice if there was a way to refer to other parts of the same data structure.
@akiva wow didn't know you had created that, it looks nice 😉
Thanks, @richiardiandrea! I hope that wasn’t sarcasm. Hah.
lol, no no I am checking the code 🙂 You also have Twitter in there, that I always wanted to have in a blog. When I was working for Lambda-X we used cryogen
The twitter-api
library is kind of old but it works… except with Clojure 1.9. It breaks spec all over the place.
yeah I opened an issue there 🙂
we will eventually need to fix it because my current project has that dependency
ahahah great
@akiva whats the issue?
This might just be me but as it is right now it throws if it encounters anything that isn’t already a java.util.UUID
instance so it’s kind of useless when you’re pulling UUIDs as strings out of a database. So it isn’t a breaking issue, really, it just seems rather limited in comparison with how I’ve seen people handle and process UUIDs from different sources.
To further the point a bit, if you have to convert a string to a java.util.UUID
, you’ll never need uuid?
anyway because you’ve already converted to a UUID
instance.
But if you’re passing data through multiple layers and you have a routine that expects a UUID then it’s useful.
Also bear in mind that there are multiple string forms of UUID. And also multiple binary forms.
I think we have to deal with four different UUID representations at work.
We’re mainly dealing with java.util.UUID
with uuid?
so the other representations aren’t as relevant and would require handling on the dev end. I’m fine with it staying in line with java.util.UUID
but being able to recognize a string that adheres to the JVM’s UUID as something that could be converted correctly to an instance would be helpful.
how could it check whether a string is a uuid?
is this a setup for the old "a programmer has a problem and thinks I'll solve it with a regex" joke/
I suppose, not sure if Rich would go for that or not
I think it'd be surprising if uuid? returned true for both a string and a UUID. would it also need to generate both strings and UUID objects?
sorry I mean would the associated generator also have to generate both uuid strings and uuid objects
(s/exercise uuid?)
does indeed produce UUID objects @akiva
Interesting. Looking at https://github.com/clojure/clojure/blob/b26a8d0efc0675d0040dfbb28e8b489b866f7507/src/clj/clojure/core.clj#L6681, it looks like it’s just returning a boolean.
And you could write a generator for UUID strings based on that (use uuid?
to generate the UUID
and then fmap
it to a string of the appropriate form.
boot.user=> (s/exercise uuid?)
([#uuid "d46604d9-43be-415b-96fd-616992227e0b" #uuid "d46604d9-43be-415b-96fd-616992227e0b"] [#uuid "219f702b-59f3-4165-ac7f-2ffb76a06e73" #uuid "219f702b-59f3-4165-ac7f-2ffb76a06e73"] [#uuid "fe832237-2f88-4621-a912-45c45959d3b1" #uuid "fe832237-2f88-4621-a912-45c45959d3b1"] [#uuid "10e03132-a9da-4b4b-9880-b497310967a4" #uuid "10e03132-a9da-4b4b-9880-b497310967a4"] [#uuid "cbcd32a3-7e21-452a-9505-59d36de3c2fd" #uuid "cbcd32a3-7e21-452a-9505-59d36de3c2fd"] [#uuid "50d2dce5-2427-4304-9a17-e259f3db78bf" #uuid "50d2dce5-2427-4304-9a17-e259f3db78bf"] [#uuid "3fc3768a-68ec-46da-ba1e-15b7e91063bc" #uuid "3fc3768a-68ec-46da-ba1e-15b7e91063bc"] [#uuid "b992038c-f4dc-41fe-8349-41849fa90c36" #uuid "b992038c-f4dc-41fe-8349-41849fa90c36"] [#uuid "5d1f1321-9bba-4806-9a86-88cc3977047a" #uuid "5d1f1321-9bba-4806-9a86-88cc3977047a"] [#uuid "8f4327b4-6869-4c61-ae94-cc7d8e1822f8" #uuid "8f4327b4-6869-4c61-ae94-cc7d8e1822f8"])
(just showing that uuid?
can generate objects)
I have to admit, I’m spec-ignorant at this point. I’m just looking at the code and it shows it returning a boolean. I need to read up on this some more. It’s also why I haven’t filed an issue. I’m not about to bust through a wall like the Kool-Aid man on clojure.core
. Heh.
Bringing this back home to my point with @smw, though, this is a good example of how uuid?
simply wraps a one-liner around deeper code.
@akiva generators for predicates are mappings defined here https://github.com/clojure/clojure/blob/master/src/clj/clojure/spec/gen.clj#L128
Interesting, @bfabry. It’s showing uuid?
mapping to (uuid)
which, I imagine, would produce UUID instances.
But I also like @sveri’s comment that a string representation of a UUID isn’t actually a UUID even if it could be used as input to creating a proper java.util.UUID
.
I think my issue is solved with (class #uuid "550e8400-e29b-41d4-a716-446655440000”)
. Wrapping the string in the (class #uuid)
and passing that to uuid?
. Boom.
Heh, basically. What could be used to create a UUID isn’t yet a UUID and thus would fail uuid?
because it’s not a UUID yet.