Fork me on GitHub

The easiest to shadow is name and then key for me. And I've been bitten by it


Lisp-2 seem to have way too complex of rules with all these symbol name spacing rules. I don’t see how that would be preferred to a lisp-1. Shadowing is a minor annoyance in comparison. In my own opinion of course. 😬


“Common Lisp has 7 namespaces” immediately strikes me as a problem. Hah


I’m just surprised it is preferred by people.


I am trying to find the index of non-zero elements in my matrix but I cannot doing that using the clojure matrix core function non-zero-indices as it gives me the indices in this format #object[[I 0x7956f93a [I@7956f93a]]]. Does anyone have a suggestion for this? Thanks beforehand!


@ali417 That looks like an array type... can you call seq on it?


I did and got nothing back


Is there a way to run another JVM a la lein trampoline with boot? I have a project that I run with trampoline because otherwise the shutdown hook doesn’t fire (I think lein intercepts the C-c or something). I’d like to port this to boot, but am not sure what the equivalent functionality is

Caio Guedes12:05:43

Guys! I'm very difficult to understand what is the Component and why I need use it. Could you help me with some articles or videos about it? thanks 😊


This, should probably help. It’s basically about breaking up your code in components. It takes a bit of work to setup right, but you get a lot back from it. For example it’s easy to use one database connection across different components, you can easily reload components, you can make a visual of how your system works, which is always up to date.


@caio.cesar.g.souza Also its worth noting that a component in the libraries context is something that has a start / stop lifecycle like a webserver, a websocket connection, a scheduler. It can be used for other stuff too, but thats its main point.

Caio Guedes17:05:10

@sveri thanks, this is a easy way to see when use it.


Questions: I have a function signature like this:

(defn create-websocket-connection
  [product_ids callbacks channels auth-params])
I want product_ids and callbacks to be mandatory, but channels to be optional as it will have a default value, and auth-params to be optional, as a non-authenticated connection is possible. Should I leave this as it is, or combine channels and auth-params into one map (`options`), in case someone wants to pass no channels (use default), but need to pass auth-params? That way they don't have to call it with nil in place of channels.


Logically, I don't feel that channels and auth-params need to be grouped together into one map, but with them both being optional, I'm not sure what is best here.


@brandon.ringe A common pattern is to include a opts parameter that takes a map value, so

(defn create-websocket-connection
  [product_ids callbacks opts])


@mfikes thanks, wasn't sure if something like that was standard, but common patterns are good, so I'll do that.


just for completeness, you can do default values for regular parameters by using arity overloading, like so:

=> (defn f
     ([a b] (f a b :default nil))
     ([a b c] (f a b c nil))
     ([a b c d] [a b c d]))
=> (f 1 2)
[1 2 :default nil]
=> (f 1 2 3)
[1 2 3 nil]
=> (f 1 2 3 4)
[1 2 3 4]


Thanks for the info. Btw I've read using variable arity is bad:

(defn create-websocket-connection
  [product_ids & [opts]]


What do you think?


Would it be better to require opts passed as an empty map if no options need to be specified?


That seems like a code smell to me


there's nothing wrong with having a function have multiple arities - many core functions do. that's different from variable arity, which means that it accepts any number of arguments


so you could define the function that takes the opts map to have an additional arity that passes the empty map for the function caller


=> (defn f ([] (f {})) ([opts] opts))
=> (f)
=> (f {:a 2})
{:a 2}
=> (f {:a 2} :wrong)

clojure.lang.ArityException: Wrong number of args (2) passed to: user/f


in this case, the function accepts either 0 or 1 arguments


if passed 0, it calls itself with an empty map as its 1st argument


try doing (source symbol) or (source +) for examples of core functions that have multiple arities


@brandon.ringe so that article is saying that it's a bad idea to use varargs (when you see & in a signature) for optional args. Varargs means something a bit different than multiple arity. You can pass any number of arguments to varargs. Multiple arity functions have multiple, but fixed, numbers of args


The guideline is to use multiple arity to express optional arguments, not varargs


Yes I understand the difference, I was specifically asking about varargs, sorry for not being clear. I see what you mean. Thanks! Refactor time...


Looking back at your original question, you could also use kwargs for that


Since you have two independent optional params


So the signature there might look something like, (defn create-websocket-connection [product-ids callbacks & [{:keys [channels auth-params] :or {channels your-default-channels}}])


Is that not also functioning as varargs though with that ampersand there? So it's varargs but just destructuring the first arg?


It would be called like, (create-websocket-connection product-ids callbacks :auth-params auth :channels some-channels) and you could omit either or both of the kwargs

👍 4

Crap I gave the wrong declaration. It should be like this, (defn create-websocket-connection [product-ids callbacks & {:keys [channels auth-params] :or {channels your-default-channels}})


I'm on a phone, mistake-prone ;)


Oh yeah I thought so lol