This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-05-12
Channels
- # beginners (40)
- # boot (2)
- # cider (16)
- # clara (2)
- # cljs-experience (2)
- # clojure (100)
- # clojure-italy (11)
- # clojure-russia (2)
- # clojure-spec (9)
- # clojure-uk (34)
- # clojurescript (36)
- # community-development (3)
- # core-async (4)
- # cursive (4)
- # datascript (1)
- # datomic (20)
- # duct (10)
- # emacs (1)
- # fulcro (2)
- # off-topic (40)
- # onyx (12)
- # overtone (8)
- # portkey (6)
- # quil (1)
- # re-frame (7)
- # ring-swagger (2)
- # shadow-cljs (2)
- # spacemacs (2)
- # test200 (10)
- # tools-deps (16)
- # unrepl (1)
- # vim (24)
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. 😬
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?
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
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, https://github.com/stuartsierra/component 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.
thanks @gklijs!
@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.
@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]))
#'boot.user/f
=> (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]]
Would it be better to require opts passed as an empty map if no options need to be specified?
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))
#'boot.user/f
=> (f)
{}
=> (f {:a 2})
{:a 2}
=> (f {:a 2} :wrong)
clojure.lang.ArityException: Wrong number of args (2) passed to: user/f
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
Yes I understand the difference, I was specifically asking about varargs, sorry for not being clear. I see what you mean. Thanks! Refactor time...
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