Fork me on GitHub
#beginners
<
2022-06-07
>
Ian Fernandez03:06:44

how can I get a var defined in a ns and create a keyword from this var?

Ian Fernandez03:06:17

(def a 1) #'user/a

Ian Fernandez03:06:33

(some-func a) => :user/a

Ian Fernandez03:06:44

what is this some-func? this func exists?

phronmophobic03:06:54

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

thanks3 1
phronmophobic03:06:24

Note that if you wanted a function to do this, you would have to call it like (some-func #'a).

Ian Fernandez03:06:17

Basically this is tricky since I want to form namespaced keywords from some vars on a namespace

Ian Fernandez03:06:43

I want to select some vars defined in a ns and form keywords with it

phronmophobic03:06:02

What are you going to do with the keywords?

Ian Fernandez03:06:20

use then in another namespace to call a function

Ian Fernandez03:06:28

in *ns* => a I want to define some vars and a function

Alex Miller (Clojure team)03:06:48

why don't you just use the vars? why the keywords?

phronmophobic03:06:04

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.

Ian Fernandez03:06:09

I think I'm going too far with abstractions and forgot the starting point hahahah

šŸ˜ 1
Jon Olick03:06:40

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})

phronmophobic03:06:18

https://clojure.org/reference/data_structures#Sets > Sets are functions of their members, using get: >

(s :b)
> -> :b
> 
> (s :k)
> -> nil

Martin PÅÆda04:06:14

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 .

Jon Olick15:06:33

so does it grab like the first element of the set and then like call get with it?

Jon Olick15:06:44

not clear how that works

andy.fingerhut15:06:35

(some-set x) behaves at least fairly similarly to (get some-set x) (I do not have memorized whether there are any differences)

andy.fingerhut16:06:05

i.e. It returns x if the value is in the set, otherwise nil

Jon Olick16:06:51

basically its like a is there anything in there thats also in this

Jon Olick16:06:03

or in this case

Jon Olick16:06:19

iterate over the second argument, excluding things in the first set

Jon Olick16:06:27

easy-peasy, thanks! šŸ™‚

andy.fingerhut16:06:47

I don't see how iteration comes into it. (get some-set x) checks whether one value is in the set, or not.

Jon Olick16:06:39

in the filter command

andy.fingerhut16:06:15

Ah, sorry, I was forgetting your original question and focusing on the (some-set x) expression. My fault.

popeye08:06:13

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?

Ben Sless08:06:17

Why do you care about its size? There's some hidden assumption here

popeye09:06:59

@UK0810AQ2 My intention is, if the data in that variable exceeds 1MB i wanted to download the value as file format

Ben Sless09:06:58

where is the data coming from to begin with?

popeye09:06:27

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

Ben Sless09:06:54

are you on Clojure or ClojureScript?

popeye09:06:11

clojure from backend and clojurescript from frontend

Ben Sless09:06:21

and the check is in backend?

Ben Sless09:06:07

Why not use java.nio.file.Files.size(path)?

popeye09:06:29

In my scenario cannot check the file size , Data will be filtered from logic and those data size should be validated

Ben Sless09:06:41

And do you care about the size of the objects in memory or the size of text you'll be sending to the frontend?

popeye09:06:38

size of the objects in the memory

Ben Sless09:06:38

okay, so clj-memory-meter might suite your needs

popeye09:06:34

it is for jdk more than 9

emccue11:06:37

@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?

emccue11:06:57

its not 100% accurate, but its good enough for your heuristic

emccue11:06:06

and when you return the response indicating how it should be downloaded, use a ByteArrayInputStream

popeye08:06:23

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=> 

emccue14:06:38

you can ignore the keywords and tree walk values and sum them up? I was thinking you had just one string

dumrat11:06:31

Anyone has a sample of a reitit router usage in a clojurescript project?

Ferdinand Beyer11:06:23

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

šŸ‘ 1
Dad-Shoes11:06:44

@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.

šŸ‘ 1
Jim Strieter13:06:13

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!

delaguardo13:06:52

would help a lot to see actual error

Max13:06:54

what is the error?

delaguardo13:06:58

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 declaration

šŸ™Œ 3
Jim Strieter14:06:57

that worked. Thank you!

Ferdinand Beyer15:06:06

ā€œSyntaxā€ error šŸ™‚ (Arenā€™t LISPs proud for the lack of syntax?)

delaguardo15:06:58

I would say minimalistic instead of "lack of" :)

kennytilton16:06:33

"Calling a provisional deficit a syntax error does not make it a syntax error." - Abe Lincoln

dumrat14:06:00

What's the cause of error like

"<A /> is using incorrect casing..."
Can't see any issue in the generated output

Bramhesh Srivastav14:06:42

hallo group, another noob happy to be here.

šŸ‘‹ 5
šŸ£ 1
Max14:06:23

welcome! we're glad to have you! šŸŽ‰

Bramhesh Srivastav14:06:50

Thanks! 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. šŸ™‚

šŸ’œ 1
Jon Olick15:06:02

how do you break out of an infinite doseq?

bronsa14:06:56

doseq has a :while clause

bronsa14:06:32

user=> (doseq [x (range) :while (< x 2)] (println x))
0
1
nil

Jon Olick15:06:35

looking for something kind of like reduced

leonoel15:06:44

Use run!. It relies on reduce, therefore it supports reduced :

(run! (fn [x]
        (if (< x 10)
          (prn x)
          (reduced nil)))
  (range))

sheluchin15:06:39

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?

dpsutton15:06:39

(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})

dpsutton15:06:03

you can us ns-aliases to get the aliases and then walk the structure

sheluchin15:06:49

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..

dpsutton15:06:00

no there is not

dpsutton15:06:28

the reader expands ::alias/name. itā€™s just sugar and doesnā€™t exist as a value

sheluchin16:06:32

I see. Is that walk namespace important to become familiar with? Should I put some time into studying it?

Jon Olick15:06:20

I'll just use loop/recur instead I guess

Jim Strieter20:06:56

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}

Alex Miller (Clojure team)20:06:07

or (assoc {:a 0 :b 0 :c 0} :c 42) is more natural if you don't have the replacement already in a map

Alex Miller (Clojure team)20:06:34

and merge is another option (but prob slower for this)

Jim Strieter20:06:37

I had no idea that conj will do that. Thanks! šŸ™‚

Michaƫl Salihi09:06:45

Me too! TIL that conj can be used instead of merge in this case. Thx @U064X3EF3!

Jon Olick21:06:53

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 ?

dpsutton21:06:34

(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

Jon Olick21:06:28

yeah thats the fundamental question here, what does that format mean?

Jon Olick21:06:45

map would have to implement ifn, which would do what exactly?

dpsutton21:06:18

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

dpsutton21:06:37

({:a 1} :a) -> 1

Jon Olick21:06:37

What if it was a hash map and a hash map

Jon Olick21:06:46

I already get the keyword thing

Jon Olick21:06:03

Does it like intersect the structures?

dpsutton21:06:31

no. it just behaves as a lookup

dpsutton21:06:02

thereā€™s nothing special about keyswords. it is just looking for the argument in the hashmap

Jon Olick21:06:36

oh so it would like look for a key that exactly matches the other hash map's key

Jon Olick21:06:40

as if it was a key

Jon Olick21:06:02

which is different than set, where set would like intersect them or something

Jon Olick21:06:13

(so confusing the different ways to do everything)

Jon Olick21:06:28

sometimes it feels like it lacks consistency for the sake of succientness

dpsutton21:06:54

what consistency is lacking?

Jon Olick21:06:17

well (set-a set-b) does an intersection of the sets

Jon Olick21:06:22

but doing the same with a hash map does not

dpsutton21:06:40

it does not do that

dpsutton21:06:49

(#{:a} #{:b}) is a membership check and returns nil

dpsutton21:06:02

because the set #{:b} is not a member of the set containing :a

Jon Olick21:06:05

membership, intersection, tomato tomaato

Jon Olick21:06:42

(is (= (remove #{:a} #{:b :c :d :a :e}) (:e :c :b :d)))

Jon Olick21:06:12

maybe not the best example

Jon Olick21:06:17

but syntax is confusing

dpsutton21:06:11

understandable. break things down and work in a repl. read docstrings and ask questions here šŸ™‚

dpsutton21:06:35

do you want to work through the logic of (remove #{:a} coll)?

Jon Olick21:06:56

maybe a bit later šŸ™‚ busy atm with some work stuff

kennytilton00:06:27

(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.

2FO22:06:39

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"}}}

dpsutton22:06:33

{:paths ["src/main/clojure"]} just remove it

Alex Miller (Clojure team)22:06:47

That's not the path to it

Alex Miller (Clojure team)22:06:05

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

dpsutton22:06:11

oh thank you alex. I had forgotten about installing dev builds. sorry @U01HBARPCFL i missed a subtlety of your question

2FO22:06:14

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.

dpsutton22:06:04

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.

2FO23:06:44

Got it, thanks. I have managed to remove rum following @U064X3EF3 instructions without issue

Alex Miller (Clojure team)23:06:54

Just so you know, most people typically do not manually remove libs from their maven local repo, they just let them accumulate

Alex Miller (Clojure team)23:06:40

Clojure jars are zip files of Clojure source so are typically quite small