This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-12-08
Channels
- # adventofcode (283)
- # aleph (2)
- # announcements (1)
- # aws (12)
- # beginners (43)
- # calva (14)
- # cider (9)
- # cljsrn (1)
- # clojure (16)
- # clojure-kc (1)
- # clojure-nl (2)
- # clojure-uk (8)
- # clojurescript (55)
- # cursive (6)
- # datomic (6)
- # emacs (15)
- # jobs (1)
- # kaocha (5)
- # nrepl (13)
- # re-frame (2)
- # shadow-cljs (3)
- # spacemacs (6)
- # sql (10)
- # vim (13)
- # yada (5)
i am facing an issue with emacs on debian can you join #emacs for further detail please
When is it preferred to write a function with no argument over a def? For example, on Lacinia’s example, https://lacinia.readthedocs.io/en/latest/tutorial/init-schema.html#id5, the resolver-map
is a function. Will it be fine if I use def here?
(defn resolver-map
[]
{:query/game-by-id (fn [context args value]
nil)})
@dawranliou With a def
, the body is evaluated at compile time, i.e., whenever the namespace is loaded. With a defn
, the "body" is evaluated at compile time but it produces a function and the function's body is only executed when you call it at runtime. Does that help?
Yes. Thank you @U04V70XH6! For the example above, would you say it is pretty much the same to use a def
here?
The example is
(defn resolver-map
[]
{:query/game-by-id (fn [context args value]
nil)})
Yes. There is nothing here that could cause problems by being evaluated at compile time.
Thank you very much. I just want to make sure if I wasn’t missing something here. 🙂
The tutorial went on and made this function take arguments. I guess that’s a reason why it started out like that
How do tagged literals work?
You mean like creating a custom tagged literal, or just tagged literals in general?
in general
And also how to create custom ones.
I was trying to understand how integrant does what it does, with its ig/ref
Looks like ref
is a function that returns a record
(defn ref
"Create a reference to a top-level key in a config map."
[key]
{:pre [(valid-config-key? key)]}
(->Ref key))
Is ig/ref
run at read time then?
In general the basic idea of tagged literals is to give the Clojure reader a way to recognize and evaluate custom data types. When the reader sees #inst
, for instance, it knows the next thing it sees should be a string with a formatted timestamp that it knows how to parse.
That I get, tagged literals are what make EDN extensible.
But I was wondering when this parsing happens.
At read time.
:thumbsup:
Hey, I’m having a slight brain derp. Is there an inverse operation to apply
? Say I have a function that takes 2 arguments, and I have a vector of 2 elements, and I want to pass those as arg1 and arg2.
That is what apply
does.
Happens to the best of us 🙂
then… to collect a lot of arguments into one, would you (comp f vector)
? or is there a more idiomatic way?
I think it would be more common to see (defn foo [& args] ...)
Probably just write as a vector literal
or a vector literal.
(f [a b c])
instead of ((comp f vector) a b c)
(defn foo [& args] (prn args))
#'cljs.user/foo
dev:cljs.user=> (foo 1 2 3)
(1 2 3)
nil
(I don’t have the actual values, this is theoretical exploration of tools for composing functions. TLDR: use base language capabilities 🙂 )
If you use & args
you’ll get a seq not a vector, but it’s easy to convert.
is seq -> vec conversion O(n)?
how about vec -> seq?