This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-05-11
Channels
- # aws (6)
- # beginners (105)
- # boot (6)
- # cider (50)
- # cljsrn (10)
- # clojure (41)
- # clojure-brasil (6)
- # clojure-italy (25)
- # clojure-nl (17)
- # clojure-russia (4)
- # clojure-serbia (1)
- # clojure-spec (8)
- # clojure-uk (242)
- # clojurescript (27)
- # core-async (10)
- # cursive (5)
- # data-science (9)
- # datomic (43)
- # emacs (6)
- # fulcro (6)
- # graphql (1)
- # javascript (3)
- # juxt (4)
- # lein-figwheel (1)
- # mount (1)
- # onyx (19)
- # parinfer (2)
- # portkey (15)
- # protorepl (1)
- # re-frame (30)
- # reagent (3)
- # ring-swagger (1)
- # shadow-cljs (22)
- # sql (6)
- # tools-deps (23)
- # vim (13)
Ran into a bug in my code where I macroexpand-all some code and then walk across the nodes. When I come across a symbol?
I deref
it. This works until I hit a place where I try to deref a clojure.lang.PersistentHashMap
, and it yells at me for trying to cast a java.lang.Class to a java.util.concurrent.Future.
Thing is, I am walking the code to find java.lang.Class
instances that have a particular namespace.
I could reject this particular case as a precondition or try/catch around it, but I'm wondering if there's a more graceful way of detecting this kind of thing earlier. Maybe a better condition than just symbol?
?
Not sure of your intended purpose of using deref on all symbols, but looking at the source for clojure.core/deref shows some class names like clojure.lang.IDeref that you could try doing (instance? clojure.lang.IDeref my-thing) checks on.
is there a shortcut to this?
(defn make-user [username reward time id]
{:username username :reward reward :time time :id id})
A shortcut how? I mean, if that is all it is doing what's the point in having the function?
like so
(defrecord User [username reward time _id])
(let [fields [:username :reward :time :_id]
picked (-> u :lst (map fields))]
(apply (partial map ->User) picked))
That's not a syntax I've seen before. What does the leading ->
do on ->User
? I assume it's not just a thread macro with no space
It can be tempting to see something like ->User as special but ->User is just a regular symbol
->User
is the name of the User
records positional constructor, whilst map->User
is the constructor to convert a map into a User
record. Clojure constructs these automatically for records.
It looks to me like the (partial map ->User)
is a typo and should be (partial map->User)
also prefixing ->
to a var or symbol can be quite common in Clojure code. You should read it aloud in those cases as being “to”, e.g. “to-User”.
or “map to User”
ok just seen someone else replied
Yeah, we use X->Y quite a lot, but that’s a defn. I need to poke defrecord, I was just confused by the ->User being called when I couldn’t see a definition for it. Didn’t realise defrecord made one automatically
yeah you can’t override its implementation though; deftype
’s differ from defrecord
s in that they lack these extras… which is useful if you need different behaviour on them; though deftype
s are pretty minimal, so typically require more work.
@bravilogy there's a fun macro for that
user=> (defmacro keyed [] (into {} (map (juxt (comp keyword name) identity)) (keys &env)))
#'user/keyed
user=> ((fn [a b c] (keyed)) 1 2 3)
{:a 1, :b 2, :c 3}
user=> (let [a :a b :b z :z] (keyed))
{:a :a, :b :b, :z :z}
or a little closer to your existing logic, (map->User (select-keys m (keys (map->User {}))))
Any Selmer users here that know how to "escape" {{
inside a template so Selmer doesn't think it's introducing a variable?
I don't see anything in the docs. I guess I could use an HTML entity for it...?
Does Clojure have something where I can do both non-blocking and multi-threaded. In other words, could I run multiple database queries on separate threads, and each thread is using non-blocking io?
clojure's io story is the jvms, and the jvm has both threads and blocking and non-blocking io, and you can mix and match it all
thanks @hiredman. Does that seem like a silly idea to you? As in, does it make any sense to not block a thread if that's the only thing running on it?
> jdbc's iterface (the main interface people use for databases) is decidedly blocking that's good to know
not as cheap as not having a thread, but you can have a ton before you notice any issues
or possible! :)
everything is "erased" to Object under the hood. (It's possible that will change in the future pending the JVM "generic specialization" efforts)