Fork me on GitHub
#beginners
<
2022-12-20
>
Flawless09:12:27

Hi all, I'm trying to figure out with bb and clojure-lsp. I'm just working on simple one-file script (without bb.edn), and now i'm trying to run LSP. LSP itself seems started normally, but, for example it can't find any definition. Maybe you can suggest me something to read about lsp and bb, because after googling for a while, I've got no understanding, should it work, if I use babashka deps inside my script but no deps.edn or not.

borkdude09:12:14

Find definition of what?

Flawless10:12:22

some function definition, local or from the library both.

Flawless10:12:00

I called lsp-find-definition

borkdude10:12:02

What does the squiggle on line 28 say?

borkdude10:12:16

can you should the output of clj-kondo --lint script.clj?

Flawless10:12:17

I see, it comes from lsp, not from kondo, kondo reports only unused requires (but flycheck don't), that's the err on line 28: 1 1 error syntax Can't parse /Users/flawless/projects/tools/convert_binance.bb, No matching clause: :unknown (lsp)

ericdallo11:12:36

That sounds like a outdated clojure-lsp, could you share/check lsp-clojure-server-info?

Flawless12:12:32

I updated clojure-lsp and the problem has gone. Actually I just had clojure lsp installed from incorrect tap. Thank you all.

đź‘Ť 1
ericdallo12:12:00

yeah, I'd love to remove the outdated clojure-lsp from the core brew but they don't want it :)

M J11:12:45

Hey, I have this hash-set selections (r/atom #{2 4 6}) How do I iterate over it and print each one alone?

MiÄ·elis Vindavs11:12:34

(doseq [x @selections] (println x))

M J11:12:47

I have this array:

multi-select-items (r/atom ({:value A :id 27069} {:value B :id 27070}))  
and this hash-set:
selections     (r/atom #{2 4 6 27069})
How do I return all values from "multi-select-items" that are in "selections"?

Ed11:12:35

if you mean where the :id is in the selections set, then this should work

(filter (comp @selections :id) @multi-select-items)

M J11:12:41

selections doesn't have :id in it, just a hash-set of numbers

Ed11:12:55

I mean the :id in the mulit-select-items atom. So for your example, I assume the result you want is a seq with only the first map in {:value A :id 27069}

Ed12:12:20

so filter takes a predicate and a sequence and returns a sequence containing only the elements where the predicate returns true. The predicate here is built by combining 2 functions with comp . The first function that comp will call is :id which will get the :id key from each map and pass it to the set in the selections atom which will look that value up in the set. If it's not there, it will return nil which is considered falsey by filter and the resulting seq will not include that value. Does that make sense?

M J12:12:40

Yeah I understand, it returns nil now since selections has no :id, if i has :id, which I tried, it returns what I want. So, what can I do?

Ed12:12:56

(let [multi-select-items (atom '({:value A :id 27069} {:value B :id 27070}))  
        selections         (atom #{2 4 6 27069})]
    (filter (comp @selections :id) @multi-select-items)) ;; => ({:value A, :id 27069})
this will return all the values in multi-select-items where the :id is in the selections set. I'm not sure what you mean ...

M J12:12:55

YES! Thanks alot 🙏

đź‘Ť 1
M J14:12:22

Hi, how can I only return :value in the filter?

Ed14:12:13

once you've filtered the list, you need to then map a transformation over that list. Maybe something like

(let [multi-select-items (atom '({:value A :id 27069} {:value B :id 27070}))  
        selections         (atom #{2 4 6 27069})]
    (->> (filter (comp @selections :id) @multi-select-items)
         (map :value))) ;; (A)
in this instance there's a shorthand function keep that you could also use, but this seems easiest to me.

M J14:12:14

Great! thanks

Muhammad Hamza Chippa14:12:33

how to pass props in hiccups something like this?

<Canvas
      direction="DOWN"
      nodes={nodes}
      edges={edges}
      // node={(props) => <CustomNode {...props} />}
      node= {function (props) {
        return <CustomNode {...props} />;
      }}
    />

const CustomNode = (props) => {
  return (
    <Node {...props} />)

dumrat16:12:03

Is there a way to do nested map destructuring? I can make do with this:

(let [{:keys [context]} {:context {:db "xxx"}}
      {:keys [db]} context]
  db)
Is there a way to express that shorter?

dpsutton16:12:16

(let [{{:keys [db]} :context} {:context {:db "xxx"}}]
  db)

clapping 1
dpsutton16:12:57

sometimes :keys can confuse you about how destructuring works. It is a bit of sugar. This might make it clearer how you can just use destructuring recursively

(let [{{thing :db} :context} {:context {:db "xxx"}}]
  thing)
the :context destructuring is itself destructuring, introducing the local thing for whatever is at the key :db inside of :context

dumrat16:12:16

Thanks @U11BV7MTK, the :keys destructuring does make sense.

dumrat16:12:02

@U11BV7MTK On second thought it is confusing.

dpsutton16:12:34

{:keys [foo bar]} is equivalent to destructuring {foo :foo bar :bar}

dpsutton16:12:33

the keyword you destructure at is bound to a local of the same name. but in general the pattern is {local :key}. And this can nest. {{thing :db} :context} as above, or {{:keys [db]} :context}

dumrat16:12:08

Yes, understood. I'm trying to think how keys destructuring might (Does it?) work with multiple keys instead of a single one at top level. Named destructuring makes more sense there I guess.

dpsutton16:12:41

what do you mean multiple keys? Give a structure you’d like to destructure

dumrat16:12:45

{:context {:year "2022" :user "dr"} :data {:plant "apple :birth "2019"}} Suppose I want to get year and plant and birth inside a let ?

dpsutton16:12:04

tcp=> (let [structure {:context {:year "2022" :user "dr"} :data {:plant "apple" :birth "2019"}}
            {{:keys [year]} :context {birth-local :birth} :data} structure]
        [birth-local year])
["2019" "2022"]

dumrat16:12:28

(let [{{year :year} :context {:keys [plant birth]} :data} m] ...)
makes sense. I'm not sure if it's possible to keys destructure at top level.

dpsutton16:12:56

there is no top level. it’s just destructuring at “this” level

dpsutton16:12:28

tcp=> (let [{foo :foo} {:foo "top level is just where you are"}]
        foo)
"top level is just where you are"

dpsutton16:12:32

and note we’ve been destructuring :context at the top level this whole time

đź‘Ť 1
skylize17:12:27

You can also interweave sequence destructuring.

(let [m {:foo [{:bar :baz} {:fiz :buz}]}
      {[_ {fiz :fiz} :as ms] :foo} m]
  {:fiz fiz
   :ms ms})
; =>
;; {:fiz :buz,
;;  :ms [{:bar :baz} {:fiz :buz}]}

skylize17:12:23

> keys destructuring might (Does it?) work with multiple keys instead of a single one at top level Are you trying indicate the :keys sugar here? I don't think that offers facilities for descent into nested maps. Just use the unsugared version.