Fork me on GitHub
#beginners
<
2020-09-10
>
dharrigan06:09:54

@alexmiller Hi, thanks for the update. Yes, just the line at the bottom should read the root value has already been reset back to 42, potentially causing a race condition instead of the root value has already been reset back to 0, causing a race condition

altjsus06:09:54

Good day everyone. I'm writing gui application with cljfx and it works on multimethod. I have the following structure.

(defmulti handle ::event)
(defmethod handle ::analyze (…))
(defmethod handle ::try-load-file (…))
And I want to catch all the exepctions of my method in a multimethod and show an alert window with exception info. But the first question is how can I do that in general. It's got to be something like this:
(defmulti handle
  (fn [event]
    (try (event)
         (catch Exception e (str e)))))
Although it doesn't works because event is passed as something evaluated already.

altjsus07:09:31

A little help?

altjsus07:09:08

Or the multimethod can be bad idea in given case. If some correction would be given - it would help a lot

didibus07:09:18

@altjsus I'm not sure what you're trying to do

didibus07:09:35

You want to catch exceptions that your handle methods could throw?

didibus07:09:25

If so, I would do this:

(defn try-handle [...]
  (try
    (handle ...)
    (catch (Exception e) (str e))))

(defmulti handle ::event)
(defmethod handle ::analyze (...))
(defmethod handle ::load-file (...))

didibus07:09:05

And I'd have your callers call try-handle instead of handle

baptiste-from-paris07:09:16

IMO, you don’t want to throw or catch exception in the dispatch fn as it will try to find a dispatch method. Even with your str it will throw an exception (NullPointerEx) If you look at MultiFn.java from clojure source code you’ll find :

public Object invoke(Object arg1) {
	return getFn(dispatchFn.invoke(arg1)).invoke(Util.ret1(arg1,arg1=null));
}

baptiste-from-paris07:09:08

you could have a default value for your dispatch and handle exception on a per-method basis

(defmulti yeahhh (fn [o] o))

  (defmethod yeahhh :default [event] (pr "called when no defmethod was found"))
  (defmethod yeahhh :a [event]
    (try (throw (ex-message "Ex"))
         (catch Exception e (str "Handled") )))

  (yeahhh :a)

baptiste-from-paris07:09:45

which means that the dispatchFn has to find some defmethod to call invoke again

mail.acharyarahul.now07:09:12

Hi, I'm looking for a code-flow visualizer for clojure. Something similar to http://www.pythontutor.com/visualize.html#mode=edithttp://www.pythontutor.com/visualize.html#mode=edit. Any pointers would be much appreciated. Thanks!

didibus07:09:17

Hum, I don't know of any web based ones, but Emacs, Cursive and I believe VSCode with Calva all have debuggers that allow you to step through code and see the value of every variable and the result of every evaluation as you step through the code

didibus07:09:34

There's some library also that do this a bit more "manual" I think they normally refer to this as tracing, for example https://github.com/clojure/tools.trace

didibus07:09:30

Here's the guide for VSCode with Calva: https://calva.io/debugger/

mail.acharyarahul.now07:09:40

I'm primarily looking for something web based where I can trace the code flow line by line. The only visualizer I have been able to find so far is a block-visualizer - http://lincoln-b.com/blocks/ I did install Calva just a while ago. Will go through it and will check out the other libraries as well. Thank you!

didibus07:09:43

I don't know of a web based one. Could be an interesting project!

didibus07:09:34

Hum, its not ideal, but since your link supports JavaScript. You can first convert the ClojureScript code to JS with this: http://app.klipse.tech/ and then paste the JS in your python tutor link

mail.acharyarahul.now08:09:34

I have Clojure code actually. Haven't explored ClojureScript yet.

altjsus08:09:31

Are there anyone who has some experience with cljfx?

jr0cket08:09:22

The #cljfx channel seems active

timofey.sitnikov11:09:14

Good morning Clojurians. I am trying to figure out how to serve static files based on this document: https://cljdoc.org/d/metosin/reitit/0.3.10/doc/ring/static-resources. Here is the code I have

(ns sandbox.main
  (:require [reitit.ring :as ring]
            [ring.adapter.jetty :as jetty]))

(def app
  (ring/ring-handler
    (ring/router
      ["/ping" (constantly {:status 200, :body "pong"})])
    (ring/routes
      (ring/create-resource-handler {:path "/" :root "public"})
      (ring/create-default-handler))))

(defn start []
  (jetty/run-jetty #'app {:port 3000, :join? false})
  (println "server running in port 3000")) 
I have placed an index.html file under project_root/public , but I keep getting HTTP/1.1 404 Not Found What can i be doing wrong? Below is the output, notice that the ping request gets the pong reply.
[I] /home/sporty/clojure~> http localhost:3000/ping
HTTP/1.1 200 OK
Content-Length: 4
Date: Thu, 10 Sep 2020 11:14:24 GMT
Server: Jetty(9.4.28.v20200408)

pong

[I] /home/sporty/clojure~> http localhost:3000/index.html
HTTP/1.1 404 Not Found
Content-Length: 0
Date: Thu, 10 Sep 2020 11:14:30 GMT
Server: Jetty(9.4.28.v20200408)

timofey.sitnikov11:09:14

Good morning Clojurians. I am trying to figure out how to serve static files based on this document: https://cljdoc.org/d/metosin/reitit/0.3.10/doc/ring/static-resources. Here is the code I have

(ns sandbox.main
  (:require [reitit.ring :as ring]
            [ring.adapter.jetty :as jetty]))

(def app
  (ring/ring-handler
    (ring/router
      ["/ping" (constantly {:status 200, :body "pong"})])
    (ring/routes
      (ring/create-resource-handler {:path "/" :root "public"})
      (ring/create-default-handler))))

(defn start []
  (jetty/run-jetty #'app {:port 3000, :join? false})
  (println "server running in port 3000")) 
I have placed an index.html file under project_root/public , but I keep getting HTTP/1.1 404 Not Found What can i be doing wrong? Below is the output, notice that the ping request gets the pong reply.
[I] /home/sporty/clojure~> http localhost:3000/ping
HTTP/1.1 200 OK
Content-Length: 4
Date: Thu, 10 Sep 2020 11:14:24 GMT
Server: Jetty(9.4.28.v20200408)

pong

[I] /home/sporty/clojure~> http localhost:3000/index.html
HTTP/1.1 404 Not Found
Content-Length: 0
Date: Thu, 10 Sep 2020 11:14:30 GMT
Server: Jetty(9.4.28.v20200408)

jason35811:09:22

I don't know much about ring, but shouldn't it be project_root/resources/public ?

timofey.sitnikov11:09:47

I tried the following locations:

project_root/public
project_root/resources/public
project_root/src/resources/public

teodorlu11:09:33

I recently struggled with the arguments to the Compojure equivalent to ring/create-resource-handler too. I ended up avoiding the issue by not passing arguments. By default, it mounts resources/public to "/". I then put my static files in a subdirectory that gave me the path I wanted.

timofey.sitnikov12:09:56

OK, got it, deps.edn must include the resources` directory like so: :paths ["src" "resources"], the static files do need to be in the project_root/resources/public, and the path parameter is required like so (ring/create-resource-handler {:path "/"})

timofey.sitnikov12:09:18

Thank you @ and @

cgrand12:09:45

@timofey.sitnikov I believe it looks for the resource on the classpath

cgrand12:09:23

so move your file under project_root/resources/public/

teodorlu12:09:44

@timofey.sitnikov note also that you're referring to docs for an old version (0.3.10) of Reitit. Docs for the latest version (0.5.5): https://cljdoc.org/d/metosin/reitit/0.5.5/doc/ring/static-resources

rosschapman14:09:56

Can someone help me understand the syntax of for in this toy code example? Why is it that let binding becomes a keyword :let:

(for [x (range 1 6)
      :let [y (* x x)
            z (* x x x)]]
  [x y z])

rosschapman14:09:57

I mean I understand this is how it just is but curious about why the modifiers turn into keywords if it’s not too esoteric.

alexmiller14:09:56

:let is a special modifier understood by for (`for` is a macro and basically has its own syntax)