Fork me on GitHub
#beginners
<
2022-02-06
>
theequalizer7302:02:24

Hi, is there a way to extract a function, which already has many references to it, to another namespace using Spacemacs or InterlliJ/cursive? I’m trying to find something in the cljr-refactor package in spacemacs but I can’t find anything.

Drew Verlee03:02:14

There is no limit to how many times a function can be required. Are you asking if there is a way for a editor function to add the require in some context?

Drew Verlee03:02:35

Several tools, lsp and cljr refactor, can do that.

theequalizer7310:02:57

I was trying to move a function to another namespace and wanted to know if lsp or cljr-refactor could do it. It happens that there is no way to do that yet.

Chase19:02:21

in a cljs ns I required [cljs.tools.reader :refer [read-string]] but I meant to do [cljs.reader :refer [read-string]] (without the tools part, not sure what the difference is) but I'm getting a repl error that read-string is now already used by the tools.reader invocation. How do I switch that out without restarting my repl?

Chase19:02:47

I believe that might be clojure only, not cljs

seancorfield19:02:33

Oh... Another difference between the dialects... one day I will learn cljs and share these frustrations I suspect...

seancorfield19:02:04

Hmm, so none of the reified namespace stuff exists in cljs? *ns* produces nil in the REPL...

Chase19:02:42

Yeah, I believe the cljs api for ns docs are here: https://cljs.github.io/api/cljs.core/#ns

seancorfield19:02:22

Ah, yeah, unmap is actually what you want there. Sorry.

seancorfield19:02:52

unalias would remove the :as part. unmap removes the :refer'd part.

Chase19:02:34

Unfortunately, it didn't seem to work for me. (ns-unmap 'cljs.tools.reader 'read-string) returns true but then I get the same error when trying to require [cljs.reader :refer [read-string]

Chase19:02:49

No biggie, I'll just restart

seancorfield19:02:22

You need to unmap it from the calling namespace, not from cljs.tools.reader.

seancorfield19:02:42

:refer creates a mapping into your ns, which you can unmap.

Chase19:02:28

I tried that but then (ns-unmap 'frontend.chat.api 'read-string) gives me a weird Use of undeclared Var frontend.chat.api/frontend error. Not sure where that last frontend part is coming from.

seancorfield19:02:31

Mind you, this seems strange to me:

(! 519)-> clj -Sdeps '{:deps {org.clojure/clojurescript {:mvn/version "RELEASE"}}}' -M -m cljs.main -r
ClojureScript 1.11.4
cljs.user=> (require '[cljs.tools.reader :refer [read-string]])
nil
cljs.user=> (require '[cljs.reader :refer [read-string]])
nil
cljs.user=> 
Why doesn't that second require fail?

seancorfield19:02:24

You may have more luck getting better cljs answers in #clojurescript

Chase19:02:46

yep, I appreciate ya trying.

seancorfield19:02:28

(for Clojure there's a clojure.tools.reader in a Contrib lib for Clojure, there is no clojure.reader, and read-string is already in clojure.core so it isn't even comparable)

Chase19:02:47

https://github.com/clojure/tools.reader/issues/10 doesn't seem to be too different. I'm just following along in an older book maybe and let myself get taken down the rabbit hole

John Bradens21:02:29

Is there somewhere I can see how clojure.core functions are defined? Like if I want to see how remove works, or something like that, can I look that up somewhere? I'm doing some exercises where I write my own functions, and I'd love to compare to the originals

andy.fingerhut21:02:51

In a REPL, you can type (source remove)

andy.fingerhut22:02:15

You can also git clone , then look in the source file core.clj (or any other source file)

practicalli-johnny15:02:25

If using https://clojuredocs.org/ to search for clojure.core function documentation, there is also a source code link that takes you directly to the source code of that function definition on GitHub

👍 1
sova-soars-the-sora22:02:05

is there an equivalent of the wrap-reload middleware from Ring available in pedestal ?

sova-soars-the-sora22:02:36

(just asked in the pedestal channel #pedestal)

Neil Barrett23:02:58

Hi everyone - this is my first time here. Where can I post a code snippet to get help?

dpsutton23:02:42

You're in the right place here in the #beginners channel

Neil Barrett23:02:33

Ok, here goes ...

(def a1 (atom nil))
(def a2 (atom nil))
(def a3 (atom nil))

(defn putp [c val]
  (let [p (promise)]
    (put! c val (fn [] (deliver p val) ))
    (println "realized?" (realized? p))
    @p
    (println "OK")  ))

(def c (chan))

(future
  (reset! a1 "about to put and block until take")
  (putp c 10)
  (reset! a2 "put succeeded"))

(take! c (fn [v] (reset! a3 v)))

(println "@a1:" @a1)
(println "@a2:" @a2)
(println "@a3:" @a3)
Why does @a2 return nil?

apiology23:02:18

FYI for next time - you can use ` on a line before and after a big block of text to quote it as a single block. It’s called a ‘code block’ - there’s also an icon in the editing toolbar for it on the far right.

phronmophobic00:02:55

the callback from put! takes a single argument

phronmophobic00:02:18

should be something like:

(defn putp [c val]
  (let [p (promise)]
    (put! c val (fn [_] (deliver p val) ))
    (println "realized?" (realized? p))
    @p
    (println "OK")  ))

phronmophobic00:02:06

when running your code, I saw:

Exception in thread "async-dispatch-1" clojure.lang.ArityException: Wrong number of args (1) passed to: example/putp/fn--15434
Did you see that error?

Neil Barrett00:02:50

Thanks a lot, Adrian - I wrote some comments in #beginners

phronmophobic00:02:51

I'm using emacs+cider. I'm less familiar with your editor, but it should show an error if there was one

phronmophobic00:02:03

the presentation talk you reference is using an alpha version of core.async from 2013, so I'm not that surprised that there's a slight difference

Neil Barrett00:02:51

Sure - I'm just happy to find a solution

dpsutton01:02:17

async=> (def a1 (atom nil))
#'metabase.test.util.async/a1
async=> (def a2 (atom nil))
#'metabase.test.util.async/a2
async=> (def a3 (atom nil))
#'metabase.test.util.async/a3
async=> (defn putp [c val]
          (let [p (promise)]
            (put! c val (fn [_] (deliver p val) ))
            (println "realized?" (realized? p))
            @p
            (println "OK")  ))
#'metabase.test.util.async/putp
async=> (def c (chan))
#'metabase.test.util.async/c
async=> (future
          (reset! a1 "about to put and block until take")
          (putp c 10)
          (reset! a2 "put succeeded"))
realized? false
#object[clojure.core$future_call$reify__8477
        "0x14d69935"
        {:status :pending, :val nil}]
async=> (take! c (fn [v] (reset! a3 v)))
OK
nil
async=> (println "@a1:" @a1)
@a1: about to put and block until take
nil
async=> (println "@a2:" @a2)
@a2: put succeeded
nil
async=> (println "@a3:" @a3)
@a3: 10
nil
This works for me

dpsutton01:02:15

note that it cannot print “OK” until you take! from the channel c since you are using an unbuffered channel

dpsutton01:02:36

also, i modified the function on the put! call to take a single (ignored) argument

dpsutton01:02:36

and if i leave that function not taking an argument i can recreate your issue. But I see the following: > Exception in thread “async-dispatch-6” clojure.lang.ArityException: Wrong number of args (1) passed to: metabase.test.util.async/putp/fn--114165

bbss02:02:08

The reason is probably that you are running the code as a "script" in one go, which terminates before the future ran. You can deref the future for that not to happen I think. As you can see @U11BV7MTK ran the code in a repl, step by step, in a process that keeps running.

dpsutton03:02:48

When running as a main file i get the same behavior: it works when the callback function takes a parameter and errors when that argument is left out

👍 1
Neil Barrett07:02:49

Thanks dpsutton!

Neil Barrett23:02:48

Also, "OK" is not printed.