This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-10-19
Channels
- # beginners (37)
- # boot (96)
- # cider (23)
- # clara (2)
- # cljs-dev (40)
- # clojars (1)
- # clojure (149)
- # clojure-conj (1)
- # clojure-dev (2)
- # clojure-dusseldorf (5)
- # clojure-france (82)
- # clojure-italy (1)
- # clojure-nlp (1)
- # clojure-russia (13)
- # clojure-spec (24)
- # clojure-uk (62)
- # clojurescript (131)
- # core-async (13)
- # core-logic (7)
- # data-science (1)
- # datomic (10)
- # defnpodcast (3)
- # docker (4)
- # emacs (3)
- # events (4)
- # hoplon (68)
- # klipse (4)
- # leiningen (1)
- # off-topic (5)
- # om (140)
- # onyx (16)
- # pedestal (24)
- # planck (10)
- # proton (2)
- # re-frame (9)
- # reagent (4)
- # remote-jobs (1)
- # ring-swagger (16)
- # untangled (5)
- # vim (8)
- # yada (30)
I have a problem here that seems like it should have a straightforward solution, but I guess it is so straightforward that I can't see it. Given a map such as {:a 1 :b 2 :c 3}
, how can I return the key whose value is highest?
Everything I can think of involves messing around with max
and vector indices, which seems way too complicated.
Great... I was looking at the documentation for https://clojuredocs.org/clojure.core/min-key, which is far more abstruse.
(there are so many crazy random functions in core that are seriously hard to remember)
There should be a law that documentation examples have to cover the most common cases first!
Sometimes I think it would be neat to have a job doing Clojure full time on a team. Then I look at code people actually write, and I don't feel nearly smart enough.
(let [m {:a 1 :b 2 :c 3}]
(apply max-key val m))
I'm having a hard time grasping how this works, in fact. I know that val
gives the value of a map entry such as [:a 1]
.And m
, in the context of apply
, becomes a vec of map entries, right? [[:a 1] [:b 2] [:c 3]]
.
Ah, I guess val
is the "key" here... max-key
actually lets you use any function, as far as I can tell.
So it figures out a max by using val
as the key of each map entry.
lol, yes, it's the maximum or minimum object as determined by the key function. equivalent to (last (sort-by or (first (sort-by
I think the name is probably related to a use case like (max-key :balance [{:user-id 1 :balance 1}{:user-id 2 :balance 2}])
and yeah, when a map is in the last position in an apply
call, the map becomes a sequence of map entries
(note it's equivalent to but much more efficient than the sort-by variants, because doesn't build up an interim list)
@amacdougall - re: finding functions, check out this utility that comes with your repl:
user=> (apropos "max")
(clojure.core/max
clojure.core/max-key
clojure.core/ref-max-history
clojure.core.rrb-vector.rrbt/max-extra-search-steps
io.aviso.columns/max-length
io.aviso.columns/max-value-length)
i have literally never used it, but every so often i remember it exists and am like “why have i never used this"(you give it a string like “url”, “file”, etc, and it gives you back any functions whose name seems relevant)
When I want to call a different function for different regex-matches, is cond a good solution? E.g.
(cond
(re-matches #"1" 1) (callfunc 1)
(re-matches #"2" 1) (callfunc 1))
@danielgrosse I'd say so!
macro question - is there a fn i can use to transform the arg of a macro so that it has namespace qualified symbols - so i can use the forms as part of an eval
later ?
@dominicm resolve
returns vars... and would require consideration around special-forms (resolve 'if) => nil
which i was hoping already exists 🙂
(condp #(re-matches %2 %1) x
#"1" (callfunc 1)
#"2" (callfunc 2)
... )
Something like that maybe?Better matches the fact that the predicate and queried object don't change, just the cases by which the queried object is compared.
@fellshard That feels like a "clever" solution, I personally found the first one easier to understand.
However, I can understand with a large number of such cases why it might be necessary.
And yeah, the more expressions, the cleaner it'll be. Small number of expressions, might not be worth the effort
To me, there is mental overhead to parsing the predicate. And for 2/3 cases, not something that's really worth it.
Yeah. condp
that's the expectation at least, since the predicate is supposed to take two args, but not sure about re-matches
.
Would be neat if regexes implemented IFn
, maybe with re-matches
. Could see it as being a 'no obvious default' case, though
Is there a convenient way to “read” a .cljc
file as :cljs
?
I mean to see the output of the reader according to :cljs
or :clj
?
I want to create a default object with some values. The title has later to be extended with a number. I now use a
(def element
{:name #(fn [nr] (str "name-" nr))
:height 120
}
)
How could I call the function when I use the element later? Or is there a better way to do this?I want to do something like
(merge element {:width 200})
But when I want to assign multiple values at once?
Can you describe the problem you’re trying to solve? It’s a bit hard to guess what you want.
I want to create structures, which are then converted to xml. The elements have some default values, which are always the same, and some which I have to calculate at the generation. The nodes have to have a unique name, but for further handling, I assign them a common name and extend it with a number. e.g. element-title-1 element-title-2 element-body-1 element-body-2
so you want a function that creates a default object which takes some parameters, and then you assoc on top
and in this situation, just assoc each key in the map at the end onto the object you are creating
Okay, thank you. I will think about it.
and if you need unique auto-generated numbers, an easy way to do that is to have a single atom holding a counter and then increment it and return that value
monotonic-time
might help: https://github.com/danlentz/clj-uuid
Is there like a "clojure master class" style workshop that occurs anywhere in the US? I know clojure conj is coming up, but thats more like the future of the language.
Thank you all for your tips. :thumbsup:
I think my current weakness is that I'm a solid data manipulator, but a lot of the "metagame" of clojure is pretty highly developed, like how to optimize your workflow/repl development, how to make the attribute level the most important aspect of your programming, how to get the most out of core.spec, etc
well, there's one specifically on core.spec, but the rest are either intro or datomic
huh. The intro course will probably have one or two things I could benefit from. And the other two courses look good. I'd be shelling out a decent amount to hit them all though
@idiomancy have you watched previous years’ conferences’ talks online?
see also strange loop, clojure/west, etc (there’s one or more european conferences too)
I want someone who has had experience in industry clojure to curate a list of things that separate a powerful, professional clojure workflow from a "clojure immigrant"
@viebel you can invoke either the reader (in Clojure) or tools.reader with a specified feature set (which specifies the platform). (read-string {:read-cond :allow :features #{:clj}} “(your-form)”)
is the idea in Clojure’s reader, although the Clojure reader will forcibly add the platform feature so you won’t really get the cljs result properly. But you can do that with tools.reader which is slightly different to invoke but similar.
@viebel and in fact, reading a file using tools.reader with :features #{:cljs} is exactly what ClojureScript does. https://github.com/clojure/clojurescript/blob/176c681b25b75e907bf376698263dacf6ce998f4/src/main/clojure/cljs/analyzer.cljc#L3098
@idiomancy we get asked about “master class” type things at Cognitect all the time but in general when we dig in more we find that people are interested in widely divergent topics that are entirely dependent on what they are actually building. that is, they really want someone on-site to pair / assess / guide so we often end up doing that kind of an engagement instead. the intro class we do at the conj is a pretty good overview of a good percentage of the language you’ll actually use.
I am a co-author of Clojure Applied and our goal there was to convey how to take you from “I understand the syntax” to “I can apply this language to solve problems”. So that is my best answer there. :)
however, re your topics it does not cover repl workflow (which is both constantly changing and highly individualized in my experience so a poor topic for a book) or spec (which didn’t exist yet). I am working on a new edition of Programming Clojure which will have a full chapter covering spec plus some additional integration throughout the text (but don’t expect that soon)
please do! I think it is pretty full but still has some spots.
oh, congrats on snagging the edgiest functional programming twitter handle I've ever seen
lol i’ve seen that handle floating around for years and just now got the relevance to functional programming
what? is it just the word "pure"?
i mean, I dont want to overanalyze this, but its the association between "pure" fp and danger, like disruptive tech. You know, like, the fp revolution? I mean I dont think that's really the intention, but it kind of works
Pured Anger
P. U. Redanger
@alexmiller is there a command lime tool (or web app) that reads cljc files?
@viebel kind of like planck may be? https://github.com/mfikes/planck
@viebel and does what?
@jrheard the handle pre-dates by years me ever doing anything with FP and I had not ever thought of that :)
@idiomancy He talks about it a bit on https://defn.audio/2016/10/06/episode-11-alex-miller/ (puredanger)
before I used http://puredanger.com I owned http://baconwrapped.com :)
but I don’t anymore
That is a truly awesome domain name 😸
used to host my blog there
@alexmiller returns the :clj content or the :cljs.
It might be useful for debugging
@jrheard: Also available at http://clojure.org/community/swag
Hi! I'm trying to use case
with type
like this:
.. which doesn't work. What's the proper way to do this?
@alexmiller I heard you talking about puredanger,com on defn and I remembered having your blog on my rss catcher back in the day! I can see now why you've not had time to continue writing it! :thumbsup:
@andreas-thoelke Try (condp = (type ch) …)
@exupero hmm, but with condp
, how would I do the third condition in my example? (list of matching items)
contains?
is for map keys, I think?
You mean like this:
works actually!
thanks! 🙂
@brabster I write stuff at http://insideclojure.org occasionally, or also on the Cognitect blog http://blog.cognitect.com/?author=53c6f0c5e4b0c2fe7d81be34
Hi all, got a question on (with-redefs)
: is there any way for me to get at the value of the redefined var? My specific use case is that inside of some tests, I want to return mock data from a function for some specific inputs to it, but if the inputs aren't in that special set I want to call the original function
...and the calls themselves might be way down on the call stack and can be hard to predict. So what I'd like to do is something like (with-redefs [ns/foo (fn [x] (if (= x :specific-thing) :mock-data (ns/foo x)))] ...(test code)... )
...but I worry that the second invocation of (ns/foo)
will just recur into the redefined fn and blow the stack