clojure

New to Clojure? Try the #beginners channel. Official docs: https://clojure.org/ Searchable message archives: https://clojurians-log.clojureverse.org/
2021-01-19T01:38:59.235700Z

Perhaps because of the name places its api doc next to the other namespace functions, the main docs being in alphabetical order. Obviously there’s the cheatsheet nowadays.

nhanclassroom 2021-01-19T03:35:08.239900Z

(defn handle-args
  "Handle your program arguments here.  You may support zero or more.

  Ensure that you use the exit function when processing is complete."
  [config {:keys [options arguments]}]
  (if (seq arguments)
    (if (= 1 (count arguments))
      [0 ["there is 0 arguments"]]
      [0 ["> 1 argument"]])
    [0 ["No argument handler"]]))
Can someone explain me [0 ["string here"]] meaning. This is a template name lein-bin. handle-args function is call in main fuction. It is also the entry where we put something we want to work in cli here. I can understand the abstraction layer. But the syntax is strange. Why we need ["a number" ["code"]] in here. It put directly "code" it will not work. What is the meaning of those numbers?

2021-01-19T03:39:18.240700Z

What is the function that calls handle-args doing with the return value?

nhanclassroom 2021-01-19T03:40:47.242600Z

(defn run 
  "Processing starts here.  Use this function when running from a REPL.

  The help option and any errors are handled here.  Otherwise, processing
  is passed to the argument handler."
  [& args]
  (let [config (configure)
        {:keys [options arguments errors summary] :as opts} 
        (-> (parse-opts args (:cli config))
            check-options
            (update-in [:options] merge logging-options)
            configure-logging)]
    (cond
      (:help options) (exit 0 (usage config summary))
     errors (exit 1 (error-msg config errors))
     :else (let [[exit-code message] (handle-args config opts)]
             (exit exit-code message)))
    ))
handle-args being called from here

nhanclassroom 2021-01-19T03:40:48.242900Z

(defn -main
  "Entry point for command-line processing.  This will be used when running outside of a REPL."
  [& args]
  (System/exit (apply run args)))
And run being called from here

2021-01-19T03:42:59.243500Z

This line of code (let [[exit-code message] (handle-args config opts)] is using a Clojure syntax for something called "destructuring". Are you familiar with that?

2021-01-19T03:43:36.243700Z

If not, I can explain it.

πŸ‘€ 1
nhanclassroom 2021-01-19T03:44:46.244300Z

Ah, thank you. Can you give me some good document url. I can learn by myself. I should not waste your time

2021-01-19T03:46:03.244600Z

No worries. The quick explanation is that when you do (let [[a b c] (some expression here)] ...), it calls (some expression here), gets the return value, and as long as that return value is a sequence kind of thing (either a list, a vector, or a sequence), then a is bound the first element, b to the second, and c to the third. Any remaining elements are ignored in that expression.

πŸ‘€ 1
πŸŽ‰ 1
2021-01-19T03:47:03.244800Z

Here is a link to an article on http://clojure.org with many other things destructuring can do, but you might want to focus on the part for sequence destructuring and save the parts that can take apart maps for later when you need it: https://clojure.org/guides/destructuring

βœ… 1
nhanclassroom 2021-01-19T03:49:59.245200Z

ah. So num in this case is exit-code

nhanclassroom 2021-01-19T03:50:09.245400Z

wow. Thank you very much.

2021-01-19T03:52:44.245700Z

So the symbol exit-code is bound to that first element, which are the numbers 0 in the sample code you pasted earlier. Those are passed to the function exit, which probably passes it to the Java method System/exit, and that becomes the exit status when the process exits, which some ways of invoking a program pay attention to to see if an error occurred (convention is 0 for all OK, non-0 for some kind of error).

πŸŽ‰ 1
2021-01-19T03:53:41.245900Z

The symbol message is bound to the second element, which in your example is a vector of one string. I don't know exactly what exit is doing with that vector, probably printing the string somehow.

βœ… 1
nhanclassroom 2021-01-19T03:58:33.246100Z

`(defn exit [exit-code message] (if (= 0 exit-code) (apply infoc message) (apply errorc message)) exit-code)

nhanclassroom 2021-01-19T03:58:37.246300Z

ah, I can find defn of exit here. Thank you very much

nhanclassroom 2021-01-19T03:59:02.246500Z

infoc and errorc is import from other package

jumar 2021-01-19T05:09:08.248100Z

Is there still a use case for definterface or should I always use defprotocol instead? Joy of Clojure, 2nd ed. (p. 302-304) mentions primitive arg types and return values as the primary advantage of definterface. Has that changed over they years?

seancorfield 2021-01-19T05:48:56.249500Z

https://corfield.org/blog/2013/05/01/instrumenting-clojure-for-new-relic-monitoring/ @jumar That is probably the only time I've used definterface and deftype myself...

😺 1
jumar 2021-01-19T05:49:48.249700Z

Cool, thanks for the example!

seancorfield 2021-01-19T05:50:06.250Z

Oh, and in next.jdbc...

seancorfield 2021-01-19T05:52:38.250200Z

https://github.com/seancorfield/next-jdbc/blob/develop/src/next/jdbc/result_set.clj#L440 I use it as a marker for disambiguating printing.

2021-01-19T05:54:28.250600Z

I think that's still accurate.

2021-01-19T05:56:29.250800Z

In that protocol functions are always boxed and can't be made primitive.

2021-01-19T05:56:50.251Z

As far as I know they still don't support primitives, so if you do need that definterface can be an alternative

2021-01-19T05:57:33.251200Z

Though I'm not sure if proxy or reify can generate primitive types, but I guess if the book says so

2021-01-19T07:44:32.251400Z

that's true, and one thinks of it as a thing you're doing to a namespace

borkdude 2021-01-19T11:13:51.252600Z

@jumar definterface can be useful if you want to provide a marker type, i.e. an interface for the sole purpose of being able to say (instance? Marker foo)

borkdude 2021-01-19T11:14:49.252900Z

see e.g. core.match where this is used

Vlad 2021-01-19T18:58:44.258600Z

quick question - a project that is set up with lein - is there a simple way of updating the documentation based on the project.clj ?

Vlad 2021-01-19T19:03:23.258700Z

it might be that the answer is no -

teodorlu 2021-01-19T19:47:58.261200Z

Good evening. I'm trying to look up an old post by Stuart Sierra where he argues for something along the lines of > it's better to have a broad API with many functions that's possibly difficult to understand than a narrow API that hasn't been battle-tested, and that might not have what I need. Digging through his blog now, https://stuartsierra.com/blog/page/26, haven't found it yet. Any advice?

teodorlu 2021-01-20T09:25:14.271400Z

@vemv thanks a lot! 😊

teodorlu 2021-01-20T09:38:22.271700Z

https://clojurians.slack.com/archives/C03S1KBA2/p1611087986264200?thread_ts=1611085678.261200&cid=C03S1KBA2 @grazfather Care to elaborate? I've begun reading that book, and I'm enjoying it so far. To me, it rhymes with a lot of what Rich said in The Language of the System, and the principles behind namespace-qualified keywords.

teodorlu 2021-01-19T19:51:14.261500Z

specifically, I think he mentions that he often prefers to use a big java package directly, rather than going through a Clojure wrapper that might just cover certain parts.

βœ… 1
Travis Brown 2021-01-19T20:13:43.263400Z

Is there a tool that allows generation of a spec based on sample data (as a starting point to further refine the spec)? Asking here because searching the terms "generate spec" are all about s/gen, and I don't see such a thing in the toolbox

2021-01-19T20:16:02.263500Z

like this? https://github.com/stathissideris/spec-provider

πŸ‘ 1
Travis Brown 2021-01-19T20:20:24.263800Z

exactly this! πŸ™

grazfather 2021-01-19T20:26:26.264200Z

I disagree. and I think that most would. Peek at "the philosophy of software design"

phronmophobic 2021-01-19T20:49:17.265500Z

having a narrow api that only indirectly gives you access to an underlying platform sounds a lot like https://en.m.wikipedia.org/wiki/Inner-platform_effect

βž• 1
1
caumond 2021-01-19T20:52:02.266Z

Did you have a look to available plugins in leiningen https://github.com/technomancy/leiningen/wiki/Plugins#documentation . Gargamel or codoc, depending on what you are looking for.

phronmophobic 2021-01-19T20:53:02.267100Z

I like the adage, β€œmake the common case easy and the complex case possible” since it’s all too common for libraries to do neither

βž• 2
1
vemv 2021-01-19T21:35:42.267900Z

ah this q is for me - I'm quite a big S. Sierra fan :) https://stuartsierra.com/2013/07/28/the-amateur-problem

πŸ™Œ 1
richiardiandrea 2021-01-19T23:06:38.269500Z

Hi there, does anybody know an alternative to https://github.com/jebberjeb/specviz? I would like to see my model in a nice graphical format and I wonder what I can use.