This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-07-27
Channels
- # announcements (12)
- # babashka (18)
- # bangalore-clj (3)
- # beginners (110)
- # calva (14)
- # cestmeetup (4)
- # cider (4)
- # clj-kondo (2)
- # clojure (34)
- # clojure-colombia (5)
- # clojure-europe (11)
- # clojure-nl (8)
- # clojure-spec (19)
- # clojure-uk (11)
- # clojurescript (16)
- # clojureverse-ops (8)
- # community-development (4)
- # conjure (65)
- # core-async (19)
- # cursive (22)
- # data-science (7)
- # datascript (1)
- # datomic (20)
- # devcards (1)
- # figwheel-main (64)
- # fulcro (10)
- # graalvm (3)
- # helix (3)
- # kaocha (22)
- # malli (68)
- # meander (6)
- # off-topic (39)
- # pathom (6)
- # pedestal (2)
- # reagent (5)
- # remote-jobs (2)
- # reveal (30)
- # rum (4)
- # shadow-cljs (83)
- # specter (3)
- # tools-deps (9)
- # xtdb (18)
I want to map the values in a hash map {}. What's the best way to do this? Why isn't there a core library function (unless I'm mistaken)?
You can write your own mapvals
:
(defn mapvals [f m]
(into (empty m) (map (fn [[k v]] [k (f v)])) m))
reduce-kv
maybe?
(reduce-kv #(assoc %1 %2 (f %3)) {} m)
(defn mapvals [f m] (reduce-kv (fn [m k v] (assoc m k (f v))) (empty m) m))
I made a gist that shows how to walk a map with nested maps and vectors: https://gist.github.com/gerritjvv/c41f67050dad8804010b657474971f21there is also: https://github.com/redplanetlabs/specter, for deep levels of maps.
When there's only one level of unwrapping in for
, we might as well use map
:
(defn mapvals [f m]
(into {}
(map (fn [[k v]] [k (f v)]) m)))
And we can get rid of the intermediate data (lazy sequence) created by map
, by using arity-1 of map
that returns a transducer, and arity-3 of into
that accepts a transducer:
(defn mapvals [f m]
(into {}
(map (fn [[k v]] [k (f v)]))
m))
Finally, if m
is a sorted-map, or some other kind of map, the output will still be a hashmap, which is probably not what we want. To fix this, we can use empty
:
(defn mapvals [f m]
(into (empty m)
(map (fn [[k v]] [k (f v)]))
m))
This way, we're pretty fast and also preserve the type. This doesn't preserve metadata. That's another thing to think about. See also: https://ask.clojure.org/index.php/1926/adding-functions-map-vals-and-map-keysI think that https://github.com/weavejester/medley/blob/master/src/medley/core.cljc would be util
A related (mapping values) blog article: https://lambdaisland.com/blog/2019-12-04-advent-of-parens-4-a-useful-idiom
hi guys, i'm a beginners and i would know how i can made a http request without download a library like cli-http
i not found a simple to do that like
(simple-request :POST "...")
yes for a simple interface without download a dependency
well those goals are in conflict :)
If you do want to avoid a dep then I suppose you're limited to the core library. Try (slurp "
can't do a post that way
at the bottom they're all using Java so you'd be doing a whole bunch of interop (something like https://www.baeldung.com/java-http-request)
well, i'm question about that just because i was used to javascript and python that have simples interfaces like request
and fetch
but maybe this is a different way that i need to get used to
imho. After doing some python with its requests module, I really wished Java/Clojure any lang had something like this. Then I realised python's requests is a module you have to install, just like http-kit 🙂
I'm not familiar with those, but have you seen these examples? https://github.com/dakrone/clj-http#post Specifically (http/post "
There's an extra {:body ...} instead of just passing it. but that's where future options (which there likely will be) would go
yess! i m using this to my study and i like that, maybe using javascript and script languages got me unaccustomed kk
i'm trying to use a single .cljc file to span 2 projects... the namespace is messing it up so i'd like to make a library of it... i'm not sure how i would make it a usable local library file i could include with :require ... presumably i can compile it and put it on the classpath? not certain
while i wait i'll explain the differences. If you are using deps.edn this is trivial. Just add a dependency on this new project and give it local coordinates while you develop it. I've also seen examples where this stays local coordinates and always just refer to the sibling projects the whole time.
If lein, you can look into lein checkouts
to have your local copy while developing. Will have to switch back to an artifact when done developing.
I used this https://github.com/s3-wagon-private/s3-wagon-private to create a private s3 bucket to host my local artifacts, so other projects could use it
haven't used deps.edn much but that sounds like a more convenient approach. there's one component file i need to stay synced between projects (it's rum/react stuff) so i think this is the way to go... how can i make sure a project is using deps.edn?
it's confusing, i thought it would be pretty smart to have the server and then a nested cljs project for the realtimey stuff, but now if i am trying to share a source file across 2 projects i make a library and every time i update the code i need to require a new library version?
@watchtheblur When I wanted to get fancy with time
I looked at the source. Turns out there is not a lot there:
(defmacro time
"Evaluates expr and prints the time it took. Returns the value of
expr."
{:added "1.0"}
[expr]
`(let [start# (. System (nanoTime))
ret# ~expr]
(prn (str "Elapsed time: " (/ (double (- (. System (nanoTime)) start#)) 1000000.0) " msecs"))
ret#))
So I just wrote my own using that as a starting point.yeah maybe just de-nesting is the easiest way
Is (comp - compare)
a good approach for reversing sort order? It seems to work perfect as far as I can tell, but it's not mentioned in any comment on ClojureDocs which makes me doubt myself.
oh misread. that actually looks fine. was thinking it was using -
as the compare function
now I'm tempted to add both of them to clojuredocs since these tricks are easy to forget
https://clojure.org/guides/comparators is a good reference btw
https://clojure.org/guides/comparators#_reverse_order mentions this example
Hi all! When I using the next.jdbc.sql/insert!
I noticed that I get a return such as {:last_insert_rowid() 5}
. I was trying to get the returned id by using something like (:last_insert_rowid() insert-return)
but that doesn’t work. Also, I noticed that I cant define the map {:last_insert_rowid() 5}
(it gives me a (err) Map literal must contain an even number of forms
)
Am I getting something wrong here? the return from insert!
is not an actual hash map?
this is a case of something creating a keyword programmatically that is not readable by the reader
hi, I have a function foo
that receives n params (predicates), and i'd like to re-use some of them, is there any way to "def" the params?
(defn foo [x & preds])
(foo x :a :b :c)
i'd want something like that
(def base :a :b)
=>
(foo x base :c)
does the order of predicates matter? This would work if the order doesn't matter
(def base [:a :b])
(apply foo x :c base)
it does,
if the order does matter, than you can use the slightly clumsier:
(def base [:a :b])
(apply foo x (conj base :c :d :e :f) )
yep, forgot about apply, thanks!
@aviv the normal idiom is something like this:
(let [base [:a :b]
f (apply partial foo base)]
... ; assumed multiple uses of f
(f :c :d :e :f))
you could skip the def of base and the apply and just have f (partial foo :a :b)
if that makes sense in your context
oh never mind, you need a hole for x
client.cljs , server.clj , components.cljc ... but the components.cljc has a call to a javascript function in .cljs ... what do I do
because i tried naively including it via require refer and got circular dependency
@sova, there's not enough info to answer your question. Generally, some options are: • move the cljs function into the cljc file • use reader conditionals, https://clojure.org/guides/reader_conditionals depending on what the use case is, there are other options that might be better suited
Thanks @smith.adriane! details: sente project, using rum for components server and clientside; trying to call chsk-send! from components.cljc is not working. will try including it in the file + reader conditional. the whole file is reader conditionals but i'm a little confused on how to make the javascript components have access to my cljs functions
come to think of it, i'm not sure how to include the rum components either
because normal refers don't seem to do the trick
what’s in the components namespace that is used from clj?
ie. what’s the reason to not make components a cljs file
serverside rendering / all the components
figuring it would make more sense to put them in one file because then they can't be out of sync as they were getting...
(the serverside-pre-render and the clientside ui)
I'm not familiar with rum, but you might be able to export (https://clojurescript.org/reference/advanced-compilation#access-from-javascript) some functions like chsk-send!
so that you can reference them in the server side rendered components
rum does mention server side rendering, https://github.com/tonsky/rum#server-side-rendering but it doesn't give much guidance. maybe there's a #rum channel where you can get rum specific advice for server side rendering
it's not important to have the serverside have js functionality to me, it just has to be an identical component in order to "hydrate" ... which inflates the dry html components with a javascript/reactive thingy without redrawing the whole page. pretty neat. but things gotta line up perfectly
where is chsk-send!
being called then? seems like you could just use reader condtionals to remove those calls
it's being called inside the cljs versions of the components, yes i could remove it but how do i make them do stuff?
thanks for your help, i feel like i'm missing something obvious
i'm imagining something like:
[:div {:on-change #?(:cljs (fn [] (chsk-send! my-args))
:clj nil)}]
I'm assuming that the rehydration will fill in the correct on-change handler assuming the html matches
oh it will, but that means i have to keep this component inside the client.cljs file and not the .cljc file it seems.
the reader conditional means it should be fine in the cljc file
hmm okay, maybe the error is elsewhere.
you may also need a reader conditional around the require statement that brings in chsk-send!
i'm stumped about how to refer these rum components into the client.cljs
the line in components.cljc is (rum/defc component-name) ... not a (defn ...)
according to https://github.com/tonsky/rum#server-side-rendering, it seems like that it should be fine to have rum/defc
in a clj file
that does make sense
i see some projects have {:paths ["src"]} and some have {:paths ["src/clj" "src/cljs" "src/cljc"]}. i know that clj is clojure, cljs is clojurescript, and cljc is common stuff. what are the pros/cons of using one directory structure for all the code, vs separating code out into different directories?
at least, i try and keep a minimal number of files, other projects have different philosophies... for me it's simpler to reason about as "client/server/etc" but some people like having many cljs files or many clj
personally, yes, i think if the number of files started getting large it would make sense to divide it up.
it usually looks like projectname/src/projectname/source.clj(s) for me. more interestingly you can use dots in the project name to create subfolders, you might see that in stuff like ring.util and ring.middleware
I was surprised to see how ()
, the empty list, behaves with when
and if
, as below.
(when () (prn "Am I really being printed?"))
;; prints "Am I really being printed?"
(when nil (prn "Am I really being printed?"))
;; does not print anything
I figure this has something to do when when
and if
being macros, but it feels like inconsistent behaviour. Could someone help me rationalize it? I think if I understand the reasoning, I'll be able to remember this behaviour better.
rather than true?
, I think you might be looking for boolean
Thanks, this is exactly what I was looking for. Clearly, "the thing I don't understand" is true?
vs. truthy?
vs. boolean
, and it sounds little research into those will clear this up.
The docstring of if
gives the definitive answer here. The rest are some helper functions that might help you along the way
> Evaluates test. If not the singular values nil or false, evaluates and yields then, otherwise, evaluates and yields else
there are two values that "choose" the else branch, otherwise the then branch is chosen
Great! Someone also wrote a very helpful example on https://clojuredocs.org/clojure.core/if .