Fork me on GitHub
#startup-in-a-month
<
2021-01-27
>
bringe20:01:53

@andyfry01 Love to see your use of the comment form! It really is like a notebook where you can just try things out, come back to it later, and build things up.

bringe21:01:33

Inside the comment forms in Calva, you can have your cursor anywhere inside a form and hit alt+enter (but the Mac equivalent) to evaluate, so no need to put your cursor at the beginning/end of the form to evaluate (not sure if that was done in the latest stream).

bringe21:01:38

Another tip - you can refactor this use of swap! to be something like (swap! app-state random-food 0) if you switch the args around to random-food.

pez21:01:45

alt+enter on Mac as well.

bringe21:01:11

Oh, I thought alt was something else on Mac, is it not?

pez21:01:59

It is sometimes/often labeled option, but it is aka alt.

👍 3
pez21:01:54

This has confused some Mac users, because they do not find the alt key on their keyboards. So it is good to be aware of.

afry23:01:04

I've been a mac user for ... a while, and to this day I get mixed up with what keys are to be found on which keyboards 😛

afry23:01:38

It gets especially weird when apps start swapping around control with command

afry23:01:44

I was actually trying out option+enter (windows equivalent: alt+enter earlier today. I was trying to use that to load the whole comment block, vars and all, into the REPL. There may be a bug, or maybe I don't understand the command fully, but if I try it with this code for example, I get Unable to resolve symbol: hey in this context

(comment
    (def hey "hey")
    (def wat "wat")
    (str hey wat))

afry23:01:13

Thus requiring me to go through each form, load it into the REPL, and then evaluate (str hey wat) at the end.

bringe00:01:38

So if you actually evaluate the whole comment form it doesn't evaluate any of the forms inside it. That's basically the point of it. When you use a comment form as a playground you need to evaluate each form inside it individually. In Calva, alt+enter if inside a form inside a comment form, will evaluate the top level form inside the comment.

bringe00:01:21

So if you put your cursor in the first def like here: (def |hey "hey") and hit alt-enter the whole def form there will be evaluated, not the outer comment form.

bringe00:01:57

It follows usual evaluation rules from within the comment form.

bringe00:01:17

So if you wanted to eval all those defs at once for some reason, you could put them inside a do

bringe00:01:44

Usually you'd be working with individual forms though. Maybe you want to try some code that has var names in it already - instead of using individual defs you could use one let.

(let [hey "hey"
        wat "wat"]
    (str hey wat))

pez07:01:01

Just want to add about the top-level evaluation inside comment forms. The way it is designed is that the comment form creates a new top level context. Let’s look at your example, @andyfry01:

(comment
    (def hey "hey")
    (def wat "wat")
    (str hey w|at))
(The | represents the cursor.) Since the comment form created a new top level context, what the top-level evaluator “sees” is this:
(def hey "hey")
(def wat "wat")
(str hey w|at)
And since the current one top level form then is Which is then evaluated, and if hey is not defined, well, you’ve seen what happens.Just want to add about the top-level evaluation inside comment forms. The way it is designed is that the comment form creates a new top level context. Let’s look at your example, @andyfry01:
(comment
    (def hey "hey")
    (def wat "wat")
    (str hey w|at))
(The | represents the cursor.) Since the comment form created a new top level context, what the top-level evaluator “sees” is this:
(def hey "hey")
(def wat "wat")
(str hey w|at)
And since the current one top level form then is (str hey wat) that is then evaluated, and if hey is not defined, well, you’ve seen what happens. You expect the whole contents of the comment form to be evaluated. I can see why, but the thing is that if we didn’t treat comment forms special at all what would be evaluated is the whole comment form (not just it’s content) and that would give you nil. Calva tries to make it easy for you to be in complete control of what is evaluated. So inside that Rich comment block you can move between redefining hey and then evaluate the str expression. You might not want to evaluate the last mentioned one at the same time (it could be expensive in some way). So, you are in control. Sometimes we do want to evaluate a bunch of expressions in one go. For this Clojure gives us the do macro. So
(comment
  (do
    (def hey "hey")
    (def wat "wat")
    (str hey w|at)))
I hope I didn’t confuse things now… 😃

afry12:01:03

Ah, that's a really interesting way to think about comment: a new top-level context. Like a namespace inside a namespace, where you can get the advantage of using all of the other vars which are defined in the parent namespace, but you don't have to worry about anything in the comment form interfering with the parent namespace. I can dig that. Block scoped variables in ES6 Javascript is the parallel that springs to mind right away:

const parentNamespace = () => {
    const variable = "Hello from the outer context"

    const richComment = () => {
        const variable = "Hello from the inner context"
        console.log(variable) 
    }
    
    richComment() // -> "Hello from the inner context"
    console.log(variable)
}

parentNamespace() // -> "Hello from the outer context"

afry12:01:54

And yeah, I can see the benefit of defining and loading individual vars within a comment form as opposed to loading the whole comment form all at once. I'll give that do thing a try when I've got some setup code that I want to run all at once.

afry12:01:33

You guys have been invaluable this month for my Clojure growing pains 😅 Thanks so much for all of the tips, it's really improving my workflow

pez12:01:48

You are most welcome, sir!

pez12:01:40

About namespacing. Note that this is only a structural thing regarding the text in the file. When you evaluate something in the comment block it will both be “from” the same namespace as the file and “to” the same namespace. So hey will be defined in the file’s namespace. Don’t know if you meant anything else, but just in case. 😃

pez12:01:41

Looking at your ES6 example it seems like my clarification was needed. Haha.

pez12:01:37

Clojure has global bindings that can be scoped by namespaces, and local bindings that are lexically scoped, much like the ES6 example there.

bringe19:01:15

> the thing is that if we didn’t treat `comment` forms special at all what would be evaluated is the whole `comment` form (not just it’s content) and that would give you `nil`. Just to clarify here, and maybe Peter meant this, but if the whole comment form is evaluated, the result is nil and none of the forms inside it are evaluated.

3
bringe19:01:53

So if you have defs in there and they haven't been evaluated, and you evaluate the whole comment form, those vars will still not be defined.

3
bringe21:01:59

The function passed to swap! will receive the value of the atom as its first param

bringe21:01:00

Also, that issue with Calva asking for the nrepl port at jack-in should be fixed soon

👍 3
afry23:01:50

@brandon.ringe The comment form has been revolutionary for me! I've got a few of them already in the file I was working in today. You did indeed guess it: they're all laid out like little scratch pads where I can define a bunch of variables and functions and experiment around with them, and I can isolate different ideas inside the scopes of each comment.