Fork me on GitHub
#clojure
<
2023-03-01
>
emccue15:03:21

Transit seems to interpret "~" as a tagged string with no tag and no string value. Is this the proper interpretation?

Alex Miller (Clojure team)15:03:16

(#C031CBEUTGS is a thing btw) I'm a bit rusty, but I don't think that is a valid piece of transit

emccue15:03:43

I'll transition to there

respatialized16:03:41

Say I’ve got a map of keys and sequential values like: {:k [k1 k2 k3] :l [l1 l2]} How would I go about creating the permutations of all the keys and values in separate maps? e.g something like:

[{:k k1 :l l1} {:k k2 :l :l1} … {:k k3 :l l2}]

dpsutton16:03:39

(let [m {:k '[k1 k2 k3] :l '[l1 l2]}]
  (for [k (:k m)
        l (:l m)]
    {:k k :l l}))
({:k k1, :l l1}
 {:k k1, :l l2}
 {:k k2, :l l1}
 {:k k2, :l l2}
 {:k k3, :l l1}
 {:k k3, :l l2})

respatialized16:03:32

I perhaps should have specified that I would like a solution that doesn’t depend on manually enumerating the keys - one that works for any arbitrary map of this shape

Martin Půda16:03:47

(defn perm [m]
  (let [ks (keys m)
        vs (vals m)]
    (mapv #(zipmap ks %)
          (apply combi/cartesian-product vs))))

(perm '{:k [k1 k2 k3] :l [l1 l2]})
=> [{:k k1, :l l1} {:k k1, :l l2} {:k k2, :l l1} {:k k2, :l l2} {:k k3, :l l1} {:k k3, :l l2}]

1
dpsutton20:03:44

A question about load-file. The docstring of ns states > Sets ns to the namespace named by name (unevaluated), creating it > if needed. and of course, if you evaluate an ns form it sets your repl. But load-file states > Sequentially read and evaluate the set of forms contained in the file. So that gives me the expectation that (load-file "some_file.clj") would evaluate any ns form in some_file.clj if present, and thereby setting the namespace to the ns defined by that form. But this does not seem to be the case. Should a docstring be updated to reflect this?

seancorfield20:03:25

Are you saying you'd expect it to change *ns* in the calling code?

dpsutton20:03:24

that’s what the docstrings together imply right? load-file evaluates forms, ns form sets the current namespace. So if you load-file on a file that has an ns form my expectation is that it does all of the side effects of evaluating those forms

dpsutton20:03:58

it looks like Compiler.load does Var.pushThreadBindings and then pops at the end which is presumably what inhibits the changing of the ns

seancorfield20:03:39

Yeah, I would not expect *ns* changes to bleed out of loading (and eval'ing) a file...

dpsutton20:03:02

i expect that from practice but not from their docstrings

seancorfield20:03:04

So I guess clarification of the push/pop in load-file's docstring would help.

dpsutton20:03:48

(i’ve also exclusively used require with :reload and have to use load-file to work around a bug in cljs. And my first worry after reading the docstring is that it would change the current ns. then I was confused as to why it didn’t

borkdude21:03:43

load-file doesn't change the current ns, the ns binding is restored (popped) afterwards

borkdude21:03:10

neither does load-string:

user=> (load-string "(ns foo)")
nil
user=>

dpsutton21:03:24

yeah. i understand the implementation, I was just surprised that the docstring lead me to believe the opposite

👍 1
dpsutton21:03:54

and when i had to switch my workflow, i read the docstring to see if it would affect me. the docstring led me to believe it would change it. but of course it doesn’t