Fork me on GitHub
#beginners
<
2018-12-08
>
Daouda09:12:04

hey guys, somebody using emacs on debian?

Daouda10:12:56

i am facing an issue with emacs on debian can you join #emacs for further detail please

Daouda10:12:39

then i will fully describe the issue

Daw-Ran Liou19:12:00

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)})

seancorfield19:12:20

@dawran6 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?

Daw-Ran Liou00:12:58

Yes. Thank you @U04V70XH6! For the example above, would you say it is pretty much the same to use a def here?

Daw-Ran Liou00:12:11

The example is

(defn resolver-map
  []
  {:query/game-by-id (fn [context args value]
                       nil)})

seancorfield00:12:47

Yes. There is nothing here that could cause problems by being evaluated at compile time.

Daw-Ran Liou01:12:39

Thank you very much. I just want to make sure if I wasn’t missing something here. 🙂

Daw-Ran Liou01:12:38

The tutorial went on and made this function take arguments. I guess that’s a reason why it started out like that

jaihindhreddy20:12:35

How do tagged literals work?

manutter5120:12:20

You mean like creating a custom tagged literal, or just tagged literals in general?

jaihindhreddy20:12:21

And also how to create custom ones.

jaihindhreddy20:12:39

I was trying to understand how integrant does what it does, with its ig/ref

jaihindhreddy20:12:45

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))

jaihindhreddy20:12:10

Is ig/ref run at read time then?

manutter5120:12:15

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.

jaihindhreddy20:12:54

That I get, tagged literals are what make EDN extensible.

jaihindhreddy20:12:10

But I was wondering when this parsing happens.

manutter5120:12:19

At read time.

3Jane21:12:52

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.

3Jane21:12:24

(Without destructuring the vector)

manutter5121:12:16

That is what apply does.

3Jane21:12:36

:woman-facepalming: I did have a brain derp. Thanks!

manutter5121:12:50

Happens to the best of us 🙂

3Jane21:12:37

then… to collect a lot of arguments into one, would you (comp f vector)? or is there a more idiomatic way?

manutter5121:12:27

I think it would be more common to see (defn foo [& args] ...)

jaihindhreddy21:12:30

Probably just write as a vector literal

manutter5121:12:46

or a vector literal.

jaihindhreddy21:12:06

(f [a b c]) instead of ((comp f vector) a b c)

manutter5121:12:55

(defn foo [& args] (prn args))
#'cljs.user/foo
dev:cljs.user=> (foo 1 2 3)
(1 2 3)
nil

3Jane21:12:15

(I don’t have the actual values, this is theoretical exploration of tools for composing functions. TLDR: use base language capabilities 🙂 )

manutter5121:12:45

If you use & args you’ll get a seq not a vector, but it’s easy to convert.

dmitrygusev21:12:52

is seq -> vec conversion O(n)?

dmitrygusev22:12:35

how about vec -> seq?