Fork me on GitHub
#rewrite-clj
<
2021-03-13
>
lread02:03:02

Awesome @vemv ! Much thanks for the report!

✌️ 4
reefersleep22:03:28

I’m trying to edit map literals in a string and print the string back out, and I’m confounded that the maps are returned as a single line rather than the line-by-line form that were originally in the string. I thought the whitespace would have been preserved.

lread23:03:10

Ah… maybe… I see? So z/edit performs an sexpr on the node and the resulting form (which has lost whitespace) is coerced back to a node.

reefersleep23:03:37

So I should use something other than z/edit?

borkdude23:03:13

@lee I have some assoc like functions here https://github.com/borkdude/rewrite-edn which work on rewrite-clj nodes

borkdude23:03:07

sorry I meant to tag @reefersleep

borkdude23:03:00

I guess you can just use rewrite-edn as an add-on lib

borkdude23:03:56

it also has assoc-in, update, update-in

lread23:03:07

@reefersleep, you could also look at z/assoc.

reefersleep23:03:11

That yield a weird result like this:

{:a :b
 :c :d :some-key 10}

borkdude23:03:44

ah right, I think rewrite-edn does try to do better here

borkdude23:03:13

but in general it's hard to take into account a general indentation style/context

reefersleep11:03:06

Yes, I certainly understand that. Even if you assume a standard, you’re increasing the lexical scope that assoc needs to know about. And if you don’t assume a standard, well,

borkdude11:03:26

Anyway, after automatically formatting with cljfmt or whatever these problems might go away?

reefersleep11:03:45

That might also be an option

lread23:03:28

Or find :some-key and edit or replace the token to the right. Many ways to roam around a zipper.

reefersleep23:03:27

Cheers both 🙂

reefersleep23:03:56

@borkdude do I have to check rewrite-edn out and build it locally because it’s a proof of concept?

borkdude23:03:18

let me just cut a 0.0.1 release now

🙌 3
borkdude23:03:14

v0.0.1 released

borkdude23:03:44

@lee Are there any codox-like docs for rewrite-clj?

borkdude23:03:37

ah yes, I think in your case z/assoc would also have worked

lread23:03:55

I thinks so too, but @reefersleep did not have success.

lread23:03:51

@reefersleep, if you are still stuck after your next attempts, share a small sample of what you are parsing along with your code and we’ll help.

reefersleep23:03:59

Here’s a tiny repro of the z/assoc problem:

(-> 
"{:a :b
 :c :d}"
    z/of-string
    (z/assoc :e :f)
    z/print-root)

reefersleep23:03:19

Yields:

{:a :b
 :c :d :e :f}

lread23:03:07

And you are looking to format differently with whitespace?

lread23:03:43

(what would you like the yields to be?)

borkdude23:03:30

rewrite-edn puts it on a new line, indented like the previous k/v pair

reefersleep23:03:59

Like what borkdude says 🙂

borkdude23:03:51

it's not the only right answer though, just a different answer that I happened to like

reefersleep23:03:09

Oh yeah. Any choice is an assumption.

lread23:03:08

so maybe rewrite-edn is your friend here!

reefersleep01:03:22

Turns out it is!

lread01:03:10

Yipee! 🙂

reefersleep23:03:54

I’m trying to use r/assoc, but it assumes a node, and I’m in zipper-mode. Is there a way to go from zipper to node and back?

reefersleep23:03:00

(I see I can go one way)

borkdude23:03:25

@reefersleep from the top of my head: maybe (z/replace zloc (r/assoc (z/node zloc) .. ..))

reefersleep23:03:24

Brilliant! That works great!

borkdude23:03:40

afk now, cya tomorrow

reefersleep23:03:33

Thanks, sleep tight!