Fork me on GitHub
#beginners
<
2021-03-13
>
dpsutton01:03:48

its in clojure.repl/pst.

Chase01:03:06

Yep, sorry I just deleted that on ya. I figured that out on a hunch seconds before you posted

dpsutton01:03:14

check out (source clojure.main/repl-requires)

dpsutton01:03:18

no worries ūüôā

fappy02:03:43

[Audience: What about the condition system? You know, restarts ...]

You know, I looked at that. I mean, I actually think that the condition system is a hard coded special case in Common Lisp. That if you had some more general primitives, the condition system would just be one thing you could implement with them.

In Clojure, you have two things you can utilize to help you with those kinds of problems. One is the exception system, which allows you to travel up and down the stack.

The other is that dynamic binding of functions. You want to have a function. I am going to bind this function to something that you should call if you have a problem. Well now you have got that ability to call some code from above down low to see what you should be doing. Combining dynamic functions and exceptions gives you some of the condition system.

[Audience: But it does not give you restarts.]

[Audience: You can, once you throw an exception.]

But you do not throw an exception. That is what I am saying. Let us say you are somebody who needs to talk to the file system. And you say: if I have a problem talking to the file system, I am going to call "I had a problem talking to the file system". Somebody above you can bind that to do whatever you want. You are going to call that and use the return value, and not throw.

Right, and so I think that gets you most of the way there. Dynamically rebinding of functions and exceptions, I think, is enough. I like the condition system, but it seems to me to be a hard wired special construct, rather than two ...
‚ÄĒ from https://github.com/matthiasn/talk-transcripts/blob/master/Hickey_Rich/ClojureIntroForLispProgrammers.md What is Rich Hickey referring to, when he mentions ‚Äúthe exception system, which allows you to travel up and down the stack‚ÄĚ ?

blak3mill3r02:03:55

He's talking about the JVM's built in exception handling behavior, which unwinds stack frames one at a time, re-throwing the exception, until it is caught.

andy.fingerhut02:03:47

It isn't clear to me what he means when he says "up and down the stack", though. It is clear exceptions let you go one way (popping things off the stack)

blak3mill3r02:03:06

yeah the "down" is less clear but I think I grok

blak3mill3r02:03:42

when you use try it compiles to bytecode that will insert an entry in the exception table, then you might call another function inside the body of the try... in this sense you are "on your way down the stack" and the exception system is at work

blak3mill3r02:03:10

though the entries in the exception table have nothing to do with the stack IIUC

blak3mill3r02:03:22

they just include ranges of bytecode for which that exception handler applies

andy.fingerhut02:03:46

They do have something to do with the call stack in that some stack frames have an exception handler, but others might not.

blak3mill3r02:03:03

sure, I just mean at the jvm bytecode level

andy.fingerhut02:03:10

At least, they can be viewed as behaving that way, regardless of the underlying implementation.

blak3mill3r02:03:30

that seems right

andy.fingerhut02:03:36

The mechanism for throwing an exception in the JVM does behave like it is popping frames off the call stack, and in each one, checking whether an exception handler exists in the byte code location where the call occurred.

fappy02:03:48

So control is traveling according to your code and the bindings you decided beforehand. But you (the developer) can‚Äôt travel arbitrarily while you‚Äôre testing and developing in the context of an eval, right? (I think what was confusing me was he said ‚Äúyou‚ÄĚ and this was in comparison (in the conversation) to Common Lisp where you really could pick an arbitrary frame to restart after tweaking a binding.)

andy.fingerhut02:03:11

Are you talking about Common Lisp systems that let you enter an interactive debugger, modify bindings,and continue running the program?

fappy02:03:37

Yeah, that’s in the context of the video clip at that point …

andy.fingerhut02:03:58

There are some things at least similar to that in the Clojure world, none that I have personally used.

fappy02:03:20

clojureman lists others. I wonder if there are any more missing from his list

andy.fingerhut02:03:31

One kind of hand-rolled one can be found by searching for "break" in this talk transcript: https://github.com/matthiasn/talk-transcripts/blob/master/Halloway_Stuart/REPLDrivenDevelopment.md

thanks2 2
andy.fingerhut02:03:28

I haven't looked for anything that is more like what some CL implementation give you, since I personally have never gotten into that style of debugging much.

andy.fingerhut02:03:06

That clojureman special project readme lists way more things than I would have ever recalled

blak3mill3r02:03:32

I feel like I've seen quite a few clojure-debug projects come and go, though I can't recall how to find very many of them... I don't think I've ever seen the ability to pick a particular stack frame and resume from there (without setting a breakpoint there first)

blak3mill3r02:03:58

sounds like an interesting feature, although I must admit I don't know exactly what I'd do with it if I had it

fappy03:03:53

Probably in cases where it is very expensive or inconvenient to get to back to the same setup to re-try a proposed fix. If we have well-thought-out functions and manage state well, then normally this isn’t an issue. But we don’t always get to work with clean code. Restarting from an arbitrary stack frame feels like traveling back in time. But of course any side effects that already happened remain, and will be attempted again …

Singh04:03:44

Hi I had a quick question when I try to run this function i always get this .call is not a function I am not sure why it’s not working. Am I doing something wrong?

(def word (atom "")) 
(defn testing [string]
    (if (= string "test") (swap! word "changed Value"))word)
(testing "test")
this is what I am trying to accomplish ( I am js dev)
function testing(string) {
let word = "";
if(string === "test") {
word = "changed Value"
}
return word
}
testing("test")
output: "changed Value"

R.A. Porter04:03:48

swap! takes a function as its second argument, that accepts the current value and transforms it. Your string is not a function. For your case, you'd want to call reset!

ūüôĆ 2
Singh04:03:52

Thank you for helping @U01GXCWSRMW noob in clojure ūüôā

Rowan Barnard07:03:54

Hi everyone, just wanted to know if I am correctly executing the main function of a clojure file using the clojure command-line tools. I used the command: clojure -M -m pinger.core The pinger.core clj file contains my main function. The command worked, just wanting to know if that is the correct way of doing it as I just inferred it from reading the clojure command-line tools help output and re-working the book's (that I am learning from) command of clj -m pinger.core which seems to no longer be the correct way of doing it.

alexmiller14:03:21

That is correct

alexmiller14:03:03

-M means to run clojure.main, and -m is passed to clojure.main with a main class name

Rowan Barnard05:03:21

OK thank you very much Alex ūüôā

Rowan Barnard07:03:03

Also when I first ran the code I got back an UnknownHostException for http://www.google.com but I can't repro it as afterwards every subsequent run went fine.

Rowan Barnard07:03:49

The source code for that is: (ns pinger.core   `(:import [http://java.net URL HttpURLConnection]))` (defn response-code [address]   `(let [conn ^HttpURLConnection (.openConnection (URL. address))`         `code (.getResponseCode conn)]`     `(when (< code 400)`       `(-> conn .getInputStream .close))`     `code))` (defn available? [address]   `(= 200 (response-code address)))` (defn -main []   `(let [addresses ["https://google.com"`                    `"https://clojure.org"`                    `"http://google.com/badurl"]]`     `(while true`       `(doseq [address addresses]`         `(println address ":" (available? address)))`       `(Thread/sleep (* 1000 60)))))`

Rowan Barnard07:03:55

So was just curious as to why I might have gotten an UnknownHostException upon first running it?

anonimitoraf10:03:47

Hi guys, I'm struggling to avoid thinking of modelling concurrent code with JS's Promises. It feels quite convenient to have functions that return Promises. I've written trivial code which use core.async semantics but I've not found tutorials/posts/etc that really go into detail into how JS Promises can be replaced by core.async semantics. Am I wrong in thinking that JS Promises solve totally separate problems that core.async is supposed to? If anything, it feels like core.async is much better compared with JS AsyncIterators

robertfw10:03:40

Give this talk a watch, Rich compares the core.async approach with promises https://www.youtube.com/watch?v=drmNlZVkUeE

ūüėć 2
anonimitoraf11:03:24

Sweet, thanks. I'll check it out

dazld12:03:45

I’m trying to learn macros, and am struggling a bit with something, I can’t work out how to turn a symbol->var->keyword of that var. Given something like the below:

(defmacro foo
  [data]
  (clojure.walk/prewalk (fn [part]
                          (if (symbol? part)
                            (if-let [resolved-var# (resolve &env part)]
                              (let [kw# (keyword resolved-var#)]
                                `[~kw# ~resolved-var#])
                              part)
                            part))
                        data))
=> #'user/foo
(defn bar [a b]
  (+ a b))
=> #'user/bar
(foo [bar])
=> [[nil #'user/bar]]
..I’m trying to get something like [[:user/bar #'user/bar]] out. what am I missing? I’ve tried (keyword resolved-var#)` but - predictably - that gives me the auto gensym, something like [[:resolved-var__31201__auto__ #'user/bar]] - not the originally passed symbol. Any ideas?

dazld13:03:34

ah, ok -

(keyword (symbol resolved-var#))
did the trick