This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-06-07
Channels
- # announcements (2)
- # asami (2)
- # babashka (15)
- # babashka-sci-dev (31)
- # beginners (130)
- # boot (4)
- # cider (5)
- # circleci (12)
- # clj-kondo (10)
- # cljs-dev (8)
- # clojure (7)
- # clojure-czech (14)
- # clojure-europe (19)
- # clojure-france (5)
- # clojure-uk (2)
- # clojured (23)
- # clojurescript (11)
- # conjure (8)
- # datomic (5)
- # emacs (1)
- # etaoin (8)
- # events (2)
- # fulcro (10)
- # graalvm (18)
- # gratitude (1)
- # holy-lambda (16)
- # honeysql (4)
- # introduce-yourself (1)
- # jobs (2)
- # kaocha (3)
- # london-clojurians (1)
- # lsp (53)
- # off-topic (16)
- # other-languages (2)
- # pathom (4)
- # pedestal (3)
- # podcasts-discuss (1)
- # portal (10)
- # re-frame (69)
- # reitit (2)
- # shadow-cljs (11)
- # vim (7)
- # xtdb (29)
how can I get a var defined in a ns and create a keyword from this var?
(def a 1) #'user/a
(some-func a) => :user/a
what is this some-func? this func exists?
there's a couple tricky points to this question.
(def a 1)
creates a var with the value 1
when (some-func a)
is evaluated, it will call some-func
and pass the value of user/a
and not the var itself.
To get the var itself, you need to use the special form (var a)
which is almost always abbreviated using the special syntax #'a
You can get information about the var by examining its metadata (ie. (meta #'a)
).
You create a keyword from a var by doing something like:
user> (def a 42)
#'user/a
user> (let [m (meta #'a)
ns (:ns m)
nm (:name m)]
(keyword (str (ns-name ns))
(str nm)))
:user/a
Note that if you wanted a function to do this, you would have to call it like (some-func #'a)
.
Basically this is tricky since I want to form namespaced keywords from some vars on a namespace
I want to select some vars defined in a ns and form keywords with it
What are you going to do with the keywords?
use then in another namespace to call a function
in *ns* => a
I want to define some vars and a function
why don't you just use the vars? why the keywords?
Based on your question, it sounds like there's a way to do what you're trying to do, but it also sounds like what you're trying to do might not be the best approach. More info about your use case and an example might help.
I think I'm going too far with abstractions and forgot the starting point hahahah
with the following, its doing a (filter (complement #{:a}) #{:b :c :d :a :e}) from what I can tellā¦ but how is #{:a} a function? thats a set?
(remove #{:a} #{:b :c :d :a :e})
https://clojure.org/reference/data_structures#Sets > Sets are functions of their members, using get: >
(s :b)
> -> :b
>
> (s :k)
> -> nil
You can use as function any object that implements IFn (https://clojuredocs.org/clojure.core/ifn_q)- that means sets, vectors, hash maps, keywords, symbols or anything created with fn
.
(some-set x)
behaves at least fairly similarly to (get some-set x)
(I do not have memorized whether there are any differences)
i.e. It returns x
if the value is in the set, otherwise nil
I don't see how iteration comes into it. (get some-set x)
checks whether one value is in the set, or not.
Ah, sorry, I was forgetting your original question and focusing on the (some-set x)
expression. My fault.
I have stored a chunk of collection in variable temp, How to identify the size of the variable in clojure? like 1mb or 2 mb?
@UK0810AQ2 My intention is, if the data in that variable exceeds 1MB i wanted to download the value as file format
I am reading those data from a file, if data size is 1 MB then i will convert that to file, else I want to display the contenet in the front end
In my scenario cannot check the file size , Data will be filtered from logic and those data size should be validated
And do you care about the size of the objects in memory or the size of text you'll be sending to the frontend?
@U01J3DB39R6 if you are buffering it all in memory anyways, why not just load it as a byte array and then use .length
on it?
and when you return the response indicating how it should be downloaded, use a ByteArrayInputStream
tried this
user=> (.getBytes {:a "a"})
Execution error (IllegalArgumentException) at user/eval38 (REPL:1).
No matching field found: getBytes for class clojure.lang.PersistentArrayMap
user=>
you can ignore the keywords and tree walk values and sum them up? I was thinking you had just one string
Have you checked out the official examples in the reitit repo? There are 6 frontend example projects: https://github.com/metosin/reitit/tree/master/examples
@U031CHTGX1T thank you for pointing out that there were 6 of them. I was using just the frontend example and was a little confused. I just hopped in the frontend-prompt and it got me where I need to be.
Does anyone know why lein test would complain about a syntax error even though requiring the same file in repl does not?
(defn update-strikes
[state]
(if (:last-bar-in-day state)
;; If this is last bar of the day, give every ticker an empty set:
(assoc state :strikes (map-kv (fn [x] #{}) (:spot state)))
(if (not (:first-bar-of-day state))
;; If this is NOT the first bar of the day, simply return state:
state
;; If this *is* first bar of day, add stuff to set:
(loop [ticker-list (:ticker-list (:parameters state))
state' state]
(if (empty? ticker-list)
state'
(recur
(rest ticker-list)
(assoc-in
state'
[:strikes (first ticker-list)]
(let [old-set (get-in state' [:strikes (first ticker-list)])
new-set (clojure.set/union old-set (make-strike-set state' (first ticker-list)))] ;; <----- lein test complains error is here
new-set))))))))
I load this function at REPL and use it just fine.
Only lein test complains there is an error!would help a lot to see actual error
I think I know. the error probably looks similar to this:
Syntax error (ClassNotFoundException) compiling at (foo/core_test.clj:7:16).
clojure.set
Full report at:
/var/folders/8r/3952rpsd74bg9775v8qvfg3h0000gn/T/clojure-12853718471848133743.edn
Tests failed.
add clojure.set
into the list of :require in namespace declarationthat worked. Thank you!
āSyntaxā error š (Arenāt LISPs proud for the lack of syntax?)
I would say minimalistic instead of "lack of" :)
"Calling a provisional deficit a syntax error does not make it a syntax error." - Abe Lincoln
What's the cause of error like
"<A /> is using incorrect casing..."
Can't see any issue in the generated outputThanks! i am working on some clojure script off youtube videos and also looking at crux db. Couldn;t set up datomic local. Have also watched many Rich Hickey talks about a dozen times each. š
Use run!
. It relies on reduce
, therefore it supports reduced
:
(run! (fn [x]
(if (< x 10)
(prn x)
(reduced nil)))
(range))
you don't
If I have a data structure with a bunch of namespaced keywords, and aliases established for some of those keywords, is there a way to print out that data structure with the aliases applied to the keywords?
(def namespace->alias
(set/map-invert (update-vals (ns-aliases *ns*) ns-name)))
(walk/postwalk (fn [form]
(if-let [new-ns (and (keyword? form)
(some->> form namespace symbol namespace->alias))]
(keyword (str ":" new-ns) (name form))
form))
{::io/a {::set/b [::io/c ::set/d]}
::io/e ::set/f
:unaliased :unaliased})
Thanks @U11BV7MTK. I haven't used walk
before. That's going to take some dissection.
Is there not some sort of printer setting or something like that which can control this without having to manually walk the code? Not sure "printer setting" is the right term here..
I see. Is that walk
namespace important to become familiar with? Should I put some time into studying it?
What is an efficient way of using all the values in one map to update another? Supposing I start with:
{:a 0 :b 0 :c 0}
what should some-func be so that this:
(some-func {:a 0 :b 0 :c 0} {:c 42})
evaluates to this:
{:a 0 :b 0 :c 42}
or (assoc {:a 0 :b 0 :c 0} :c 42)
is more natural if you don't have the replacement already in a map
and merge
is another option (but prob slower for this)
I had no idea that conj will do that. Thanks! š
Me too! TIL that conj
can be used instead of merge
in this case. Thx @U064X3EF3!
so if I do (map {:this :that} list-of-maps-of-this-that-and-others) will it return a sub-list of maps of just :this and :that ?
(map f [x y z])
will return a lazy list of ( (f x) (f y) (f z) )
. a map of {:this :that}
when invoked as a function will perform a lookup on the argument. Hard to parse what ālist-of-maps-of-this-that-and-othersā is but your question can be answered by thinking about how ({:this :that} <arg>)
behaves
terminology gets a big ambiguous here for āmapā. iāll use the term hashmaps to disambiguate. hashmaps do implement IFn
and it behaves as a lookup
@U036UA9LZSQ, have you read through https://clojure.org/reference/data_structures ?
thereās nothing special about keyswords. it is just looking for the argument in the hashmap
understandable. break things down and work in a repl. read docstrings and ask questions here š
(so confusing the different ways to do everything)
I feel your pain, @U036UA9LZSQ, and coming from a Common Lisp background I agree. CL also has lots of ways of doing things, but not the odd syntax tricks where, eg, data structures become functions.
Hang in there, follow Dan's advice, and you will soon get past this surface issue and then enjoy the succinctness.
Good day,
How can I uninstall a dep
that has been installed with CLI tools?
e.g uninstall rum
{:paths ["src/main/clojure"]
:deps {rum/rum {:mvn/version "0.12.9"}}}
That's not the path to it
Usually it will be in -/.m2/repository/rum/rum - if you want you can delete that directory, but note that any cached classpaths may refer to it (if you encounter an issue with this, use clj -Sforce ā¦
to force a cached classpath to recompute
oh thank you alex. I had forgotten about installing dev builds. sorry @U01HBARPCFL i missed a subtlety of your question
No problem at all @U11BV7MTK. I didn't know that :paths
was directly related to deps. That path was copied from a CLI tools tutorial unrelated to rum
. Thanks @U064X3EF3 I'll give that a go.
paths are not related to deps. My thinking was that you had started a project and had your sources at src/main/clojure
and wanted a dependency on rum so you added that. And if thatās the case you can just remove the declared dependency on rum by dropping it from your :deps
map.
Got it, thanks. I have managed to remove rum
following @U064X3EF3 instructions without issue
Just so you know, most people typically do not manually remove libs from their maven local repo, they just let them accumulate
Clojure jars are zip files of Clojure source so are typically quite small