This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-02-12
Channels
- # aleph (22)
- # aws (7)
- # babashka (17)
- # beginners (69)
- # chlorine-clover (9)
- # cider (2)
- # clj-kondo (3)
- # cljdoc (30)
- # clojure (113)
- # clojure-dev (30)
- # clojure-europe (11)
- # clojure-italy (2)
- # clojure-nl (16)
- # clojure-spec (1)
- # clojure-sweden (3)
- # clojure-uk (17)
- # clojurescript (77)
- # cryogen (12)
- # data-science (5)
- # datomic (27)
- # duct (2)
- # emacs (37)
- # fulcro (24)
- # graphql (2)
- # kaocha (1)
- # lambdaisland (27)
- # leiningen (4)
- # off-topic (15)
- # onyx (1)
- # other-lisps (3)
- # re-frame (94)
- # reagent (2)
- # reitit (20)
- # ring (1)
- # shadow-cljs (66)
- # spacemacs (5)
- # sql (59)
- # tools-deps (140)
- # vim (1)
- # xtdb (17)
A quick question about java interop, say I have a method that returns its results into one of its args, how do I call this via java interop? eg.
//java
obj.transform(src, result)
I assume the object used to return results probably has a getter method for the result info, is that right? if so you would create the object and pass it, then use the getter method to get the result info. So just normal Java interop (object creation and method calling) would be needed.
hey, as I keep working with a namespace in the repl I rename functions, etc, eventually the namespace in the repl gets really polluted with functions that don't exist anymore in the source file, etc... I was wondering what's a nice way to reset/recreate the namespace, without having to restart the repl, if there's one. Thanks!
I have not personally used it much, so I don't have wealth of experience to tell you any known possible pitfalls or warnings about using this, but there is remove-ns
Hmmm I found cider-refresh, although I'm not exactly sure what it does yet.
e.g. I don't know what happens if you remove-ns
while one or more other namespaces have been loaded, that refer to names in the removed ns
I tried cider-refresh
and it worked nicely... but I'm not sure what it does exactly. I'd have to go through the docs [*] more carefully really soon 😄
*: https://docs.cider.mx/cider/usage/misc_features.html#_reloading_code
There is a #cider channel here where you can ask about that, if you cannot find docs, and/or the source code you find not easy to follow (I have not looked at it to know how it works)
gotcha thx! right that would be a better place to ask about cider specifics
@UCFTL4UQP cider-refresh basically uses tools.namespace refresh: https://github.com/clojure/tools.namespace#reloading-code-motivation It's quite heavy tool to refresh a whole project. (As Andy suggested) you may use
(remove-ns *ns*)
but that has some corner cases like when other ns-es depend on that namespace (then you might need to remove and reload them too )cool thx! since I'm working with a super small test project for now seems like the overhead is not that much
I have a hotkey bound to remove-ns
(and a hotkey bound to load-file
) so if my current ns gets "stale" like that -- and is actually a problem (not all staleness is) -- then two key chords gets it back to being fresh.
That's with Atom/Chlorine but it should be easy enough to program that up with Emacs too.
(and I'll note that I don't use that remove-ns
command very often -- and I have no other "refresh/reload" aspects to my workflow at all)
Using cider-undef`` before deleting a def or defn ensures you dont pollute you namespace. I added
, e u
in spacemacs as I found this function so useful, especially for tests
Hello everyone. I had a small doubt: I tried to create a function with optional arguments. In my head, i remebered that i need to declair this way : (defn foo [val1 & val2])
and it did not worked as i expected. Did i miss something?
Clojure(Script) fns inherently don't have a concept of "optional arguments". What they do have, is multiple-arities, including variadic ones. Meaning one fn may take any number of arguments. What you do, when your fn gets called with n arguments for any given value of n, is completely up to you.
What you defined here is a fn foo
that takes 1 or more than 1 arguments. In the fn body, the first arg is available with the name val1
, and the rest of the arguments are available with the name val2
.
Example: (defn add-arguments [& args] (apply + args))
Thank you friend
@ramon.rios What about (defn foo [val1 &val2] ())
?
Instead of (defn foo [val1 & val2])
you probably want to name the last arg something else, for example (defn foo [val1 & more])
. The arg after &
is a sequence of the remaining args, not a single arg.
Were you expecting it to be a single arg?
If you're asking for help, say what you tried, and what your expectations were, what you did to validate your expectations, and what happened when you validated those expectations
i truly sorry not respond at the time, i was in a sprint planning that took a long time. So i'll check the folks suggestions.
On my case, i have a function wich need to have 1 optional argument
What i tried to do is the code that i put in my first message, but i had arity issues
I was thinking about using multiple arity for solve this
but i remembered that i had this option, so that's why i asked.
@ramon.rios If you just want a single argument to be optional:
(defn foo [val1 & [val2]] ...)
then you can check if val2
is truthy without having to pick apart a sequence of "additional arguments"Caveats: you can still pass more that 2 args to foo
as written; you can't tell the difference between val2
being passed nil
explicitly versus not being present.
(you can handle val2
being false
by testing (some? val2)
if that's a use case you need)
Or you can use multiple arities:
(defn foo
([val1] ... do stuff without val2)
([val1 val2] ... do stuff with val2))
now you can only pass one or two arguments, not more, and that first arity could just call (foo val1 nil)
if nil
is a suitable default value, much like the &
version above.( @heatonsam ) This is the place
Thank you! I'm doing something right now where I modify the values of a hash-map that's contained with other hash-maps in a seq. I do this by filtering by the value of a specific key to get the correct element. Of course by doing so I am returned only that element in the seq and lose the rest of the hash-maps. Example that is more or less what I'm doing:
(defn edit-element-by-id
[id query-parameters data-from-file]
(let [element (filter #(= (:vector %) id) data-from-file)]
(let [args (walk/keywordize-keys query-parameters)]
(map #(merge % (select-keys args (keys %))) element))))
However, what I really want is to get back the entire seq of hash-maps, with the element in question and its values changed.. I know I should be thinking bigger, but I've hit a slight conceptual roadblock...It's going to be called in a PUT request, I just separated out from the :body of the function.
I was originally trying to do this using update
but I had trouble; I'll try update-in.
Oh yeah, what form would you use and why?
well, from your example you could make a map of {(:vector m) m}, which would make it very easy to update the one you care about
the kind of ultimate end of that approach is to flatten all your data out and basically build a little in memory relational database, but there are a number of stops along the way between using a seq and that
a seq is basically the worst(lowest common denominator) datastructure, so structuring your data some other way is pretty much always going to give you more power
even just making it a vector would allow you to assoc in a new updated version of the map if you know the index
I'm not sure I understand. data-from-file (which was originally from csv, but I dealt with all that crap...) looks like:
({:vector 1
:name aname
:otherfield other}
{:vector 2
:name othername
:otherfield nother}
...)
So what you are saying is to make the outer data structure also a map, and use the id as the key for each element?
yes - you can do this directly with group-by
In my code example ":vector" is the name used for the primary key in the godawful csv file provided by my professor, so sorry if that causes confusion.
Ah ok, that makes sense. Then I can select the element easily
Thank you!
user=> (group-by :vector [{:vector 0 :item :a} {:vector 1 :item :b} {:vector 0 :item :c}])
{0 [{:vector 0, :item :a} {:vector 0, :item :c}], 1 [{:vector 1, :item :b}]}
group-by does add extra structureone thing to mind is this approach loses your original order - you can use map-indexed
with assoc to capture that as data if needed
Ok. The order of the outer map doesn't matter, just within each element in this case. I'm going to play around with these ideas - helps a lot.
hash isn't really meant to be a checksum, what are you trying to do?
calculate a simple checksum for arbitrary clojure datastructures and save it to disk to be loaded and compared later. It doesn't need to verify authenticity.